ios应用程序中的QR码扫描

时间:2013-04-23 10:09:34

标签: iphone ios objective-c qr-code

我需要在应用中集成QR码阅读器,并为其找到tutorial

我从这个link下载了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显示良好,我按住a​​lt键并左键单击鼠标以打开模拟器的照片库,一切正常。

问题是什么,

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

    如何解决这个问题?

    为什么不扫描图像?

5 个答案:

答案 0 :(得分:81)

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

只需查看Tech NoteAVFoundation programming guide即可。 AVMetadataObjectTypeQRCode是你的朋友。

这是一个很好的教程,它逐步显示: iPhone QR code scan library 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];
}

答案 1 :(得分:27)

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

你可以找到一步一步的文章,以及如何处理示例代码

  

How to use Barcode Scanner (BR and QR) in iPhone Tutorial (using ZBar)

了解它是如何运作的

  1. here

  2. 下载ZBar SDK
  3. 在项目中添加以下框架

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

  5. 在您的课程中导入标题并确认其是委托

    #import“ZBarSDK.h”

  6. @interface ViewController : UIViewController <ZBarReaderDelegate>
    

    5.scan image

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

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

    Official Docs

答案 2 :(得分:6)

在iOS 7及更高版本上试试这个。

捕捉二维码:

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

答案 3 :(得分:4)

here首先导入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 {

}

答案 4 :(得分:1)

您可以将自己的框架用于 QRCodeReader

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

如何使用

  1. 嵌入二进制文件
  2. 将UIView拖放到视图控制器中。
  3. 改变UIVIew的类别。
  4. 绑定你的UIView。
  5. 在视图控制器中粘贴“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];
    }
    

    感谢。

相关问题