我想使用Zbar sdk扫描静态UIImage,它会给出错误,即 ZBarSymbol是Null
我也检查了
ZBarReaderController:UIImagePickerController
但由于它是UIImagePickerController的子类,我根本不想使用UIImagePickerController,因为我只想扫描我在我的应用程序中引用的UIImage ..我搜索了这个问题,但没有得到任何帮助。
我也试过ZBarImageScanner,
- (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];
}
答案 0 :(得分:2)
Baig的精彩解决方案的精简版。将ZBAR_I25更改为所需的代码格式。
- (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;
}
答案 1 :(得分:1)
得到解决方案:
- (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;
}