更改AVSession方向以进行条形码扫描

时间:2019-03-21 08:57:09

标签: ios swift barcode-scanner avcapturesession avcapturedevice

这是我设置AVCaptureSession进行条形码扫描的方式。

    captureSession = AVCaptureSession()
    guard let dev = AVCaptureDevice.default(for: .video) else { return }
    guard let session = captureSession else { return }

    do {
        let input = try AVCaptureDeviceInput.init(device: dev)
        let output = AVCaptureMetadataOutput()

        if session.canAddInput(input) {
            self.captureSession?.addInput(input)
        }

        if session.canAddOutput(output) {
            session.addOutput(output)
            output.setMetadataObjectsDelegate(self, queue: DispatchQueue.init(label: "scan"))
            output.metadataObjectTypes = self.objectTypes
        }
    } catch {

    }

    let prevLayer = AVCaptureVideoPreviewLayer.init(session: captureSession!)
    prevLayer.frame = self.view.frame
    prevLayer.videoGravity = .resizeAspectFill

    cameraPreview.layer.addSublayer(prevLayer)
    self.captureSession?.startRunning()

问题在于某些代码仅在横向模式中被检测到。我知道可以更改预览层的方向,但是如何更改扫描到纵向的方向?

0 个答案:

没有答案