If you use vimage you can work directly on the buffer data without converting it to any image format.
outImg
contains the cropped and scaled image data. The relation between outWidth
and cropWidth
sets the scaling.
int cropX0, cropY0, cropHeight, cropWidth, outWidth, outHeight;
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer,0);
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
vImage_Buffer inBuff;
inBuff.height = cropHeight;
inBuff.width = cropWidth;
inBuff.rowBytes = bytesPerRow;
int startpos = cropY0*bytesPerRow+4*cropX0;
inBuff.data = baseAddress+startpos;
unsigned char *outImg= (unsigned char*)malloc(4*outWidth*outHeight);
vImage_Buffer outBuff = {outImg, outHeight, outWidth, 4*outWidth};
vImage_Error err = vImageScale_ARGB8888(&inBuff, &outBuff, NULL, 0);
if (err != kvImageNoError) NSLog(@" error %ld", err);
So setting cropX0 = 0
and cropY0 = 0
and cropWidth
and cropHeight
to the original size means no cropping (using the whole original image). Setting outWidth = cropWidth
and outHeight = cropHeight
results in no scaling. Note that inBuff.rowBytes
should always be the length of the full source buffer, not the cropped length.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…