旋转从视频帧获取的 CGImage

发布于 2024-10-05 15:36:27 字数 1164 浏览 4 评论 0原文

这是 Apple 的代码(来自技术问答 QA1702),用于从视频缓冲区获取 UIImage。不幸的是,返回的图像旋转了 90 度。如何编辑它以使返回的图像方向正确?

- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer 
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    CVPixelBufferLockBaseAddress(imageBuffer, 0); 

    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
                                                 bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 

    CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace);

    UIImage *image = [UIImage imageWithCGImage:quartzImage];

    CGImageRelease(quartzImage);

    return (image);
}

This is Apple's code (from Technical Q&A QA1702) for getting a UIImage from a video buffer. Unfortunately, the image returned is rotated 90 degrees. How do I edit this so that the image returned is correctly oriented?

- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer 
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
    CVPixelBufferLockBaseAddress(imageBuffer, 0); 

    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
                                                 bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 

    CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);

    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace);

    UIImage *image = [UIImage imageWithCGImage:quartzImage];

    CGImageRelease(quartzImage);

    return (image);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉梦枕江山 2024-10-12 15:36:27

取决于您使用的是前置摄像头还是后置摄像头

int frontCameraImageOrientation = UIImageOrientationLeftMirrored;
int backCameraImageOrientation = UIImageOrientationRight;

UIImage *image = [[UIImage alloc] initWithCGImage:newImage scale:(CGFloat)1.0 orientation:frontCameraImageOrientation];

Depends on whether you are using the front camera or the back camera

int frontCameraImageOrientation = UIImageOrientationLeftMirrored;
int backCameraImageOrientation = UIImageOrientationRight;

UIImage *image = [[UIImage alloc] initWithCGImage:newImage scale:(CGFloat)1.0 orientation:frontCameraImageOrientation];
月野兔 2024-10-12 15:36:27

您可以更改 videoOrientation,这样您就可以获得正确的图像

connection.videoOrientation = AVCaptureVideoOrientationPortrait

,因为默认情况下视频方向不是纵向。

You can change videoOrientation, this way you get a correct image

connection.videoOrientation = AVCaptureVideoOrientationPortrait

Because by default Video orientation is not portrait.

萌酱 2024-10-12 15:36:27

只需旋转上下文

只需以弧度旋转上下文。此调用会将上下文旋转 90 度。

CGFloat degrees = 90.f;
CGFloat radians = degrees * (M_PI / 180.f);
CGContextRotateCTM(context, radians);

您可以轻松地设置此方法以采用所需的方向并相应地旋转。

Just rotate the context

Simply rotate the context in radians. This call would rotate the context 90 degrees.

CGFloat degrees = 90.f;
CGFloat radians = degrees * (M_PI / 180.f);
CGContextRotateCTM(context, radians);

You can easily set this method to take the desired orientation and rotate accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文