简体   繁体   English

300x300 像素 Png 图像到字节数组

[英]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可能是错误的。

  1. 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_countrow stride

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

相关问题 迅捷:加载300张图片的数组 - swift: loading array with 300 images iOS上空间对象的Nutiteq问题,初始化异常-sqlite3_auto_extension EXEC_BAD_ACCESS地址0x300 - Nutiteq issue with spatialite on iOS, init exception - sqlite3_auto_extension EXEC_BAD_ACCESS address 0x300 CloudKit查询操作仅返回300个结果 - CloudKit Query Operation only returns 300 results IOS可以复制超过300MB的本地文件吗? - Can IOS copy local file over 300MB? Swift 裁剪 UIImage 占用超过 300 MB Memory - Swift cropped UIImage take more than 300 MB Memory 从像素阵列生成图像(快速) - Generate Image from Pixel Array (fast) 在640x852像素的NSImageView中显示500x500像素的图像,而没有任何种类或模糊(在OS X上为Swift) - Displaying a 500x500 pixel image in a 640x852 pixel NSImageView without any kind or blurriness (Swift on OS X) 如何获取图像上给定点(x,y)的灰度像素值? - How to get the grayscale pixel value for a given point (x,y) on an image? ARKit – 使用 SCN 文件 > 300 MB 时出现“意外发现 nil” - ARKit – Getting “Unexpectedly found nil” when using SCN file > 300 MB 使用Swift从数据数组逐像素创建图像 - Creating image on a pixel-by-pixel basis from data-array with Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM