diff --git a/RSBarcodes/RSCodeGenerator.h b/RSBarcodes/RSCodeGenerator.h index f8ce3b6..6a7cbbb 100644 --- a/RSBarcodes/RSCodeGenerator.h +++ b/RSBarcodes/RSCodeGenerator.h @@ -18,10 +18,10 @@ @required - (UIImage *)genCodeWithMachineReadableCodeObject: -(AVMetadataMachineReadableCodeObject *)machineReadableCodeObject; +(AVMetadataMachineReadableCodeObject *)machineReadableCodeObject withWidth:(CGFloat)width withHeight:(CGFloat)height; - (UIImage *)genCodeWithContents:(NSString *)contents - machineReadableCodeObjectType:(NSString *)type; + machineReadableCodeObjectType:(NSString *)type withWidth:(CGFloat)width withHeight:(CGFloat)height; /** The fill (background) color of the generated barcode. */ @property (nonatomic, strong) UIColor *fillColor; @@ -98,7 +98,7 @@ extern NSString *const DIGITS_STRING; * * @return Encoded image. */ -- (UIImage *)drawCompleteBarcode:(NSString *)code; +- (UIImage *)drawCompleteBarcode:(NSString *)code withWidth:(CGFloat)width withHeight:(CGFloat)height; @end diff --git a/RSBarcodes/RSCodeGenerator.m b/RSBarcodes/RSCodeGenerator.m index c04345e..389aced 100644 --- a/RSBarcodes/RSCodeGenerator.m +++ b/RSBarcodes/RSCodeGenerator.m @@ -14,6 +14,14 @@ @implementation RSAbstractCodeGenerator NSString *const DIGITS_STRING = @"0123456789"; +CGFloat const WIDTH_REF = 90.f; +CGFloat const HEIGHT_REF = 28.f; + +CGFloat const TOP_SPACING_REF = 1.5f; +CGFloat const BOTTOM_SPACING_REF = 2.f; +CGFloat const SIDE_SPACING_REF = 2.f; +CGFloat fixRatio = 0.8; + - (BOOL)isContentsValid:(NSString *)contents { if (contents.length > 0) { for (int i = 0; i < contents.length; i++) { @@ -45,7 +53,7 @@ - (NSString *)completeBarcode:(NSString *)barcode { stringWithFormat:@"%@%@%@", [self initiator], barcode, [self terminator]]; } -- (UIImage *)drawCompleteBarcode:(NSString *)code { +- (UIImage *)drawCompleteBarcode:(NSString *)code withWidth:(CGFloat)width withHeight:(CGFloat)height { if (code.length <= 0) { return nil; } @@ -55,9 +63,14 @@ - (UIImage *)drawCompleteBarcode:(NSString *)code { // Top spacing = 1.5 // Bottom spacing = 2 // Left & right spacing = 2 - // Height = 28 - CGSize size = CGSizeMake(code.length + 4, 28); - UIGraphicsBeginImageContextWithOptions(size, NO, 0); + // Height = 28 = 112 + // Width = 90 ==> lineWith 1 + + CGFloat widthRatio = width / WIDTH_REF; + CGFloat heightRatio = height / HEIGHT_REF; + + CGSize size = CGSizeMake(width, height); + UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetShouldAntialias(context, false); @@ -74,14 +87,13 @@ - (UIImage *)drawCompleteBarcode:(NSString *)code { [self.strokeColor setStroke]; CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height)); - CGContextSetLineWidth(context, 1); + CGContextSetLineWidth(context, widthRatio * fixRatio); for (int i = 0; i < code.length; i++) { NSString *character = [code substringWithRange:NSMakeRange(i, 1)]; if ([character isEqualToString:@"1"]) { - CGContextMoveToPoint(context, i + (2 + 1), 1.5); - CGContextAddLineToPoint(context, i + (2 + 1), size.height - 2); - } + CGContextMoveToPoint(context, ((i + (SIDE_SPACING_REF * heightRatio + widthRatio)) * widthRatio) * fixRatio, TOP_SPACING_REF * heightRatio); + CGContextAddLineToPoint(context, ((i + (SIDE_SPACING_REF * heightRatio + widthRatio)) * widthRatio) * fixRatio , size.height - BOTTOM_SPACING_REF * heightRatio); } } CGContextDrawPath(context, kCGPathFillStroke); @@ -95,19 +107,15 @@ - (UIImage *)drawCompleteBarcode:(NSString *)code { #pragma mark - RSCodeGenerator - (UIImage *)genCodeWithContents:(NSString *)contents - machineReadableCodeObjectType:(NSString *)type { - if ([self isContentsValid:contents]) { - return [self - drawCompleteBarcode:[self completeBarcode:[self barcode:contents]]]; - } - return nil; + machineReadableCodeObjectType:(NSString *)type withWidth:(CGFloat)width withHeight:(CGFloat)height { + return [self drawCompleteBarcode:[self completeBarcode:[self barcode:contents]] withWidth:width withHeight:height]; } - (UIImage *)genCodeWithMachineReadableCodeObject: (AVMetadataMachineReadableCodeObject *) -machineReadableCodeObject { +machineReadableCodeObject withWidth:(CGFloat)width withHeight:(CGFloat)height { return [self genCodeWithContents:[machineReadableCodeObject stringValue] - machineReadableCodeObjectType:[machineReadableCodeObject type]]; + machineReadableCodeObjectType:[machineReadableCodeObject type] withWidth:width withHeight:height]; } @end diff --git a/RSBarcodes/RSUnifiedCodeGenerator.m b/RSBarcodes/RSUnifiedCodeGenerator.m index 2b5b8ef..9969055 100644 --- a/RSBarcodes/RSUnifiedCodeGenerator.m +++ b/RSBarcodes/RSUnifiedCodeGenerator.m @@ -68,7 +68,7 @@ + (instancetype)codeGen { #pragma mark - RSCodeGenerator - (UIImage *)genCodeWithContents:(NSString *)contents - machineReadableCodeObjectType:(NSString *)type { + machineReadableCodeObjectType:(NSString *)type withWidth:(CGFloat)width withHeight:(CGFloat)height { if ([type isEqualToString:AVMetadataObjectTypeQRCode] || [type isEqualToString:AVMetadataObjectTypePDF417Code] || [type isEqualToString:AVMetadataObjectTypeAztecCode]) { @@ -123,7 +123,7 @@ - (UIImage *)genCodeWithContents:(NSString *)contents codeGen.strokeColor = self.strokeColor; return [codeGen genCodeWithContents:contents - machineReadableCodeObjectType:type]; + machineReadableCodeObjectType:type withWidth:width withHeight:height]; } else { return nil; } @@ -131,9 +131,9 @@ - (UIImage *)genCodeWithContents:(NSString *)contents - (UIImage *)genCodeWithMachineReadableCodeObject: (AVMetadataMachineReadableCodeObject *) -machineReadableCodeObject { +machineReadableCodeObject withWidth:(CGFloat)width withHeight:(CGFloat)height { return [self genCodeWithContents:[machineReadableCodeObject stringValue] - machineReadableCodeObjectType:[machineReadableCodeObject type]]; + machineReadableCodeObjectType:[machineReadableCodeObject type] withWidth:width withHeight:height]; } @end