Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage width and height to generate code bar #43

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions RSBarcodes/RSCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down
40 changes: 24 additions & 16 deletions RSBarcodes/RSCodeGenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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
8 changes: 4 additions & 4 deletions RSBarcodes/RSUnifiedCodeGenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -123,17 +123,17 @@ - (UIImage *)genCodeWithContents:(NSString *)contents
codeGen.strokeColor = self.strokeColor;

return [codeGen genCodeWithContents:contents
machineReadableCodeObjectType:type];
machineReadableCodeObjectType:type withWidth:width withHeight:height];
} else {
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