菜鸟教程小白 发表于 2022-12-11 18:28:32

ios - 操作 CVPixelBufferRef 高度和宽度


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

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

<pre><code> - (CVPixelBufferRef) GPUImageCreateResizedSampleBufferWithBuffer:(CVPixelBufferRef)cameraFrame withBuffer:(CGSize)finalSize withSampleBuffer:(CMSampleBufferRef)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, &amp;pixel_buffer);
    CMVideoFormatDescriptionRef videoInfo = NULL;
    CMVideoFormatDescriptionCreateForImageBuffer(NULL, pixel_buffer, &amp;videoInfo);

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

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

}
return pixel_buffer;

}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><strong>CG* - CoreGraphics 使用 CPU,对于实时视频来说太慢了,使用 CV* 和 GPU</strong> </p>

<pre><code>    // - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
    CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);

    CIImage *baseImg = ;
    CIImage *resultImg = ;
    resultImg = ;

    // created once
    // glCtx = [ initWithAPI:kEAGLRenderingAPIOpenGLES2];
    // ciContext = }];
    // ciContextColorSpace = CGColorSpaceCreateDeviceRGB();
    // CVReturn res = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, VTCompressionSessionGetPixelBufferPool(compressionSession), &amp;finishPixelBuffer);

    ;

    CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly);
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 操作 CVPixelBufferRef 高度和宽度,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41424549/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41424549/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 操作 CVPixelBufferRef 高度和宽度