• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 操作 CVPixelBufferRef 高度和宽度

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:28:32 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 GPUImage 库函数来操作 CVPixelbuffer 的高度和宽度。我正在纵向录制视频,当用户旋转设备时,我的屏幕会自行调整为横向模式。我希望横向框架适合屏幕。

例如:- 我以 320x568 的纵向模式开始视频,当我将设备转为横向时,我的帧是 568x320,我想适应 320x568。为了调整这个东西,我想操纵 CVPixelBuffer。但这会占用大量内存,最后我的应用程序崩溃了。

 - (CVPixelBufferRef) GPUImageCreateResizedSampleBufferWithBufferCVPixelBufferRef)cameraFrame withBufferCGSize)finalSize withSampleBufferCMSampleBufferRef)sampleBuffer
{
   CVPixelBufferRef pixel_buffer = NULL;

// CVPixelBufferCreateWithPlanarBytes for YUV input
@autoreleasepool {

    CGSize originalSize = CGSizeMake(CVPixelBufferGetWidth(cameraFrame), CVPixelBufferGetHeight(cameraFrame));

    CVPixelBufferLockBaseAddress(cameraFrame, 0);
    GLubyte *sourceImageBytes = (GLubyte *)CVPixelBufferGetBaseAddress(cameraFrame);
    CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, sourceImageBytes, CVPixelBufferGetBytesPerRow(cameraFrame) * originalSize.height, NULL);
    CGColorSpaceRef genericRGBColorspace = CGColorSpaceCreateDeviceRGB();
    CGImageRef cgImageFromBytes = CGImageCreate((int)originalSize.width, (int)originalSize.height, 8, 32, CVPixelBufferGetBytesPerRow(cameraFrame), genericRGBColorspace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst, dataProvider, NULL, NO, kCGRenderingIntentDefault);

    GLubyte *imageData = (GLubyte *) calloc(1, ((int)finalSize.width * (int)finalSize.height * 4));


    CGContextRef imageContext = CGBitmapContextCreate(imageData, (int)finalSize.width, (int)finalSize.height, 8, (int)finalSize.width * 4, genericRGBColorspace,  kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

    CGRect scaledRect = AVMakeRectWithAspectRatioInsideRect(originalSize, CGRectMake(0, 0, finalSize.width, finalSize.height));

    CGContextDrawImage(imageContext, scaledRect, cgImageFromBytes);
    CGImageRelease(cgImageFromBytes);
    CGContextRelease(imageContext);
    CGColorSpaceRelease(genericRGBColorspace);
    CGDataProviderRelease(dataProvider);

    CVPixelBufferCreateWithBytes(kCFAllocatorDefault, finalSize.width, finalSize.height, kCVPixelFormatType_32BGRA, imageData, finalSize.width * 4, stillImageDataReleaseCallback, NULL, NULL, &pixel_buffer);
    CMVideoFormatDescriptionRef videoInfo = NULL;
    CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixel_buffer, &videoInfo);

    CMTime frameTime = CMTimeMake(1, 30);
    CMSampleTimingInfo timing = {frameTime, frameTime, kCMTimeInvalid};

    CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixel_buffer, YES, NULL, NULL, videoInfo, &timing, &sampleBuffer);
    CVPixelBufferUnlockBaseAddress(cameraFrame, 0);
    CFRelease(videoInfo);
    //   CVPixelBufferRelease(pixel_buffer);

}
return pixel_buffer;

}



Best Answer-推荐答案


CG* - CoreGraphics 使用 CPU,对于实时视频来说太慢了,使用 CV* 和 GPU

    // - (void)captureOutputAVCaptureOutput *)captureOutput didOutputSampleBufferCMSampleBufferRef)sampleBuffer fromConnectionAVCaptureConnection *)connection
    CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);

    CIImage *baseImg = [CIImage imageWithCVPixelBuffer:pixelBuffer];
    CIImage *resultImg = [baseImg imageByCroppingToRectutputFrameCropRect];
    resultImg = [resultImg imageByApplyingTransformutputFrameTransform];

    // created once
    // glCtx = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    // ciContext = [CIContext contextWithEAGLContext:glCtx options{kCIContextWorkingColorSpace:[NSNull null]}];
    // ciContextColorSpace = CGColorSpaceCreateDeviceRGB();
    // CVReturn res = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, VTCompressionSessionGetPixelBufferPool(compressionSession), &finishPixelBuffer);

    [ciContext render:resultImg toCVPixelBuffer:finishPixelBuffer bounds:resultImg.extent colorSpace:ciContextColorSpace];

    CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);

关于ios - 操作 CVPixelBufferRef 高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41424549/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap