如何将CVPixelBuffer/CVImageBuffer转换为数据? [英] How do I convert a CVPixelBuffer / CVImageBuffer to Data?

查看:432
本文介绍了如何将CVPixelBuffer/CVImageBuffer转换为数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的相机应用捕获照片,以某种方式对其进行增强并保存.

My camera app captures a photo, enhances it in a certain way, and saves it.

为此,我以CVPixelBuffer(包装在CMSampleBuffer中)的形式从摄像机获取了输入图像.我对像素缓冲区进行了一些修改,然后将其转换为Data对象.我该怎么做?

To do so, I get the input image from the camera in the form of a CVPixelBuffer (wrapped in a CMSampleBuffer). I perform some modifications on the pixel buffer, and I then want to convert it to a Data object. How do I do this?

请注意,我不想将像素缓冲区/图像缓冲区转换为UIImage或CGImage,因为它们没有元数据(如EXIF).我需要一个数据对象.如何从CVPixelBuffer/CVImageBuffer中获得一个?

Note that I don't want to convert the pixel buffer / image buffer to a UIImage or CGImage since those don't have metadata (like EXIF). I need a Data object. How do I get one from a CVPixelBuffer / CVImageBuffer?

PS:我尝试调用 AVCapturePhotoOutput.jpegPhotoDataRepresentation(),但是失败了,提示不是JPEG样本缓冲区".这很有意义,因为CMSampleBuffer包含像素缓冲区(位图),而不是JPEG.

PS: I tried calling AVCapturePhotoOutput.jpegPhotoDataRepresentation() but that fails saying "Not a JPEG sample buffer". Which makes sense since the CMSampleBuffer contains a pixel buffer (a bitmap), not a JPEG.

推荐答案

正如您所说的,您可以获取 CMSampleBuffer ,然后可以使用

As you said that you are able to get the CMSampleBuffer, then you can get it using

NSData *myata = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:<your_cmsample_buffer_obj‌​>];

这篇关于如何将CVPixelBuffer/CVImageBuffer转换为数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆