我正在实现裁剪视频帧的自定义视频合成器。目前,我使用Core Graphics来做到这一点:
-(void)renderImage:(CGImageRef)image inBuffer:(CVPixelBufferRef)destination {
CGRect cropRect = // some rect ...
CGImageRef currentRectImage = CGImageCreateWithImageInRect(photoFullImage, cropRect);
size_t width = CVPixelBufferGetWidth(destination);
size_t height = CVPixelBufferGetHeight(destination);
CGContextRef context = CGBitmapContextCreate(CVPixelBufferGetBaseAddress(destination), // data
width,
height,
8, // bpp
CVPixelBufferGetBytesPerRow(destination),
CGImageGetColorSpace(backImage),
CGImageGetBitmapInfo(backImage));
CGRect frame = CGRectMake(0, 0, width, height);
CGContextDrawImage(context, frame, currentRectImage);
CGContextRelease(context);
}
我如何使用Metal API来做到这一点?它应该快得多,对吗?
使用Accelerate Framework(特别是vImage)怎么样?会更简单吗?
最佳答案
好的,我不知道这对您是否有用,但仍然如此。
查看以下代码:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
id<MTLTexture> textureY = nil;
{
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
MTLPixelFormat pixelFormat = MTLPixelFormatBGRA8Unorm;
CVMetalTextureRef texture = NULL;
CVReturn status = CVMetalTextureCacheCreateTextureFromImage(NULL, _textureCache, pixelBuffer, NULL, pixelFormat, width, height, 0, &texture);
if(status == kCVReturnSuccess)
{
textureY = CVMetalTextureGetTexture(texture);
if (self.delegate){
[self.delegate textureUpdated:textureY];
}
CFRelease(texture);
}
}
}
我使用此代码将
CVPixelBufferRef
转换为MTLTexture。之后,您可能应该创建blitCommandEncoder
并使用它func copyFromTexture(sourceTexture: MTLTexture, sourceSlice: Int, sourceLevel: Int, sourceOrigin: MTLOrigin, sourceSize: MTLSize, toTexture destinationTexture: MTLTexture, destinationSlice: Int, destinationLevel: Int, destinationOrigin: MTLOrigin)
在其中,您可以选择裁剪的矩形并将其复制到其他纹理。
下一步是将生成的
MTLTextures
转换回CVPixelBufferRefs
,然后从中制作视频,很遗憾,我不知道该怎么做。真的很想听听您的想法。干杯。
关于ios - 如何使用Metal API或Accelerate Framework绘制裁剪后的位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812814/