[英]300x300 pixel Png image to Byte Array
My Objective is to extract 300x300 pixel frame from a CVImageBuffer (camera stream) and convert it in to a UInt Byte Array.我的目标是从 CVImageBuffer(相机流)中提取 300x300 像素帧并将其转换为 UInt 字节数组。 Technically the array size should be 90,000.
从技术上讲,数组大小应为 90,000。 However I'm getting a much more larger value.
但是,我获得了更大的价值。 Any help would much appreciate to spot the mistake.
任何帮助都会非常感谢发现错误。
Method that convert the Image buffer to UIImage将图像缓冲区转换为 UIImage 的方法
func getImageFromSampleBuffer(image_buffer : CVImageBuffer?) -> UIImage?
{
if let imageBuffer = image_buffer {
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, CVPixelBufferLockFlags.readOnly);
// 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.byteOrder32Little.rawValue
bitmapInfo |= CGImageAlphaInfo.premultipliedFirst.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
//let bitmapInfo: UInt32 = CGBitmapInfo.alphaInfoMask.rawValue
let context = CGContext.init(data: baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo)
// Create a Quartz image from the pixel data in the bitmap graphics context
let quartzImage = context?.makeImage();
// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer, CVPixelBufferLockFlags.readOnly);
// Create an image object from the Quartz image
let image = UIImage.init(cgImage: quartzImage!);
return (image);
}
return nil
}
}
Extension that convert the image dat in to UInt8 Array将图像数据转换为 UInt8 数组的扩展
extension Data {
func toByteArray() -> [UInt8]? {
var byteData = [UInt8](repeating:0, count: self.count)
self.copyBytes(to: &byteData, count: self.count)
return byteData
}
}
Usage of the Code守则的使用
let image = getImageFromSampleBuffer(image_buffer: imageBuffer)
//Issue*******
if let byteArrayOfImage = image.copy(newSize: CGSize(width: 300, height: 300))?.pngData()?.toByteArray(){
print(byteArrayOfImage.count) // Technically should print 90,000. However it prints a large value
}
What am i missing here我在这里错过了什么
Your expectation Technically the array size should be 90,000
may be wrong.您的期望
Technically the array size should be 90,000
可能是错误的。
If your CVPixelBuffer
's format is BRGA or other 4-channel type, then the array size will be w*h*4=360,000
, where w*4
or, more general, w*channel_count
is the row stride
.如果您的
CVPixelBuffer
的格式是 BRGA 或其他 4 通道类型,则数组大小将为w*h*4=360,000
,其中w*4
或更一般的w*channel_count
是row stride
。
CVPixelBuffer
is usually aligned by 16 bytes: https://developer.apple.com/documentation/corevideo/kcvpixelbufferbytesperrowalignmentkey?language=objc So the real row stride
may be even more than w*channel_count
. CVPixelBuffer
通常按 16 字节对齐: https : //developer.apple.com/documentation/corevideo/kcvpixelbufferbytesperrowalignmentkey? CVPixelBuffer
所以真正的row stride
可能比w*channel_count
还要大。
You should check the value bytesPerRow
to understand your whole problem.您应该检查值
bytesPerRow
以了解您的整个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.