如何将 BGRA 图像保存到 iphone,然后将其带到笔记本电脑上进行后期处理
Posted
技术标签:
【中文标题】如何将 BGRA 图像保存到 iphone,然后将其带到笔记本电脑上进行后期处理【英文标题】:How do I save a BGRA image to iphone and then take it to a laptop for post-processing 【发布时间】:2016-04-07 02:37:51 【问题描述】:我想用iphone的摄像头拍摄原始图像数据,然后在笔记本电脑中使用matlab或opencv对其进行后期处理。所以我需要将这些原始图像数据从 iphone 带到笔记本电脑。
目前,我使用 AVFoudnation 获取 32BGRA 图像(它是原始图像数据,对吗?)。我将它保存为文件,但我无法从 iphone 中获取它。 (有没有不越狱的解决方案?)
以下是一些相关代码:
if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo)
videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: (sampleBuffer, error) in
if (sampleBuffer != nil)
// get the raw image data
let cameraFrame = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(cameraFrame!, 0)
let rawImage = CVPixelBufferGetBaseAddress(cameraFrame!)
let bytesPerRow = CVPixelBufferGetBytesPerRow(cameraFrame!)
let rawImageData = NSData.init(bytes: rawImage, length: bytesPerRow*CVPixelBufferGetHeight(cameraFrame!))
// process the rawImageData, we now save it
var paths = NSSearchPathForDirectoriesInDomains (.DocumentDirectory, .UserDomainMask, true);
let documentsDirectory = paths[0]
let fileName = documentsDirectory.stringByAppendingString("/rawImageData")
do
try rawImageData.writeToFile(fileName, options: NSDataWritingOptions.DataWritingFileProtectionComplete)
catch
print("Raw image data write successfully.")
print("Path of raw image data: ", fileName)
CVPixelBufferUnlockBaseAddress(cameraFrame!, 0)
)
我是否需要将BGRA数据保存到相册然后取出,如果是,我该怎么做(使用UIImage?)
谢谢。
更新:
我将BGRA图像转换为UIImage并使用UIImageWriteToSavedPhotosAlbum将其保存到相册,然后我使用空投将图像文件带到may mac。但是,我发现图像是JPEG格式。怎么了? 以下是代码:
if (sampleBuffer != nil)
let rawImage:UIImage = self.imageFromSampleBuffer(sampleBuffer)
self.capturedImage.image = rawImage
UIImageWriteToSavedPhotosAlbum(rawImage, nil, nil, nil)
print("Raw Image write to photo album successfully")
// Create a UIImage from sample buffer data
func imageFromSampleBuffer(sampleBuffer : CMSampleBufferRef) -> UIImage
// Get a CMSampleBuffer's Core Video image buffer for the media data
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer!, 0);
// Get the number of bytes per row for the pixel buffer
let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer!);
// Get the number of bytes per row for the pixel buffer
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!);
// Get the pixel buffer width and height
let width = CVPixelBufferGetWidth(imageBuffer!);
let height = CVPixelBufferGetHeight(imageBuffer!);
// Create a device-dependent RGB color space
let colorSpace = CGColorSpaceCreateDeviceRGB();
// Create a bitmap graphics context with the sample buffer data
var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue
bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue & CGBitmapInfo.AlphaInfoMask.rawValue
let context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, bitmapInfo);
// Create a Quartz image from the pixel data in the bitmap graphics context
let quartzImage = CGBitmapContextCreateImage(context);
// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer!,0);
// Create an image object from the Quartz image
let image = UIImage.init(CGImage: quartzImage!);
return (image);
【问题讨论】:
你想在 MATLAB 中使用什么格式? MATLAB 不能导入任何类型的图像吗? Matlab无法导入BGRA图像,我可以将原始数据保存为iphone中的bmp并取出到笔记本电脑吗? 【参考方案1】:我找到了一个解决方案:将 BGRA 转换为 UIImage,再转换为 PNG,然后将其保存到相册中。由于PNG是一种无损压缩图像格式,这意味着我也得到了bmp图像。
let rawImage:UIImage = self.imageFromSampleBuffer(sampleBuffer)
let img = UIImagePNGRepresentation(rawImage)
let pngImg = UIImage.init(data: img!)
self.capturedImage.image = pngImg
UIImageWriteToSavedPhotosAlbum(pngImg!, nil, nil, nil)
print("PNG Image write to photo album successfully")
【讨论】:
以上是关于如何将 BGRA 图像保存到 iphone,然后将其带到笔记本电脑上进行后期处理的主要内容,如果未能解决你的问题,请参考以下文章
Swift Metal 将 bgra8Unorm 纹理保存到 PNG 文件