从 iOS 相机缓冲区转换图像。使用 AVFoundation 捕获静态图像

发布于 2024-12-06 11:39:03 字数 2057 浏览 2 评论 0原文


我正在使用 Apple 的这个众所周知的示例代码将相机缓冲区静态图像转换为 UIImages。

-(UIImage*) getUIImageFromBuffer:(CMSampleBufferRef) imageSampleBuffer{

// Get a CMSampleBuffer's Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageSampleBuffer); 

if (imageBuffer==NULL) {
    NSLog(@"No buffer");
}

// Lock the base address of the pixel buffer 
if((CVPixelBufferLockBaseAddress(imageBuffer, 0))==kCVReturnSuccess){
    NSLog(@"Buffer locked successfully");
} 

void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
NSLog(@"bytes per row %zu",bytesPerRow );
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer); 
NSLog(@"width %zu",width);

size_t height = CVPixelBufferGetHeight(imageBuffer); 
NSLog(@"height %zu",height);

// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

// Create a bitmap graphics context with the sample buffer data
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 

// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context); 

// Free up the context and color space
CGContextRelease(context); 
CGColorSpaceRelease(colorSpace);

// Create an image object from the Quartz image
UIImage *image= [UIImage imageWithCGImage:quartzImage scale:SCALE_IMAGE_RATIO orientation:UIImageOrientationRight];

// Release the Quartz image
CGImageRelease(quartzImage);

// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);


return (image );}

问题是您获得的图像通常会旋转 90°。使用方法 +imageWithCGImage:scale:orientation 我可以旋转它,但在进入此方法之前,我尝试使用 CTM 函数旋转和缩放图像,然后再将其传递给 UIImage。问题是CTM变换没有影响图像。
我问自己为什么...是因为我锁定了缓冲区吗?或者因为上下文是用内部图像创建的,所以更改只会影响进一步的 mod?
谢谢

I'm using this well known sample code from Apple to convert camera buffer still images into UIImages.

-(UIImage*) getUIImageFromBuffer:(CMSampleBufferRef) imageSampleBuffer{

// Get a CMSampleBuffer's Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageSampleBuffer); 

if (imageBuffer==NULL) {
    NSLog(@"No buffer");
}

// Lock the base address of the pixel buffer 
if((CVPixelBufferLockBaseAddress(imageBuffer, 0))==kCVReturnSuccess){
    NSLog(@"Buffer locked successfully");
} 

void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
NSLog(@"bytes per row %zu",bytesPerRow );
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer); 
NSLog(@"width %zu",width);

size_t height = CVPixelBufferGetHeight(imageBuffer); 
NSLog(@"height %zu",height);

// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

// Create a bitmap graphics context with the sample buffer data
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 

// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context); 

// Free up the context and color space
CGContextRelease(context); 
CGColorSpaceRelease(colorSpace);

// Create an image object from the Quartz image
UIImage *image= [UIImage imageWithCGImage:quartzImage scale:SCALE_IMAGE_RATIO orientation:UIImageOrientationRight];

// Release the Quartz image
CGImageRelease(quartzImage);

// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);


return (image );}

The problem is that usually the image that you obtain is 90° rotated. Using the method +imageWithCGImage:scale:orientation I'm able to rotate it, but before getting into this method I was trying to rotate and scale the image using the CTM function, before passing it to a UIImage. the problem was that CTM transformation didn't affect the image.
I'm asking myself why... is that because I'm locking the buffer? or because the context is created with the image inside, so the changes will affect only the further mod?
Thank you

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

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

发布评论

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

评论(1

尐籹人 2024-12-13 11:39:03

答案是它只影响进一步的修改,与缓冲区锁定无关。
正如您可以从这个答案中读到的那样,mod 按时间应用到上下文 CGContext 的垂直翻转

The answer is that it affects only further modifications, and it has nothing to deal with the buffer locking.
As you can read from this answer mod to the context are applied by time Vertical flip of CGContext

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