我正在做一个iOS应用程序,我必须扫描条形码。我使用ZBarReaderViewController扫描条形码。有时它会准确地给出结果,但有时会给出未知的值,如& 7&#,Ac9 。我的图像是我的代码是
-(IBAction)scanBarCode:(id)sender
{
reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
reader.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height * [UIScreen mainScreen].scale >= 1136)
[reader.readerView setScanCrop:(CGRect){ { 0.25, 0.08 }, { 0.07, 0.84} }];//.25
else
[reader.readerView setScanCrop:(CGRect){ { 0.23, 0.08 }, { 0.09, 0.84} }];
ZBarImageScanner *scanner = reader.scanner;
// TODO: (optional) additional reader configuration here
// EXAMPLE: disable rarely used I2/5 to improve performance
/*[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];*/
//swetha test
[scanner setSymbology: 0
config: ZBAR_CFG_ENABLE
to: 0];
// Enable EAN 13
[scanner setSymbology: ZBAR_EAN13
config: ZBAR_CFG_ENABLE
to: 1];
// Enable UPC-A
[scanner setSymbology: ZBAR_UPCA
config: ZBAR_CFG_ENABLE
to: 1];
// Enable Code 128
[scanner setSymbology: ZBAR_CODE128
config: ZBAR_CFG_ENABLE
to: 1];
[self presentViewController:reader animated:YES completion:nil];
[reader release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
id<NSFastEnumeration> results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
// EXAMPLE: just grab the first barcode
break;
// EXAMPLE: do something useful with the barcode data
serialnumber.text = symbol.data;
// EXAMPLE: do something useful with the barcode image
//resultImage.image =
//[info objectForKey: UIImagePickerControllerOriginalImage];
// ADD: dismiss the controller (NB dismiss from the *reader*!)
barcodeClicked = 0;
[picker dismissViewControllerAnimated:YES completion:nil];
}
如果我不清楚,请告诉我。提前谢谢。
答案 0 :(得分:0)
我的imagepickerControll函数如下:
- (void) imagePickerController: (UIImagePickerController*)reader didFinishPickingMediaWithInfo: (NSDictionary*) info {
@try {
id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
for(id obj in results) {
ZBarSymbol *symbol = obj;
serialnumber.text = [symbol data];
}
}
@catch (NSException *ex) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"%@",ex]
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
barcodeClicked = 0;
[reader dismissViewControllerAnimated:YES completion:nil];
}
我强烈建议使用try-catch并浏览所有结果,因为它可以解释2个不同的条形码(如果它们在附近),并将对象解析为ZBarSymbol。