Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

objective c - Scaning static UIImage using ios ZBar SDK

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];




}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...