首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在CGContext中旋转来自Samplebuffer的捕获帧

在CGContext中旋转来自Samplebuffer的捕获帧
EN

Stack Overflow用户
提问于 2016-12-02 03:08:01
回答 2查看 2.1K关注 0票数 0

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

下面是我的代码:

代码语言:javascript
复制
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!)
}
EN

回答 2

Stack Overflow用户

发布于 2016-12-05 23:48:35

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

代码语言:javascript
复制
if connection.isVideoOrientationSupported
{
     connection.videoOrientation = .portrait
}
     if connection.isVideoMirroringSupported
{
     connection.isVideoMirrored = true
}

就这样!

票数 1
EN

Stack Overflow用户

发布于 2018-05-31 17:29:34

如果您使用的是方法:

代码语言:javascript
复制
- (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
   fromConnection:(AVCaptureConnection*)connection

您可以更改缓冲区的方向以获得正确的图像,下面是我制作的一个示例:

代码语言:javascript
复制
UIInterfaceOrientation iOrientation = [UIApplication sharedApplication].statusBarOrientation;

if(iOrientation == UIInterfaceOrientationLandscapeLeft){
    [connection setVideoOrientation:AVCaptureVideoOrientationLandscapeLeft];
}
else{
    [connection setVideoOrientation:AVCaptureVideoOrientationLandscapeRight];
}

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40918526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档