我正在使用以下方法将相机的图像数据转换为UIImage。为了节省时间和内存,我想在将其转换为UIImage之前裁剪图像数据。
理想情况下,我传入一个cropRect,并得到一个裁剪后的UIImage。但是,由于相机输出的大小可能基于我是否使用照片或视频预设而不同,因此我可能不知道要用于cropRect的尺寸。我可以使用类似于焦点或曝光点的cropRect,它使用介于(0,0)和(1,1)之间的CGPoint,并对cropRect的CGSize执行类似操作。或者,在调用以下内容之前获取sampleBuffer的尺寸,并传入适当的cropRect。我想知道应该使用哪种方法。
我还想知道如何最好地进行裁剪,以避免创建整个UIImage,然后将其裁剪回来。通常,我只关心保留约10-20%的像素。我假设我必须迭代像素,并开始将cropRect复制到不同的像素缓冲区中,直到获得所有所需的像素。
请注意,根据方向可能会发生旋转。
理想情况下,我传入一个cropRect,并得到一个裁剪后的UIImage。但是,由于相机输出的大小可能基于我是否使用照片或视频预设而不同,因此我可能不知道要用于cropRect的尺寸。我可以使用类似于焦点或曝光点的cropRect,它使用介于(0,0)和(1,1)之间的CGPoint,并对cropRect的CGSize执行类似操作。或者,在调用以下内容之前获取sampleBuffer的尺寸,并传入适当的cropRect。我想知道应该使用哪种方法。
我还想知道如何最好地进行裁剪,以避免创建整个UIImage,然后将其裁剪回来。通常,我只关心保留约10-20%的像素。我假设我必须迭代像素,并开始将cropRect复制到不同的像素缓冲区中,直到获得所有所需的像素。
请注意,根据方向可能会发生旋转。
+ (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);
}
总之:
- 我应该传递一个指定在(0,0,0,0)和(1,1,1,1)之间的矩形的cropRect,还是传递一个指定像素位置如(50,50,100,100)的cropRect?
- 最好怎样裁剪像素缓冲区?
int offset = bytesPerPixel * (((w + 12) * y) + x); // 在cropRect中进行偏移量计算
更好的修复方法是:int offset = bytesPerPixel * x + r * y; // 在cropRect中进行偏移量计算
目标pixbuff也应该是32字节对齐的吗? - mahboudz