26

我需要在应用程序中集成二维码阅读器并找到它的教程

我从这个链接下载了 Z-bar sdk 。

这是我所做的。

在 QRscannerViewController.m

-(IBAction)StartScan:(id) sender
{
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
     reader.readerDelegate = self;

     reader.readerView.torchMode = 0;

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

    resultTextView.hidden=NO;
 }

 - (void) readerControllerDidFailToRead: (ZBarReaderController*) reader
                         withRetry: (BOOL) retry{
     NSLog(@"the image picker failing to read");

 }

 - (void) imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo: (NSDictionary*) info
 {


     NSLog(@"the image picker is calling successfully %@",info);
      // ADD: get the decode results
     id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
     ZBarSymbol *symbol = nil;
     NSString *hiddenData;
      for(symbol in results)
       hiddenData=[NSString stringWithString:symbol.data];
      NSLog(@"the symbols  is the following %@",symbol.data);
      // EXAMPLE: just grab the first barcode
     //  break;

      // EXAMPLE: do something useful with the barcode data
      //resultText.text = symbol.data;
      resultTextView.text=symbol.data;


       NSLog(@"BARCODE= %@",symbol.data);

      NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
      [storeData setObject:hiddenData forKey:@"CONSUMERID"];
      NSLog(@"SYMBOL : %@",hiddenData);
      resultTextView.text=hiddenData;
     [reader dismissModalViewControllerAnimated: NO];

 }

添加了所有需要的框架,因此没有referenced from错误。

当我单击扫描按钮时,ZBarReaderViewController 显示良好,我按住 alt 键并单击鼠标左键打开模拟器的照片库,一切正常。

问题是什么,

  1. QR 图像未被扫描,即imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo 函数未被调用。
  2. QR 图像看起来比其原始尺寸大。

在此处输入图像描述

如何解决这个问题?

为什么图像没有被扫描?

4

5 回答 5

85

随着发布iOS7您不再需要使用外部框架或库。带有 AVFoundation 的 iOS 生态系统现在完全支持扫描几乎所有代码,从 QR over EAN 到 UPC。

只需查看技术说明AVFoundation 编程指南AVMetadataObjectTypeQRCode是你的朋友。

这是一个很好的教程,一步一步地展示它: iPhone QR码扫描库iOS7

只是一个关于如何设置它的小例子:

#pragma mark -
#pragma mark AVFoundationScanSetup

- (void) setupScanner
{
    self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    self.input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];

    self.session = [[AVCaptureSession alloc] init];

    self.output = [[AVCaptureMetadataOutput alloc] init];
    [self.session addOutput:self.output];
    [self.session addInput:self.input];

    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    self.output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];

    self.preview = [AVCaptureVideoPreviewLayer layerWithSession:self.session];
    self.preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.preview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

    AVCaptureConnection *con = self.preview.connection;

    con.videoOrientation = AVCaptureVideoOrientationLandscapeLeft;

    [self.view.layer insertSublayer:self.preview atIndex:0];
}
于 2013-10-14T08:33:34.970 回答
28

在我们的 iPhone 应用程序中使用 ZBar SDK 进行 BR 和 QR 码扫描。

您可以找到有关此的分步文章,以及如何使用示例代码

iPhone教程中如何使用条码扫描仪(BR和QR)(使用ZBar)

看看它怎么运作

  1. 从这里下载 ZBar SDK

  2. 在您的项目中添加以下框架

    • AVFoundation.framework
    • CoreGraphics.framework
    • CoreMedia.framework
    • CoreAudio.framework
    • CoreVideo.framework
    • QuartzCore.framework
    • libiconv.dylib
  3. 在 frameworks 中添加 zip下载的libzbar.a库

  4. 在您的班级中导入标头并确认它是委托

    #import "ZBarSDK.h"

@interface ViewController : UIViewController <ZBarReaderDelegate>

5.扫描图像

- (IBAction)startScanning:(id)sender {

    NSLog(@"Scanning..");    
    resultTextView.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];    

}

6.得到结果

- (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
    resultTextView.text = symbol.data;    

    resultImageView.image = [info objectForKey: UIImagePickerControllerOriginalImage];

    // dismiss the controller 
    [reader dismissViewControllerAnimated:YES completion:nil];
}

希望这会对您有所帮助,如果您在此示例中发现任何问题,也请告诉我,乐于助人

官方文档

于 2013-04-23T13:27:33.103 回答
9

在 iOS 7 和更新版本上试试这个。

要捕获 QR 码:

- (IBAction)Capture:(id)sender {

    isFirst=true;
    _session = [[AVCaptureSession alloc] init];
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    NSError *error = nil;

    _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];
    if (_input) {
        [_session addInput:_input];
    } else {
        NSLog(@"Error: %@", error);
    }

    _output = [[AVCaptureMetadataOutput alloc] init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    [_session addOutput:_output];

    _output.metadataObjectTypes = [_output availableMetadataObjectTypes];

    _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    _prevLayer.frame = self.view.bounds;
    _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:_prevLayer];

    [_session startRunning];
}

要阅读,请使用其委托方法:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    CGRect highlightViewRect = CGRectZero;
    AVMetadataMachineReadableCodeObject *barCodeObject;
    NSString *detectionString = nil;
    NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
            AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
            AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];

    for (AVMetadataObject *metadata in metadataObjects) {
        for (NSString *type in barCodeTypes) {
            if ([metadata.type isEqualToString:type])
            {
                barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
                highlightViewRect = barCodeObject.bounds;
                detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
                break;
            }
        }

        if (detectionString != nil)
        {
            if (isFirst) {
            isFirst=false;
            _label.text = detectionString;
            break;
           }
        }
        else
            _label.text = @"(none)";
    }

    _highlightView.frame = highlightViewRect;
}
于 2015-12-03T12:00:14.017 回答
4

首先从这里导入 ZXingWidget 库

尝试这个 ,

- (IBAction)btnScanClicked:(id)sender {

    ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];
    QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
    NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
    [qrcodeReader release];
    widController.readers = readers;
    [readers release];
    NSBundle *mainBundle = [NSBundle mainBundle];
    widController.soundToPlay =
    [NSURL fileURLWithPath:[mainBundle pathForResource:@"beep-beep" ofType:@"aiff"] isDirectory:NO];
    [self presentModalViewController:widController animated:YES];
    [widController release];


}

和代表

- (void)zxingController:(ZXingWidgetController*)controller didScanResult:(NSString *)result {

}
于 2013-04-23T10:15:20.890 回答
2

您可以使用我自己的QRCodeReader框架。

https://www.cocoacontrols.com/controls/qrcodereader

如何使用

  1. 嵌入式二进制文件
  2. 将 UIView 拖放到视图控制器中。
  3. 更改 UIVIew 的类。
  4. 绑定你的 UIView。

在您的视图控制器中粘贴“M1,M2”方法(即“ViewController.m”)

"M1" viewDidLoad


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"QR Code Reader";
    [qrCodeView setDelegate:self];
    [qrCodeView startReading];
}

这里是委托方法: “M2”QRCodeReaderDelegate


#pragma mark - QRCodeReaderDelegate
- (void)getQRCodeData:(id)qRCodeData {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"QR Code" message:qRCodeData preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancel];

    UIAlertAction *reScan = [UIAlertAction actionWithTitle:@"Rescan" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [qrCodeView startReading];
    }];
    [alertController addAction:reScan];
    [self presentViewController:alertController animated:YES completion:nil];
}

谢谢。

于 2016-10-07T10:41:25.440 回答