Pablo
Pablo

Reputation: 3467

ZXingObjC can't decode image taken from UIImagePickerController

I'm using the following code to decode a QRCode taken from camera

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{  
originalImage = (UIImage *) [info objectForKey:
                             UIImagePickerControllerEditedImage];

ZXQRCodeReader *reader = [[ZXQRCodeReader alloc]init];

 ZXLuminanceSource *source = [[ZXCGImageLuminanceSource alloc] initWithCGImage:[originalImage CGImage]];
ZXHybridBinarizer *binazer = [ZXHybridBinarizer binarizerWithSource:source];
ZXBinaryBitmap *bitmap = [[ZXBinaryBitmap alloc]initWithBinarizer:binazer];

NSError *error;
ZXResult *result = [reader decode:bitmap
                            hints:nil
                            error:&error];
if(result){
    [[[UIAlertView alloc] initWithTitle:@"Success" message:@"Success"
                               delegate:nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil, nil] show];
} 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.
    [[[UIAlertView alloc] initWithTitle:@"ERROR" message:[error localizedDescription]
                               delegate:nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil, nil] show];
}

However, I'm getting an error message saying that no bar code was found on Image. On the other side if I use test images like the ones provided by ZXingObjC it works, the problem is just when trying UIImagePickerController images. So Any ideas what could be wrong???.

Thanks a lot.

Upvotes: 2

Views: 1477

Answers (2)

smparkes
smparkes

Reputation: 14063

I'm not all that familiar with the ZXingObjC port, so YMMV. The zxing heuristics have been tuned on relatively low res images as provided by live video preview streams on phones. On modern phones, an image taken from the still camera is very high resolution ... high enough that it can resolve individual pixels from an LCD (at least up to 2K displays). zxing often requires that such high res images be low-pass filtered. The easiest way to do this is to decimate/down-sample the image. That's what I do when I decode from a stil-camera image: first try it at native resolution and then decimate to something under 1000x1000. One or the other usually works.

Upvotes: 2

earthtrip
earthtrip

Reputation: 508

Just an update to smparkes comment is that by simply reducing the UIImage size down to say 640x480 the library works perfectly with the UIImagePickerController generated images at least on my iPhone 5. It's grabbing the UPC codes every time for me that way.

Upvotes: 1

Related Questions