使用AVFoundation进行前置摄像头扫描

时间:2019-05-15 14:29:28

标签: swift

我需要使用前置摄像头像BackCamera一样扫描QRCode。我需要在哪里更改?

我正在使用QRCoder第三方https://github.com/sbhklr/QRCoder,并且已经根据我的要求自定义了代码

用于前置摄像头

let captureDevice : AVCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: .front)

使用默认的后置摄像头

captureDevice = AVCaptureDevice.default(for: AVMediaType.video)

UIView中的LoadCamera

videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoPreviewLayer.captureDevicePointConverted(fromLayerPoint: CGPoint(x: 0.5, y: 0.5))
        videoPreviewLayer.frame = self.previewView.bounds

    let preset = AVCaptureSession.Preset.photo

    if(captureSession.canSetSessionPreset(preset))
    {
        captureSession.sessionPreset = preset

    }

    highlightView.autoresizingMask = [.flexibleTopMargin, .flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin]
    highlightView.frame  = CGRect(x: self.view.frame.midX - 225, y: self.view.frame.midY - 225, width: 450, height: 450)
    view.addSubview(highlightView)

    do
    {
        deviceInput = try AVCaptureDeviceInput(device: captureDevice!)

    } catch let error as NSError
    {
        didFailWithError(error: error)
        return
    }

    if let captureInput = deviceInput
    {
        captureSession.addInput(captureInput)
    }
    else
    {
        return
    }

    captureSession.addOutput(metadataOutput)
    metadataOutput.setMetadataObjectsDelegate(self, queue: metadataObjectsQueue)
    metadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.qr]

QRCode在后置摄像头中扫描完美,但在前置摄像头中根本无法正常工作...我已经为CaptureDevice实施了Focus模式,但尝试了很多,但对我来说没有用?任何解决方案。...

0 个答案:

没有答案