gpt4 book ai didi

ios - 在 CGContext 中旋转从 Samplebuffer 捕获的帧

转载 作者:行者123 更新时间:2023-11-28 11:00:17 25 4
gpt4 key购买 nike

我正在使用前置摄像头,当我捕捉帧时,它们会逆时针旋转 90 度。我想在 CGContext 中旋转捕获的图像(因为我读到 Core Graphic 比其他框架快得多)。

这是我的代码:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))

let width = CVPixelBufferGetWidthOfPlane(imageBuffer!, 0)
let height = CVPixelBufferGetHeightOfPlane(imageBuffer!, 0)

let bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer!, 0)

let lumaBuffer = CVPixelBufferGetBaseAddressOfPlane(imageBuffer!, 0)

let grayColorSpace = CGColorSpaceCreateDeviceGray()

let context = CGContext(data: lumaBuffer,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: bytesPerRow,
space: grayColorSpace,
bitmapInfo: CGImageAlphaInfo.none.rawValue);

let transform1 = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
context!.concatenate(transform1)

//context!.rotate(by: CGFloat(M_PI_2)) // also not working!
let dstImage = context!.makeImage()

detect(image: dstImage!)
}

最佳答案

经过大量搜索和阅读文章,我在 AVCaptureConnection 类中找到了我的解决方案!我只需要请求连接在捕获函数中使用以下示例代码在缓冲区内旋转帧!!

if connection.isVideoOrientationSupported
{
connection.videoOrientation = .portrait
}
if connection.isVideoMirroringSupported
{
connection.isVideoMirrored = true
}

就是这样!

关于ios - 在 CGContext 中旋转从 Samplebuffer 捕获的帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40918526/

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