Objective-C Requires Chilkat v11.7.0+
Objective-C
Fit a QR Code within a Maximum Size
See more QR Code Examples
Demonstrates limiting the size of the image. render.maxWidth and render.maxHeight are the largest image allowed. With render.sizing set to shrinkToFit, QrCode starts from the module size asked for and reduces it, a whole pixel at a time, until the image fits, but never below render.minModuleSize. If it cannot fit, the call fails with noFit rather than returning a code too small to scan.Chilkat Objective-C Downloads
#import <CkoJsonObject.h>
#import <CkoFormats.h>
BOOL success = NO;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"payload.text" value: @"https://www.chilkatsoft.com/barcode_overview.asp"];
// Ask for 10-pixel modules, but accept anything down to 3 pixels to stay within 300 x 300.
[json UpdateInt: @"render.moduleSize" value: [NSNumber numberWithInt: 10]];
[json UpdateString: @"render.sizing" value: @"shrinkToFit"];
[json UpdateInt: @"render.minModuleSize" value: [NSNumber numberWithInt: 3]];
[json UpdateInt: @"render.maxWidth" value: [NSNumber numberWithInt: 300]];
[json UpdateInt: @"render.maxHeight" value: [NSNumber numberWithInt: 300]];
[json UpdateString: @"output.path" value: @"qa_output/qr_fit_300.png"];
[json UpdateBool: @"output.overwrite" value: YES];
[json UpdateString: @"output.return" value: @"none"];
CkoFormats *fmt = [[CkoFormats alloc] init];
CkoJsonObject *jsonOut = [[CkoJsonObject alloc] init];
success = [fmt QrCode: @"generate" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@",fmt.LastErrorText);
return;
}
NSLog(@"%@%@%@",@"module asked for: ",[jsonOut StringOf: @"measurements[0].requestedPitch"],@" pixels");
NSLog(@"%@%d%@",@"module used: ",[[jsonOut IntOf: @"measurements[0].pitch"] intValue],@" pixels");
NSLog(@"%@%d%@%d%@",@"image: ",[[jsonOut IntOf: @"measurements[0].canvasWidth"] intValue],@" x ",[[jsonOut IntOf: @"measurements[0].canvasHeight"] intValue],@" pixels");
// The image written to qa_output/qr_fit_300.png:
//
// The same code cannot fit in 100 x 100 with modules of at least 3 pixels.
[json UpdateInt: @"render.maxWidth" value: [NSNumber numberWithInt: 100]];
[json UpdateInt: @"render.maxHeight" value: [NSNumber numberWithInt: 100]];
success = [fmt QrCode: @"generate" jsonIn: json jsonOut: jsonOut];
if (success == NO) {
NSLog(@"%@%@%@%@%@",@"100 x 100: ",[jsonOut StringOf: @"errors[0].code"],@" (",[jsonOut StringOf: @"errors[0].message"],@")");
}
// Sample output:
// module asked for: 10 pixels
// module used: 7 pixels
// image: 287 x 287 pixels
// 100 x 100: noFit (the symbol does not fit the render constraints: minModuleSize)