2017-08-29 90 views
0

我正在開發一款應用程序,其中顯示了來自QR碼的檢測數據,但問題在於未檢測到QR碼。我已經使用此代碼:CIDetector未檢測到QR圖像

NSDictionary *detectorOptions = @{ CIDetectorAccuracy : CIDetectorAccuracyHigh }; 
       CIDetector *faceDetector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:detectorOptions]; 
       NSArray *features = [faceDetector featuresInImage:chosenImage.CIImage]; 
       CIQRCodeFeature *faceFeature; 
       for(faceFeature in features) 
       { 
        qrcodedetected = YES; 
        self.decodedstr = [NSString stringWithFormat:@"%@",faceFeature.messageString]; 
        break; 
       } 

我已經搜索了很多,但沒有成功。我使用了Apple默認窗體中的這些代碼。每次我都會得到零結果。如果有人對此有任何解決方案,請與我分享。這將不勝感激。提前致謝。

+0

爲什麼你不使用AVCaptureDevice.defaultDevice和AVCaptureSession讀取QR碼? – ObranS

+0

@ObranS:我不知道該怎麼做。你有這方面的任何代碼嗎?謝謝你的努力。 – Vishal

+0

在下面添加,希望它能幫助你 – ObranS

回答

0
// initialize   
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; 
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error]; 

// create session 
AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init]; 
dispatch_queue_t dispatchQueue = dispatch_queue_create("QRCodeQueue", NULL); 
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue]; 
[captureMetadataOutput setMetadataObjectTypes:[captureMetadataOutput availableMetadataObjectTypes]]; 
[self.captureSession addOutput:captureMetadataOutput]; 

// add camera view layer 
AVCaptureVideoPreviewLayer *captureLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession]; 
[captureLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 
[captureLayer setFrame:self.view.layer.bounds]; 
[self.view.layer addSublayer:captureLayer]; 

// delegate method 
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection { 

    // Specify the barcodes you want to read here: 
    NSArray *supportedBarcodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode]; 

    for (AVMetadataObject *barcodeMetadata in metadataObjects) { 
     for (NSString *supportedBarcode in supportedBarcodeTypes) { 
     if ([supportedBarcode isEqualToString:barcodeMetadata.type]) { 
      // get barcode object AVMetadataMachineReadableCodeObject 
      AVMetadataMachineReadableCodeObject *barcodeObject = (AVMetadataMachineReadableCodeObject *)[self.captureLayer transformedMetadataObjectForMetadataObject:barcodeMetadata]; 
      NSString *capturedBarcode = [barcodeObject stringValue]; 
      // do what you want... 
     } 
     } 
    } 
} 
+0

我在哪裏傳遞我從圖庫中獲得的圖像?謝謝你的努力。 – Vishal

+0

我從圖庫中挑選了二維碼圖片,然後嘗試解碼它的信息。 – Vishal