首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >放大NSData或CMSampleBufferRef

放大NSData或CMSampleBufferRef
EN

Stack Overflow用户
提问于 2013-02-26 11:58:42
回答 1查看 502关注 0票数 0

我正在用最新的SDK开发一个iOS应用程序。

这个应用程序将与OpenCV一起工作,我必须在相机上进行缩放,但是这个,它在iOS SDK上是不可用的,所以我想用编程的方式来实现它。

我必须对每一个视频帧进行“缩放”。这是我必须做的事情:

代码语言:javascript
运行
复制
#pragma mark - AVCaptureSession delegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

    /*Lock the image buffer*/
    CVPixelBufferLockBaseAddress(imageBuffer,0);

    /*Get information about the image*/
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    //size_t stride = CVPixelBufferGetBytesPerRow(imageBuffer);

    //put buffer in open cv, no memory copied
    cv::Mat image = cv::Mat(height, width, CV_8UC4, baseAddress);
    // copy the image
    //cv::Mat copied_image = image.clone();
    _lastFrame = [NSData dataWithBytes:image.data
                                  length:image.elemSize() * image.total()];

    [DataExchanger postFrame];

    /*We unlock the  image buffer*/
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
}

NSData 您知道如何在或上放大

EN

回答 1

Stack Overflow用户

发布于 2013-02-28 13:11:37

一种方法是将你的图片放入一个CGImageRef中,在该图片中选择一个正方形,然后再将它画成一个正常大小。像这样的东西(虽然可能有更好的方法):

代码语言:javascript
运行
复制
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // Create a bitmap graphics context with the sample buffer data
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
                                                 bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    // Create a Quartz image from the pixel data in the bitmap graphics context
    CGImageRef quartzImage = CGBitmapContextCreateImage(context);
    CGContextRelease(context);


    CGImageRef smallQuartzImage = CGImageCreateWithImageInRect(quartzImage, CGRectMake(200, 200, 600, 600));

    cv::Mat image(height, width, CV_8UC4 );
    CGContextRef contextRef = CGBitmapContextCreate( image.data, width, height, 8, cvMat.step[0], colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst );
    CGContextDrawImage(contextRef, CGRectMake(0, 0, width, height), smallQuartzImage);
    CGContextRelease( contextRef );
    CGColorSpaceRelease( colorSpace );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15088561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档