Unable to scan QR code with front camera(programatically) in iPad 8th generation

I am not able to scan QR code with front camera however it is scanned with back camera, particularly in ios 14.3 and iPad generate 8, however it working well in 6th, 7th generation iPads with iOS 14.3 as a latest. I have set the AVCaptureDevice to .front for making it front camera.


I have also taken care of following things:

  • Privacy - Camera Usage Description in .plist
  • Checked for permissions: not determind, authorized, unauthorized
  • AVCaptureMetadataOutput - metadataObjectTypes = [.qr]


As it allows me to scan in other versions except ios 14.3, will it be a version specific issue? Or a device specific issue? or both?


Below is the code snippet:

import AVFoundation
class SampleScanQRCodeVC: UIViewController ,AVCaptureMetadataOutputObjectsDelegate {

var _session: AVCaptureSession?var _device: AVCaptureDevice?var _input: AVCaptureDeviceInput?var _output = AVCaptureMetadataOutput.init()
var _prevLayer: AVCaptureVideoPreviewLayer?var stillImageOutput: AVCaptureStillImageOutput?

override func viewDidLoad() {
    self.checkCameraPermission()
    self.setupAVCaptureSession()
}
//MARK: Camera persmissionfunc checkCameraPermission() {
    let authorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
    switch authorizationStatus {
    case .notDetermined:
        AVCaptureDevice.requestAccess(for: AVMediaType.video) { [weak self](granted) inif granted {
                print("access granted")
                DispatchQueue.main.async { [weak self] inprint("Access authorized")
                }
            } else {
                print("access denied")
                DispatchQueue.main.async { [weak self] in
                }
            }
        }
    case .authorized:
        print("Access authorized")
        
    case .denied, .restricted:
        print("restricted")
        
    @unknown default:
        fatalError()
    }
}
// MARK: - Front camerafunc frontCamera()-> AVCaptureDevice?{
    let arrDevices: NSArray = AVCaptureDevice.devices(for: AVMediaType.video) as Any as! NSArrayfor device in arrDevices {
        if((device as! AVCaptureDevice).position == .front){
            return device as? AVCaptureDevice
        }
    }
    return nil
}
//MARK: Setup session to capture photofunc setupSessionForPhoto(){
    _device = self.frontCamera()
    // First remove existin INPUT and OUTPUT(i.e. object of AVCaptureStillImageOutput to scan code)
    _session?.removeInput(_input!)
    _session?.removeOutput(_output)
    do{
        _input = try AVCaptureDeviceInput.init(device: _device!)
    }
    catch {
        print("error in AVCaptureDeviceInput.init")
    }
    _session?.addInput(_input!)
    //Now add other object of AVCaptureStillImageOutput to capture photo and add as outputself.stillImageOutput = AVCaptureStillImageOutput.init()
    self.stillImageOutput?.outputSettings = NSDictionary.init(object: AVVideoCodecJPEG, forKey: AVVideoCodecKey as NSCopying) as! [String : Any]
    _session?.addOutput(self.stillImageOutput!)
    _session?.startRunning()
}
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
    let _: CGRect = CGRect.zero
    _ = NSArray.init(object: AVMetadataObject.ObjectType.qr)
    NSLog("we are in metadataOutput..");
    if(metadataObjects.count > 0){
        let metadataObj = metadataObjects[0] as? AVMetadataMachineReadableCodeObjectif metadataObj?.type == AVMetadataObject.ObjectType.qr {
            NSLog("it is qr..");
            if metadataObj?.stringValue != nil {
                print("Metadata: " + (metadataObj?.stringValue!)!)
                _session?.stopRunning()
                    DispatchQueue.main.async {
                    AppDelegateInstance.appDelegate.strPersonId = (metadataObj?.stringValue!)! as NSStringself.setupSessionForPhoto()
                }
            }
        }
    }
}
}

Posted on Dec 22, 2020 12:36 AM

Reply

Similar questions

1 reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Unable to scan QR code with front camera(programatically) in iPad 8th generation

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.