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. 我应该传入一个指定(0,0,0,0)和(1,1,1,1)之间的矩形的cropRect,还是传入一个指定精确像素位置(50,50,100,100)的cropRect?
  2. 如何最好地裁剪像素缓冲区?
4

1 回答 1

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();
}
于 2012-05-02T21:06:33.450 回答