gpt4 book ai didi

ios - 如何将 BGRA 图像保存到 iphone,然后将其带到笔记本电脑进行后期处理

转载 作者:可可西里 更新时间:2023-11-01 02:17:10 30 4
gpt4 key购买 nike

我想用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保存到相册,然后用airdrop把图片文件传到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);
}

最佳答案

我找到一个解决办法:把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")

关于ios - 如何将 BGRA 图像保存到 iphone,然后将其带到笔记本电脑进行后期处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465751/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com