How do I convert a CVPixelBuffer / CVImageBuffer to Data?

南笙酒味 提交于 2021-02-10 15:55:12

问题


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

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?

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: 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.


回答1:


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‌​>];


来源:https://stackoverflow.com/questions/45038919/how-do-i-convert-a-cvpixelbuffer-cvimagebuffer-to-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!