3

我正在做一个 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];
}

如果我不清楚,请告诉我。提前致谢 。

4

1 回答 1

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。

于 2013-09-26T00:27:24.280 回答