Added to support different barcodes 支持二维码和条形码
let supportedBarCodes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeUPCECode, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeAztecCode]
override func viewDidLoad() {
super.viewDidLoad()
//关于这个属性 请看我的另一篇 UI----影响View Frame的因素
self.edgesForExtendedLayout = UIRectEdge.init(rawValue: 0)
// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
// as the media type parameter.
let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
do {
// Get an instance of the AVCaptureDeviceInput class using the previous device object.
let input = try AVCaptureDeviceInput(device: captureDevice)
// Initialize the captureSession object.
captureSession = AVCaptureSession()
// Set the input device on the capture session.
captureSession?.addInput(input)
// captureDevice?.formats
// Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetadataOutput)
captureSession?.sessionPreset = AVCaptureSessionPresetHigh
// Set delegate and use the default dispatch queue to execute the call back
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
captureSession?.sessionPreset = AVCaptureSessionPresetHigh
// Detect all the supported bar code
captureMetadataOutput.metadataObjectTypes = supportedBarCodes
// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = self.view.bounds
videoContainer.layer.addSublayer(videoPreviewLayer!)
// Start video capture
captureSession?.startRunning()
划重点 这是设置扫描区域的范围
【metadataOutputRectOfInterest】【rectOfInterest 】
let width = UIScreen.main.bounds.width - 40
let O_y = (UIScreen.main.bounds.height - width-40) / 2
let rect = CGRect.init(x: 20, y: O_y, width: width, height: width)
let innerrect = videoPreviewLayer?.metadataOutputRectOfInterest(for: rect)
captureMetadataOutput.rectOfInterest = innerrect!
// 这些输出可以帮你检查设置是否有误 方便理解 调试
// print("===sp===inner\(innerrect)")
// let rect1 = videoPreviewLayer?.rectForMetadataOutputRect(ofInterest: innerrect!)
// print("====sp===\(rect1)")
给它画个框
思路就是先画个遮罩,然后选中一个区域,然后取反,会用ps就特别好理解
let maskView = UIView.init(frame: self.view.bounds);
maskView.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.4);
self.view.addSubview(maskView)
let maskPath = UIBezierPath.init(rect: self.view.bounds)
maskPath.append(UIBezierPath.init(roundedRect: rect, cornerRadius: 10).reversing())
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.cgPath;
maskView.layer.mask = maskLayer;
} catch {
// If any error occurs, simply print it out and don't continue any more.
print(error)
return
}
}
这是一个函数 ,所以想拿来直接用的朋友可以从上到下代码部分直接复制黏贴就好了。
网友评论