2012-05-02 75 views
3

我正在使用以下方式将来自相机的图像数据转换为UIImage。为了节省一些时间和内存,我希望在将图像数据转换为UIImage之前裁剪图像数据。裁剪像素缓冲区

理想情况下,我传递一个cropRect,并找回裁剪的UIImage。但是,由于相机输出的尺寸可能会根据我是否使用照片或视频预设而有所不同,因此我可能不知道用于cropRect的尺寸。我可以使用类似于焦点或曝光点的cropRect,它使用(0,0)和(1,1)之间的CGPoint,并且对cropRect的CGSize类似地做。或者我可以在调用以下代码之前获取sampleBuffer的维度,并传入适当的cropRect。我想就我应该使用的一些建议。

我也想知道如何最好的裁剪,以便不必创建一个完整的UIImage,然后裁剪回来。通常情况下,我只想保持大约10-20%的像素。我假设我必须迭代像素,并开始将cropRect复制到不同的像素缓冲区中,直到我拥有所有我想要的像素。

并记住根据orientation有可能发生旋转。

+ (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer orientation:(UIImageOrientation) orientation 
{ 
    // Create a UIImage from sample buffer data 
    // Get a CMSampleBuffer's Core Video image buffer for the media data 
    CVImageBufferRef 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 
    void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

    // Get the number of bytes per row for the pixel buffer 
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
    // Get the pixel buffer width and height 
    size_t width = CVPixelBufferGetWidth(imageBuffer); 
    size_t height = CVPixelBufferGetHeight(imageBuffer); 

    // Create a device-dependent RGB color space 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    // Create a bitmap graphics context with the sample buffer data 
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
               bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
    // Create a Quartz image from the pixel data in the bitmap graphics context 
    CGImageRef quartzImage = CGBitmapContextCreateImage(context); 
    // Unlock the pixel buffer 
    CVPixelBufferUnlockBaseAddress(imageBuffer,0); 

    // Free up the context and color space 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 

    // Create an image object from the Quartz image 
    UIImage *image = [UIImage imageWithCGImage:quartzImage scale:(CGFloat)1.0 orientation:orientation]; 
    // Release the Quartz image 
    CGImageRelease(quartzImage); 

    return (image); 
} 

总结:

  1. 我应该传递一个cropRect指定之间的矩形(0,0,0,0)和(1,1,1,1)或我传在cropRect中指定像(50,50,100,100)一样的确切像素位置?
  2. 如何最好地裁剪像素缓冲区?

回答

2

我认为你应该使用像素作为cropRect,因为你必须至少在某些时候将浮点值转换为像素值。 下面的代码没有经过测试,但应该给你这个想法。

CGRect cropRect = CGRectMake(50, 50, 100, 100); // cropRect 
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
CVReturn lock = CVPixelBufferLockBaseAddress(pixelBuffer, 0); 
if (lock == kCVReturnSuccess) { 
    int w = 0; 
    int h = 0; 
    int r = 0; 
    int bytesPerPixel = 0; 
    unsigned char *buffer; 
    w = CVPixelBufferGetWidth(pixelBuffer); 
    h = CVPixelBufferGetHeight(pixelBuffer); 
    r = CVPixelBufferGetBytesPerRow(pixelBuffer); 
    bytesPerPixel = r/w; 
    buffer = CVPixelBufferGetBaseAddress(pixelBuffer); 
    UIGraphicsBeginImageContext(cropRect.size); // create context for image storage, use cropRect as size 
    CGContextRef c = UIGraphicsGetCurrentContext(); 
    unsigned char* data = CGBitmapContextGetData(c); 
    if (data != NULL) { 
     // iterate over the pixels in cropRect 
     for(int y = cropRect.origin.y, yDest = 0; y<CGRectGetMaxY(cropRect); y++, yDest++) { 
      for(int x = cropRect.origin.x, xDest = 0; x<CGRectGetMaxX(cropRect); x++, xDest++) { 
       int offset = bytesPerPixel*((w*y)+x); // offset calculation in cropRect 
       int offsetDest = bytesPerPixel*((cropRect.size.width*yDest)+xDest); // offset calculation for destination image 
       for (int i = 0; i<bytesPerPixel; i++) { 
        data[offsetDest+i] = buffer[offset+i]; 
       } 
      } 
     } 
    } 
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 
+0

关闭,我想。看起来它每次读取的数据少一点,所以我得到了对角带。今晚晚些时候我会调试它。我必须注意的事情:图像来自相机,因此它们都是正确的风景(右转90度,与我的视图的x,y一致)。此外,图像很大,所以上面使用的cropRect很小 - 我需要将裁切矩形从视图的尺寸转换为图像的尺寸。我会在这里发布任何更正。 – mahboudz

+0

我在正确的轨道上。每行有12个字节的填充。行被填充到32的倍数?下面一行更正了图像,但我的坐标仍然关闭(但这是一个简单的翻译,我认为):'\t int offset = bytesPerPixel *(((w + 12)* y)+ x); // cropRect'中的偏移量计算更好的解决方法是:'int offset = bytesPerPixel * x + r * y; //在cropRect中的偏移量计算 目标pixbuff是否应该32字节对齐? – mahboudz