降低AVCaptureVideoPreviewLayer的亮度(rectOfInterest区域除外)

时间:2019-08-06 04:36:19

标签: swift

我正在开发QR扫描器的应用程序。这是我到目前为止所做的:

enter image description here

这是我创建中心框的代码:

轮廓是一个UIView

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer.frame = view.layer.bounds
        previewLayer.videoGravity = .resizeAspectFill
        view.layer.addSublayer(previewLayer)

        view.addSubview(outline)
        let outlineHeight = view.frame.size.width * 0.7
        outline.translatesAutoresizingMaskIntoConstraints = false
        outline.widthAnchor.constraint(equalToConstant: outlineHeight).isActive = true
        outline.heightAnchor.constraint(equalToConstant: outlineHeight).isActive = true
        outline.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        outline.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        outline.layer.borderWidth = 1.0
        outline.layer.borderColor = UIColor.white.cgColor

        view.addSubview(cornerImage)
        cornerImage.translatesAutoresizingMaskIntoConstraints = false
        cornerImage.leadingAnchor.constraint(equalTo: outline.leadingAnchor, constant: 10).isActive = true
        cornerImage.trailingAnchor.constraint(equalTo: outline.trailingAnchor, constant: -10).isActive = true
        cornerImage.topAnchor.constraint(equalTo: outline.topAnchor, constant: 10).isActive = true
        cornerImage.bottomAnchor.constraint(equalTo: outline.bottomAnchor, constant: -10).isActive = true
        cornerImage.image = UIImage(named: "cornerQR")

        view.addSubview(instructionLabel)
        instructionLabel.translatesAutoresizingMaskIntoConstraints = false
        instructionLabel.topAnchor.constraint(equalTo: cornerImage.bottomAnchor, constant: 30).isActive = true
        instructionLabel.widthAnchor.constraint(equalToConstant: outlineHeight).isActive = true
        instructionLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        instructionLabel.font = UIFont.systemFont(ofSize: 16)
        instructionLabel.textColor = .white
        instructionLabel.numberOfLines = 0
        instructionLabel.textAlignment = .center
        instructionLabel.text = "Di chuyển camera để nhìn rõ mã QR và xác nhận thanh toán"

        let rectY = view.bounds.size.height/2 - outlineHeight/2
        let rectX = view.bounds.size.width/2 - outlineHeight/2

        let outlineRect =  CGRect(x: rectX, y: rectY, width: outlineHeight  , height: outlineHeight)
        metadataOutput.rectOfInterest = previewLayer.metadataOutputRectConverted(fromLayerRect: outlineRect)
        captureSession.startRunning()

现在我不想将UIview外部的背景亮度设置得更暗,如何实现?

0 个答案:

没有答案