当条形码就在屏幕中央时,我无法扫描条形码,相反,我必须从屏幕中心稍下方的位置捕获条形码。
当我试图在横向模式下捕获条形码时,我必须扫描屏幕中心下方的条形码。我只想从屏幕的中间扫描条形码,而不考虑屏幕的方向。
发布于 2020-07-28 08:51:40
一旦你开始在不同的方向上捕获,下面的代码就必须在主线程中调用。这段代码将帮助焦点区域位于屏幕的中心。
private var output: AVCaptureMetadataOutput?
private func configurePointOfInterests() {
guard let device = self.captureDevice else { return }
guard let videoPreviewLayer = self.videoPreviewLayer else { return }
do {
try device.lockForConfiguration()
let point = CGPoint(x: self.view.bounds.midX, y: self.view.bounds.midY)
let convPoint = videoPreviewLayer.captureDevicePointConverted(fromLayerPoint: point)
device.exposurePointOfInterest = convPoint
device.focusPointOfInterest = convPoint
device.unlockForConfiguration()
} catch {
delegate?.cameraViewController(self, didReceiveError: error)
}
}
private func configureRectOfInterest() {
if let roi = videoPreviewLayer?.metadataOutputRectConverted(fromLayerRect: self.view.bounds) {
output?.rectOfInterest = roi
}
}当您开始捕获self.captureSession.startRunning()时,如下所示在主线程中调用
DispatchQueue.main.async {
self.configurePointOfInterests()
self.configureRectOfInterest()
}https://stackoverflow.com/questions/63113752
复制相似问题