gpt4 book ai didi

swift - 相机输入的尺寸为一像素一像素

转载 作者:行者123 更新时间:2023-11-30 12:25:48 25 4
gpt4 key购买 nike

这是一个相当奇怪的请求,但我希望构建一个具有实时摄像头占据整个屏幕的应用程序。但是,它不会显示正常分辨率,而是全部为一种颜色。特别是,我想采用通常是屏幕上中间像素的颜色,并使其占据整个屏幕。它需要实时快速地完成。

我尝试创建一个函数,将 capturesession 保存为 uiimage,然后从中获取像素数据,但是,事实证明它实时速度很慢。有什么建议吗?

最佳答案

假设您有 AVCaptureSession 设置。您需要设置 AVCaptureVideoDataOutput,然后设置其示例缓冲区委托(delegate)。委托(delegate)类应该覆盖 func captureOutput(AVCaptureOutput!, CMSampleBuffer!, AVCaptureConnection!) 。在此函数中,您可以访问像素缓冲区以对中心点进行采样。你可以按照下面的方法来做。我已将中心点的实际采样留给了您。

class MyClass : NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {

func addVideoOutput() {
// Add video data output.
if session.canAddOutput(videoDataOutput)
{
videoDataOutput.setSampleBufferDelegate(self, queue: sessionQueue)
videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as NSString:Int(kCVPixelFormatType_32BGRA)]

videoDataOutput.alwaysDiscardsLateVideoFrames = true
session.addOutput(videoDataOutput)
}
}

// AVCaptureVideoDataOutputSampleBufferDelegate
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
if let buffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
process(pixelBuffer: buffer)
}
}
func process(pixelBuffer: CVPixelBuffer) {
let sourceRowBytes = CVPixelBufferGetBytesPerRow( pixelBuffer );
let width = CVPixelBufferGetWidth( pixelBuffer );
let height = CVPixelBufferGetHeight( pixelBuffer );

let rt = CVPixelBufferLockBaseAddress( pixelBuffer, .readOnly );
if (rt == kCVReturnSuccess) {
...
Do your processing of the pixeldata here
...
CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
}
}

private let session = AVCaptureSession()
private let sessionQueue = DispatchQueue(label: "session queue", attributes: [], target: nil) // Communicate with the session
private let videoDataOutput = AVCaptureVideoDataOutput()
}

关于swift - 相机输入的尺寸为一像素一像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44231055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com