CoreImage的使用及常見濾鏡工具
glContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
context = [CIContext contextWithEAGLContext:glContext];
-(CVPixelBufferRef)croppedSampleBuffer:(CMSampleBufferRef)sampleBuffer targetSize:(CGSize)targetSize
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
if (shouldChangeTransform)
{
CGSize imageSize = CVImageBufferGetEncodedSize(imageBuffer);
CGSize rotatedSize ={imageSize.height,imageSize.width};
CGFloat shorterSize = (rotatedSize.width < rotatedSize.height) ? rotatedSize.width : rotatedSize.height;
rotateTransform =CGAffineTransformMakeTranslation(imageSize.width / 2.0,imageSize.height / 2.0);
rotateTransform =CGAffineTransformRotate(rotateTransform, - pi / 2.0);
rotateTransform =CGAffineTransformTranslate(rotateTransform, -imageSize.height / 2.0,-imageSize.width / 2.0);
CGFloat scale = targetSize.width / shorterSize;
scaleTransform = CGAffineTransformMakeScale(scale,scale);
CGFloat xDiff = rotatedSize.width * scale - targetSize.width;
CGFloat yDiff = rotatedSize.height * scale - targetSize.height;
cropTransform = CGAffineTransformMakeTranslation(xDiff/2.0,yDiff/2.0);
CVPixelBufferCreate(kCFAllocatorDefault, targetSize.width, targetSize.height, kCVPixelFormatType_32BGRA, nil, &resultBuffer);
shouldChangeTransform =NO;
}
CIImage *ciImage = [CIImage imageWithCVImageBuffer:imageBuffer];
CIImage *rotated = [ciImage imageByApplyingTransform:rotateTransform];
CIImage *scaled = [rotated imageByApplyingTransform:scaleTransform];
CIImage *cropped = [scaled imageByApplyingTransform:cropTransform];
[context render:cropped toCVPixelBuffer:resultBuffer];
return resultBuffer;
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者