Scaning static UIImage using ios ZBar SDK
Asked Answered
R

2

1

I want to scan a static UIImage using Zbar sdk, it gives error i.e. ZBarSymbol is Null

I also checked

ZBarReaderController: UIImagePickerController

But as it is subclass of UIImagePickerController and i don't want to use UIImagePickerController at all as i just want to scan UIImage that i have reference in my application.. I searched allot for this problem but did not got any help..

I also tried for ZBarImageScanner as,

- (void)  scanFromUIImage: (UIImage *)img{


       // setup scaner


    ZBarImageScanner *scanner = [[ZBarImageScanner alloc] init];

    Settings *settings = [Settings globalSettings];
    //ZBarImageScanner *scanner = reader.scanner;

    BOOL haveLongLinear = NO;
    for(NSString *str in settings.enabledSymbologies) {
        NSNumber *value = [settings.enabledSymbologies objectForKey: str];
        BOOL enable = value.boolValue;
        zbar_symbol_type_t sym = [str integerValue];

        [scanner setSymbology: sym
                       config: ZBAR_CFG_ENABLE
                           to: enable];

        if(sym == ZBAR_EAN13) {
            // show EAN variants as such
            [scanner setSymbology: ZBAR_UPCA
                           config: ZBAR_CFG_ENABLE
                               to: enable];
            [scanner setSymbology: ZBAR_UPCE
                           config: ZBAR_CFG_ENABLE
                               to: enable];
            [scanner setSymbology: ZBAR_ISBN13
                           config: ZBAR_CFG_ENABLE
                               to: enable];
        }

        haveLongLinear |= enable && sym > ZBAR_COMPOSITE && sym != ZBAR_QRCODE;
    }


    ZBarImage *zbimg = [[ZBarImage alloc] initWithCGImage:img.CGImage];





    UIImage *image = screenShot;
    id <NSFastEnumeration> results = [scanner scanImage:zbimg];

    //NSLog(@"-------> %@",results);




    ZBarSymbol *sym = nil;
    for(sym in results)
        break;
    assert(sym);
    assert(image);
    if(!sym || !image)
        return;

    Barcode *barcode =
    [NSEntityDescription insertNewObjectForEntityForName: @"Barcode"
                                  inManagedObjectContext: context];
    assert(barcode);
    if(!barcode)
        return;

    assert(currentFolder);

    // wana save safari screen shot to Safari Folder
        Folder *folder = nil;
        folder = [Folder defaultFolderInContext: context];
        if(folder != currentFolder) {
            [currentFolder release];
            currentFolder = [folder retain];
        }




    barcode.folder = currentFolder;
    barcode.date = [NSDate date];
    barcode.image = image;

    barcode.symbol = sym;
    barcode.type = [NSNumber numberWithInteger: sym.type];
    barcode.data = sym.data;

    barcode.name = nil;
    barcode.thumb = nil;

    NSArray *stack = nav.viewControllers;
    BarcodeListController *_list = nil;
    if(stack.count > 1) {
        _list = [stack objectAtIndex: 1];
        if(![_list isKindOfClass: [BarcodeListController class]] ||
           _list.folder != currentFolder)
            _list = nil;
    }

    if(_list)
        [nav popToViewController: _list
                        animated: NO];
    else {
        [nav popToRootViewControllerAnimated: NO];
        _list = [[[BarcodeListController alloc]
                  initWithFolder: currentFolder]
                 autorelease];
        [nav pushViewController: _list
                       animated: NO];
    }

    UIViewController *detail =
    [[BarcodeDetailController alloc]
     initWithBarcode: barcode];
    [nav pushViewController: detail
                   animated: NO];
    [detail release];


    if(settings.autoLink)
        [self performSelector: @selector(autoLink:)
                   withObject: barcode
                   afterDelay: 0.25];

    [window.rootViewController
     adDismissModalViewController:self.safari
     animated: YES];

    [self performSelector: @selector(genThumb:)
               withObject: barcode
               afterDelay: .5];




}
Reversible answered 17/2, 2014 at 6:48 Comment(1)
i solved the problem, see answer belowReversible
R
1

Got solution:

- (NSString *)scanForQR:(UIImage *)image
{

    ZBarReaderController *imageReader = [ZBarReaderController new];

    [imageReader.scanner setSymbology: ZBAR_I25
                               config: ZBAR_CFG_ENABLE
                                   to: 0];

    id <NSFastEnumeration> results = [imageReader scanImage:image.CGImage];

    ZBarSymbol *sym = nil;
    for(sym in results) {
        break;
    } // Get only last symbol

    if (!sym) {
        NSLog(@"No barcode found");
        return nil;
    }

    NSLog(@"Found barcode! quality: %d string: %@", sym.quality, sym.data);
    return sym.data;
}
Reversible answered 24/2, 2014 at 12:50 Comment(0)
T
2

A stripped-down generic version of Baig's wonderful solution. Change ZBAR_I25 to desired code format.

- (NSString *)findQRCode:(UIImage *)inputUIImage
{

    ZBarReaderController *imageReader = [ZBarReaderController new];

    [imageReader.scanner setSymbology: ZBAR_I25
                               config: ZBAR_CFG_ENABLE
                                   to: 0];

    id <NSFastEnumeration> results = [imageReader scanImage:inputUIImage.CGImage];

    ZBarSymbol *sym = nil;
    for(sym in results) {
        break;
    } // Get only last symbol

    if (!sym) {
        NSLog(@"No barcode found");
        return nil;
    }

    NSLog(@"Found barcode! quality: %d string: %@", sym.quality, sym.data);
    return sym.data;
}
Thay answered 7/4, 2014 at 22:10 Comment(0)
R
1

Got solution:

- (NSString *)scanForQR:(UIImage *)image
{

    ZBarReaderController *imageReader = [ZBarReaderController new];

    [imageReader.scanner setSymbology: ZBAR_I25
                               config: ZBAR_CFG_ENABLE
                                   to: 0];

    id <NSFastEnumeration> results = [imageReader scanImage:image.CGImage];

    ZBarSymbol *sym = nil;
    for(sym in results) {
        break;
    } // Get only last symbol

    if (!sym) {
        NSLog(@"No barcode found");
        return nil;
    }

    NSLog(@"Found barcode! quality: %d string: %@", sym.quality, sym.data);
    return sym.data;
}
Reversible answered 24/2, 2014 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.