From d0ce46f52586f69df6ebb7eb91b7a9ba79544a57 Mon Sep 17 00:00:00 2001 From: tlacan Date: Mon, 27 Nov 2017 17:10:18 +0100 Subject: [PATCH 1/6] add with/heigh on rscodegenerator.h --- RSBarcodes/RSCodeGenerator.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 From bdfcbb1b8b33668e9e3dd544e4e476954db9cf00 Mon Sep 17 00:00:00 2001 From: tlacan Date: Mon, 27 Nov 2017 17:14:11 +0100 Subject: [PATCH 2/6] Add width/height on RSCodegenerator.m --- RSBarcodes/RSCodeGenerator.m | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/RSBarcodes/RSCodeGenerator.m b/RSBarcodes/RSCodeGenerator.m index c04345e..5c41a3a 100644 --- a/RSBarcodes/RSCodeGenerator.m +++ b/RSBarcodes/RSCodeGenerator.m @@ -14,6 +14,13 @@ @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; + - (BOOL)isContentsValid:(NSString *)contents { if (contents.length > 0) { for (int i = 0; i < contents.length; i++) { @@ -45,7 +52,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 +62,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,13 +86,13 @@ - (UIImage *)drawCompleteBarcode:(NSString *)code { [self.strokeColor setStroke]; CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height)); - CGContextSetLineWidth(context, 1); + CGContextSetLineWidth(context, widthRatio); 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 + 1)) * widthRatio, TOP_SPACING_REF * heightRatio); + CGContextAddLineToPoint(context, (i + (SIDE_SPACING_REF * heightRatio + 1)) * widthRatio, size.height - BOTTOM_SPACING_REF * heightRatio); } } CGContextDrawPath(context, kCGPathFillStroke); @@ -95,19 +107,19 @@ - (UIImage *)drawCompleteBarcode:(NSString *)code { #pragma mark - RSCodeGenerator - (UIImage *)genCodeWithContents:(NSString *)contents - machineReadableCodeObjectType:(NSString *)type { + machineReadableCodeObjectType:(NSString *)type withWidth:(CGFloat)width withHeight:(CGFloat)height { if ([self isContentsValid:contents]) { return [self - drawCompleteBarcode:[self completeBarcode:[self barcode:contents]]]; + drawCompleteBarcode:[self completeBarcode:[self barcode:contents]] withWidth:width withHeight:height]; } return nil; } - (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 From cb521ff032e65e9df9d963d8c3ebc3a1e634d509 Mon Sep 17 00:00:00 2001 From: tlacan Date: Mon, 27 Nov 2017 17:15:56 +0100 Subject: [PATCH 3/6] Add width/height on RSUnifiedCodeGenerator.m --- RSBarcodes/RSUnifiedCodeGenerator.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/RSBarcodes/RSUnifiedCodeGenerator.m b/RSBarcodes/RSUnifiedCodeGenerator.m index 2b5b8ef..bb160f4 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: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 From a698779f7c78c0be45f40bcfa51fc5e98fdd88a3 Mon Sep 17 00:00:00 2001 From: tlacan Date: Mon, 27 Nov 2017 17:23:09 +0100 Subject: [PATCH 4/6] Fix RSUnifiedCodeGenerator.m --- RSBarcodes/RSUnifiedCodeGenerator.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RSBarcodes/RSUnifiedCodeGenerator.m b/RSBarcodes/RSUnifiedCodeGenerator.m index bb160f4..9969055 100644 --- a/RSBarcodes/RSUnifiedCodeGenerator.m +++ b/RSBarcodes/RSUnifiedCodeGenerator.m @@ -123,7 +123,7 @@ - (UIImage *)genCodeWithContents:(NSString *)contents codeGen.strokeColor = self.strokeColor; return [codeGen genCodeWithContents:contents - machineReadableCodeObjectType:withWidth:width withHeight:height]; + machineReadableCodeObjectType:type withWidth:width withHeight:height]; } else { return nil; } From 180dd8b4e84c62428a425c2b19c7b5d21d7d4f30 Mon Sep 17 00:00:00 2001 From: tlacan Date: Wed, 29 Nov 2017 12:12:03 +0100 Subject: [PATCH 5/6] Fix for work with scan --- RSBarcodes/RSCodeGenerator.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/RSBarcodes/RSCodeGenerator.m b/RSBarcodes/RSCodeGenerator.m index 5c41a3a..52690e3 100644 --- a/RSBarcodes/RSCodeGenerator.m +++ b/RSBarcodes/RSCodeGenerator.m @@ -20,6 +20,7 @@ @implementation RSAbstractCodeGenerator 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) { @@ -86,14 +87,13 @@ - (UIImage *)drawCompleteBarcode:(NSString *)code withWidth:(CGFloat)width withH [self.strokeColor setStroke]; CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height)); - CGContextSetLineWidth(context, widthRatio); + 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 + (SIDE_SPACING_REF * heightRatio + 1)) * widthRatio, TOP_SPACING_REF * heightRatio); - CGContextAddLineToPoint(context, (i + (SIDE_SPACING_REF * heightRatio + 1)) * widthRatio, size.height - BOTTOM_SPACING_REF * heightRatio); - } + 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); From 0fb133e1445423865d8c424dab4693d6e6e0e1b3 Mon Sep 17 00:00:00 2001 From: tlacan Date: Fri, 5 Apr 2019 11:05:14 +0700 Subject: [PATCH 6/6] Remove check on code valid return always an image --- RSBarcodes/RSCodeGenerator.m | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/RSBarcodes/RSCodeGenerator.m b/RSBarcodes/RSCodeGenerator.m index 52690e3..389aced 100644 --- a/RSBarcodes/RSCodeGenerator.m +++ b/RSBarcodes/RSCodeGenerator.m @@ -108,11 +108,7 @@ - (UIImage *)drawCompleteBarcode:(NSString *)code withWidth:(CGFloat)width withH - (UIImage *)genCodeWithContents:(NSString *)contents machineReadableCodeObjectType:(NSString *)type withWidth:(CGFloat)width withHeight:(CGFloat)height { - if ([self isContentsValid:contents]) { - return [self - drawCompleteBarcode:[self completeBarcode:[self barcode:contents]] withWidth:width withHeight:height]; - } - return nil; + return [self drawCompleteBarcode:[self completeBarcode:[self barcode:contents]] withWidth:width withHeight:height]; } - (UIImage *)genCodeWithMachineReadableCodeObject: