我有這段代碼,它是 AVCaptureMetadataOutputObjectsDelegate 的擴展:
internal func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
guard let captureSession = captureSession else { return }
captureSession.stopRunning()
if let metadataObject = metadataObjects.first {
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
guard let stringValue = readableObject.stringValue else { return }
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
found(code: stringValue)
}
}
并在它“查看”二維碼時被呼叫:
let metadataOutput = AVCaptureMetadataOutput()
if (captureSession.canAddOutput(metadataOutput)) {
captureSession.addOutput(metadataOutput)
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
metadataOutput.metadataObjectTypes = [.qr]
我想做的是增加一個新的功能,就是一打開攝像頭,就知道后置攝像頭的亮度是多少。
我發現他們到處都使用這個:
func captureOutput(_ output: AVCaptureOutput,
didOutput sampleBuffer: CMSampleBuffer,
from connection: AVCaptureConnection)
但在我看來,它不再在 AvFoundation 中了。
uj5u.com熱心網友回復:
我假設您提到的相機的“亮度”是某種光度指標。我知道幾種測量它的方法。
我想,你的代碼中一定已經定義了一個 videoDevice: let videoDevice: AVCaptureDevice。如果您不單獨存盤,請從videoInput.videoDevice.
檢查
videoDevice.iso。值越低 - 照明條件越亮。這是一個 KVO 屬性,因此您可以實時觀察它的變化。檢查
videoDevice.exposureDuration。相同:值越低→光照條件越亮。曝光持續時間基本上是 iOS 系統相機為獲得更好的夜間模式拍攝而調整的時間。正如您所提到的,您還可以從相機中獲取實時像素緩沖區進行分析。喜歡建立直方圖并將亮像素與暗像素進行比較等。
在您的相機課程中:
/// You already have the session
private let session = AVCaptureSession()
/// Define a video output (probably you did that already,
/// otherwise how would your camera scan QRs at all)
private let videoOutput = AVCaptureVideoDataOutput()
/// Define a queue for sample buffer
private let videoSampleBufferQueue = DispatchQueue(label: "videoSampleBufferQueue")
然后將輸出添加到會話:
if session.canAddOutput(videoOutput){
session.addOutput(videoOutput)
}
videoOutput.setSampleBufferDelegate(self, queue: videoSampleBufferQueue)
并實施AVCaptureVideoDataOutputSampleBufferDelegate:
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
// Handle the pixelBuffer the way you like
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/409128.html
標籤:
