Ram G.
Ram G.

Reputation: 3125

Using Google ML Kit and Front Camera for PDF 417 bar code scanning on iOS

I am using Google ML Kit and iPad Air 4 for bar code scanning. For PDF417 bar code back camera works fine. I am not able scan PDF417 using front camera. Is there any settings/code I need to do for front camera?

- (void)captureOutput:(AVCaptureOutput *)output
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
           fromConnection:(AVCaptureConnection *)connection {
  CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  if (imageBuffer) {
    // Evaluate `self.currentDetector` once to ensure consistency throughout this method since it
    // can be concurrently modified from the main thread.
    Detector activeDetector = self.currentDetector;
    //[self resetManagedLifecycleDetectorsForActiveDetector:activeDetector];

       
    _lastFrame = sampleBuffer;
    MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer];
    UIImageOrientation orientation = [UIUtilities
        imageOrientationFromDevicePosition:_isUsingFrontCamera ? AVCaptureDevicePositionFront
                                                               : AVCaptureDevicePositionBack];

    visionImage.orientation = orientation;
    CGFloat imageWidth = CVPixelBufferGetWidth(imageBuffer);
    CGFloat imageHeight = CVPixelBufferGetHeight(imageBuffer);
    BOOL shouldEnableClassification = NO;
    BOOL shouldEnableMultipleObjects = NO;



    switch (activeDetector) {
      case DetectorOnDeviceBarcode: {
        MLKBarcodeScannerOptions *options = [[MLKBarcodeScannerOptions alloc] init];
        [self scanBarcodesOnDeviceInImage:visionImage
                                    width:imageWidth
                                   height:imageHeight
                                  options:options];
        break;
      }
      
    }
  } else {
    if (kTrace) NSLog(@"%@", @"Failed to get image buffer from sample buffer.");
  }
}

Upvotes: 0

Views: 620

Answers (1)

Dong Chen
Dong Chen

Reputation: 882

Yeah, you need some logic to handle the switch between the front and back cameras. You can check out ML Kit's vision quickstart sample app to see how that's handled (in particular, see all the logic dealing with isUsingFrontCamera in this file).

Upvotes: 1

Related Questions