I'm testing ZXingObjC to use in a future project on OS X. It will use the library to recognize bar codes in images from a certain directory. To test the library I used some simple barcodes (attached at the bottom).
My test-case is built on a default Xcode 4 Cocoa project. I added an NSImageView (imageView) and a NSTextField (textField) to show the image and results.
I'm using the following code to load and process the image:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Set load and set image
NSImage *image = [[[NSImage alloc] initWithContentsOfFile:[@"~/Documents/test.jpg" stringByExpandingTildeInPath]] autorelease];
imageView.image = image;
// Clear result text field
textField.stringValue = @"";
CGImageRef imageToDecode = [image CGImageForProposedRect:nil context:nil hints:nil]; // Given a CGImage in which we are looking for barcodes
ZXLuminanceSource* source = [[[ZXCGImageLuminanceSource alloc] initWithCGImage:imageToDecode] autorelease];
ZXBinaryBitmap* bitmap = [ZXBinaryBitmap binaryBitmapWithBinarizer:[ZXHybridBinarizer binarizerWithSource:source]];
NSError* error = nil;
// There are a number of hints we can give to the reader, including
// possible formats, allowed lengths, and the string encoding.
ZXDecodeHints* hints = [ZXDecodeHints hints];
ZXMultiFormatReader* reader = [ZXMultiFormatReader reader];
ZXResult* result = [reader decode:bitmap
hints:hints
error:&error];
if (result) {
// The coded result as a string. The raw data can be accessed with
// result.rawBytes and result.length.
NSString* contents = result.text;
// The barcode format, such as a QR code or UPC-A
ZXBarcodeFormat format = result.barcodeFormat;
[hints addPossibleFormat:kBarcodeFormatCode128]; // ##############
textField.stringValue = [NSString stringWithFormat:@"%@ (%u)", contents, format];
} else {
// Use error to determine why we didn't get a result, such as a barcode
// not being found, an invalid checksum, or a format inconsistency.
textField.stringValue = @"Error";
}
}
It is - as you probably recognized - 99% example code from the README. I also added al the frameworks from the OSX section of the README. The app builds, but then it goes wrong. At first it just hanged while taking 100% CPU. I actually did let it run for an hour or so, guess it's safe to say that it isn't doing some real processing.
After trying several things, I discovered that it didn't hung when adding some possible formats. I tried a few and the results were strange to me:
kBarcodeFormatEan8 recognizes everything as "00000000"
kBarcodeFormatCode128 doesn't recognize anything at all
kBarcodeFormatCode39 hangs
I don't think I misconfigured it, I tried a lot of different images and formats, but the results is the same.
I'm testing ZXingObjC to use in a future project on OS X. It will use the library to recognize bar codes in images from a certain directory. To test the library I used some simple barcodes (attached at the bottom).
My test-case is built on a default Xcode 4 Cocoa project. I added an
NSImageView
(imageView
) and aNSTextField
(textField
) to show the image and results.I'm using the following code to load and process the image:
It is - as you probably recognized - 99% example code from the README. I also added al the frameworks from the OSX section of the README. The app builds, but then it goes wrong. At first it just hanged while taking 100% CPU. I actually did let it run for an hour or so, guess it's safe to say that it isn't doing some real processing.
After trying several things, I discovered that it didn't hung when adding some possible formats. I tried a few and the results were strange to me:
kBarcodeFormatEan8
recognizes everything as "00000000"kBarcodeFormatCode128
doesn't recognize anything at allkBarcodeFormatCode39
hangsI don't think I misconfigured it, I tried a lot of different images and formats, but the results is the same.
I'm running OS X 10.8.4 with Xcode 4.6.2.