ios应用程序中的QR码扫描

Azi*_*lah 24 iphone qr-code objective-c ios

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

我从这个链接下载了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];

 }
Run Code Online (Sandbox Code Playgroud)

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

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

问题是什么,

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

在此输入图像描述

怎么解决这个?

为什么图像不被扫描?

Ale*_*der 82

与发布时一样,iOS7您不再需要使用外部框架或库.拥有AVFoundation的iOS生态系统现在完全支持扫描从EAN到UPC的QR中的几乎所有代码.

只需看看Tech NoteAVFoundation编程指南.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];
}
Run Code Online (Sandbox Code Playgroud)

  • 这需要成为新的答案!SO的答案可以改变吗? (5认同)
  • 不要忘记在`AVCaptureSession`上调用`-startRunning`.请参阅[此处的文档](https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureSession_Class/index.html). (2认同)

swi*_*Boy 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. 在框架中添加库下载的zip的libzbar.a

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

    #import"ZBarSDK.h"

@interface ViewController : UIViewController <ZBarReaderDelegate>
Run Code Online (Sandbox Code Playgroud)

5.scan图像

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

}
Run Code Online (Sandbox Code Playgroud)

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];
}
Run Code Online (Sandbox Code Playgroud)

希望这会对你有所帮助,如果你在这个例子中发现任何麻烦也请告诉我,乐意帮忙

官方文件

  • 它在设备中完美运行,保持良好的工作 (2认同)
  • @Ramdhan Choudhary我已经从http://sourceforge.net/projects/zbar/files/iPhoneSDK/beta/ZBarSDK-1.3.1.dmg/download下载新的ZBar SDK解决了这个问题,并用您的软件包替换并工作.还要感谢详细的答案.让我做+1 ... (2认同)

SM1*_*M18 6

在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];
}
Run Code Online (Sandbox Code Playgroud)

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)