2013-04-23 73 views
23

我需要將QR代碼閱讀器集成到應用程序中,並找到tutorialQR代碼在iOS應用程序中掃描

我從此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顯示效果不錯,我按住Alt鍵並左鍵單擊鼠標打開模擬器的照片庫並且一切正常。

的問題是什麼,

  1. 的QR圖像沒有得到掃描,即imagePickerController: (UIImagePickerController*) reader didFinishPickingMediaWithInfo 功能不會被調用。
  2. QR圖像顯示大於其原始大小。

enter image description here

如何解決這個問題?

爲什麼圖像沒有被掃描?

+1

qr應完全可見以供閱讀。由於二維碼的圖像較大,圖書館無法掃描,所有四個角落都應該在圖像中。 – 2013-04-23 10:17:34

+0

@SharonNathaniel,如何縮小這個尺寸? – NAZIK 2013-04-23 10:21:22

+1

我建議在設備上將它指向紙張或屏幕上的qr代碼。由於您從模擬器中的照片庫中提取圖像,因此我不確定您可以在將其提供給讀取器之前對其進行處理。我建議在設備上試用它。 – 2013-04-23 10:23:50

回答

27

使用zbar和SDK在我們的iPhone應用程序BR和QR碼掃描。

你可以找到步步爲這一步的文章,怎麼做示例代碼以及

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

看看它是如何工作

  1. 下載zbar和SDK從here

  2. 在您的項目中添加下面的框架

    • AVFoundation.framework
    • CoreGraphics.framework
    • CoreMedia.framework
    • CoreAudio.framework
    • CoreVideo.framework
    • QuartzCore。框架
    • libiconv.dylib
  3. 添加庫的框架,在您的類下載libzbar.a拉鍊的

  4. 進口頭和確認的委託

    #進口「 ZBarSDK.h「

@interface ViewController : UIViewController <ZBarReaderDelegate> 

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

} 

6.get結果在

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

+1

+1,偉大的教程,它將在模擬器上工作,或者我必須在設備上測試它 – NAZIK 2013-04-23 13:48:40

+0

@NAZIK我很高興親愛的,順便說一句,我已經測試過它只在蘋果設備和它的工作正常,但不知道有關模擬器,你可以給我評論,如果你有任何麻煩,我可以解釋任何我知道的。 – swiftBoy 2013-04-24 04:02:07

+2

它完美的設備,保持偉大的工作frnd – NAZIK 2013-05-02 05:54:48

4

FIrst import ZXingWidget library from here

試試這個,

- (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 { 

} 
+0

哪個sdk必須使用zxing?在哪裏下載? – NAZIK 2013-04-23 10:18:37

+0

https://github.com/zxing/zxing/tree/master/iphone – 2013-04-23 10:19:10

77

與發佈iOS7一樣,您不再需要使用外部框架或庫。 帶AVFoundation的iOS生態系統現在完全支持掃描從EAN到UPC的QR幾乎所有代碼。

只需看看Tech NoteAVFoundation programming guideAVMetadataObjectTypeQRCode是你的朋友。

這裏是一個很好的教程這說明它一步一步: 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]; 
} 
+4

這需要成爲新的答案! SO的答案可以改變嗎? – tambykojak 2014-07-02 13:07:29

+2

不要忘記在'AVCaptureSession'上調用'-startRunning'。請參閱[文檔](https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureSession_Class/index.html)。 – Pang 2015-05-10 10:58:04

+0

鏈接到域名www.ama-dev.com在答案是死的。 – Pang 2017-09-19 09:12:51

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

閱讀,使用它的委託方法:

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

我的qrcodes被多次掃描如何糾正這個問題??當我嘗試掃描一次,實際上我有一個警報視圖後獲得的價值,然後它移動到另一個頁面,有時它得到價值4倍和4個警報視圖來4次segues發生 – Akshay 2017-09-08 03:57:59

1

你可以用我自己的框架QRCodeReader

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

如何使用

  1. 嵌入式二進制
  2. 將在您的視圖控制器下降的UIView。
  3. 改變UIVIw的類別。
  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]; 
} 

謝謝。

+0

我使用xcode 7.3.1和swift 2,哪一個pod我安裝它給我在運行時的lib文件中的錯誤。任何人都可以建議我應該使用哪個pod/lib? – Mubashar 2017-07-25 05:31:11