在Objective C中使用相机扫描QR Code或BarCode

时间:2012-07-25 08:39:44

标签: objective-c qr-code barcode-scanner

我想构建一个应用来扫描QRCode和条形码。我想使用相机扫描图像包含代码(QRCode或BarCode)但不拍照片。现在我不知道这样做。
任何人,请提供一些参考!

3 个答案:

答案 0 :(得分:5)

你应该看看:http://zbar.sourceforge.net/

答案 1 :(得分:1)

下载ZBarSDK并将其导入pch文件,然后使用此代码

// BarCodeView.h
@interface BarCodeView : UIViewController  < ZBarReaderDelegate > {
UIImageView *resultImage;
UITextView *resultText;
  }
@property (nonatomic, retain) IBOutlet UIImageView *resultImage;
@property (nonatomic, retain) IBOutlet UITextView *resultText;
- (IBAction) scanButtonTapped;

    // BarCodeView.m
  @synthesize resultImage, resultText;
   - (IBAction) scanButtonTapped
 {
NSLog(@"TBD: scan barcode here...");
// ADD: present a barcode reader that scans from the camera feed
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;

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

// present and release the controller
[self presentModalViewController: reader
                        animated: YES];
[reader release];
  }
 - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
  {
// ADD: get the decode results
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
resultText.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*!)
[reader dismissModalViewControllerAnimated: YES];
 }

答案 2 :(得分:1)

两个最活跃的项目是ZBarZXing

你没有提到你是针对iOS还是OS X.我不相信ZBar支持OS X.ZXing确实如此。我相信ZBar比基于C ++的ZXing端口更能支持1D代码。

(FWIW,我是ZXing的C ++ / OS X / iOS端口的主动贡献者。)