zoukankan      html  css  js  c++  java
  • 将CMSampleBufferRef保存为图片

    //转换图片
    - (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer {
        // 为媒体数据设置一个CMSampleBuffer的Core Video图像缓存对象
        CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        // 锁定pixel buffer的基地址
        CVPixelBufferLockBaseAddress(imageBuffer, 0);
        
        // 得到pixel buffer的基地址
        void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
        
        // 得到pixel buffer的行字节数
        size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
        // 得到pixel buffer的宽和高
        size_t width = CVPixelBufferGetWidth(imageBuffer);
        size_t height = CVPixelBufferGetHeight(imageBuffer);
        
        // 创建一个依赖于设备的RGB颜色空间
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        
        // 用抽样缓存的数据创建一个位图格式的图形上下文(graphics context)对象
        CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
                                                     bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
        // 根据这个位图context中的像素数据创建一个Quartz image对象
        CGImageRef quartzImage = CGBitmapContextCreateImage(context);
        // 解锁pixel buffer
        CVPixelBufferUnlockBaseAddress(imageBuffer,0);
        
        // 释放context和颜色空间
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        
        // 用Quartz image创建一个UIImage对象image
        UIImage *image = [UIImage imageWithCGImage:quartzImage];
        
        // 释放Quartz image对象
        CGImageRelease(quartzImage);
        
        return (image);
    }

    保存图片

    //保存图片
    - (void)saveImageToPhotos:(UIImage*)savedImage
    {
        UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    }
    // 指定回调方法
    - (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
    {
        NSString *msg = nil ;
        if(error != NULL){
            msg = @"保存图片失败" ;
        }else{
            msg = @"保存图片成功" ;
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
                                                        message:msg
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    
    }

    //调用示例               

    UIImage *img=[self imageFromSampleBuffer:detectSampleBufferRef];

    [self saveImageToPhotos:img];

  • 相关阅读:
    【整理】互联网服务端技术体系:存储基础之数据存储与索引结构
    python基础之enumerate() 函数+ Counter类计数器
    介绍几个好用的Java库
    【GIS】BlenderGIS应用配置之一
    使任意同步库快速变asyncio异步语法的方式 ,run_in_executor
    mysql查看表行数
    mysql8.0使用mysqldump报错:Unknown table ‘column_statistics‘ in information_schema (1109)
    sysbench安装使用,结果信息分析
    【1.6】shell使用 -p -f 之类的参数
    右键新建项中添加Typora新建Markdown文件快捷选项
  • 原文地址:https://www.cnblogs.com/Percy/p/6291301.html
Copyright © 2011-2022 走看看