如何在iPhone相机中扫描条形码?

时间:2014-01-04 11:05:05

标签: ios iphone camera qr-code barcode-scanner

我正在尝试在iPhone应用程序中测试条形码扫描仪。我使用了以下代码

   - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    //  get the decode results
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];

    ZBarSymbol *symbol = nil;
    for(symbol in results)
        // just grab the first barcode
        break;

    // showing the result on textview
    self.resultLbl.text = symbol.data;

    self.imgViewScan.image = [info objectForKey: UIImagePickerControllerOriginalImage];

    // dismiss the controller
    [reader dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)scanBtnTapped:(id)sender {

    NSLog(@"Scanning..");
    self.resultLbl.text = @"Scanning..";
    ZBarReaderViewController *codeReader = [ZBarReaderViewController new];
    codeReader.readerDelegate=self;
    codeReader.supportedOrientationsMask = ZBarOrientationMaskAll;

    ZBarImageScanner *scanner = codeReader.scanner;
    [scanner setSymbology: ZBAR_I25 config: ZBAR_CFG_ENABLE to: 0];

    [self presentViewController:codeReader animated:YES completion:nil];  
   }

此代码无法在模拟器上运行。

2 个答案:

答案 0 :(得分:1)

如果您需要iOS 7,则不需要像ZXing这样的第三方SDK来扫描条形码。扫描常用条形码类型通过新的AVMetadataMachineReadableCodeObject内置到iOS中。有人写了一个简单的demo app来说明如何添加它。它很简单,效果很好。

答案 1 :(得分:-1)

下载条形码扫描器的代码

https://github.com/stephenkeep/barcode_scanner