[iOS 视频流开发-获得视频帧处理]

news/2024/6/3 18:55:19 标签: 移动开发, ffmpeg

 

调用视频流所使用框架:<Foundation/Foundation.h>

必须定义的参数:

1.AVCaptureDevice(捕获设备:前置、后置摄像头等)

2.AVCaptureInput(捕获输入:一般就是捕获设备的输入)

3.AVCaptureOutput(捕获输出:可输入为视频文件、图像文件等)

4.AVCaptureSession(调节多个输入输出)

 

关键代码:

- (void)setupCamera
{
    NSError *error = nil;
    
    // Create the session
    _session = [[AVCaptureSession alloc] init];
    
    // Configure the session to produce lower resolution video frames, if your
    // processing algorithm can cope. We'll specify medium quality for the
    // chosen device.
    _session.sessionPreset = AVCaptureSessionPresetMedium;
    
    // Find a suitable AVCaptureDevice
    AVCaptureDevice *device = [AVCaptureDevice
                               defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // Create a device input with the device and add it to the session.
    _input = [AVCaptureDeviceInput deviceInputWithDevice:device
                                                                        error:&error];
    if (!_input) {
        // Handling the error appropriately.
    }
    [_session addInput:_input];
    
    // Create a VideoDataOutput and add it to the session
    _output = [[AVCaptureVideoDataOutput alloc] init];
    [_session addOutput:_output];
    
    // Configure your output.
    dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);
    [_output setSampleBufferDelegate:self queue:queue];
    
    // Specify the pixel format
    _output.videoSettings =
    [NSDictionary dictionaryWithObject:
     [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]
                                forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    
    
    // If you wish to cap the frame rate to a known value, such as 15 fps, set
    // minFrameDuration.
    _output.minFrameDuration = CMTimeMake(1, 15);
    
    // Start the session running to start the flow of data
    [_session startRunning];
    
    // Assign session to an ivar.
    [self setSession:_session];
}


// Delegate routine that is called when a sample buffer was written
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
       fromConnection:(AVCaptureConnection *)connection
{
    // Create a UIImage from the sample buffer data
    UIImage *img = [self imageFromSampleBuffer:sampleBuffer];

    /*
    dispatch_async(dispatch_get_main_queue(), ^{
        self.catchview.image=img;
    });
    */
    
}
// Create a UIImage from sample buffer data
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
    
    // Get a CMSampleBuffer's Core Video image buffer for the media data
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    // Lock the base address of the pixel buffer
    CVPixelBufferLockBaseAddress(imageBuffer, 0);
    
    // Get the number of bytes per row for the pixel buffer
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    // Get the pixel buffer width and height
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    
    
    // Get the number of bytes per row for the pixel buffer
    u_int8_t *baseAddress = (u_int8_t *)malloc(bytesPerRow*height);
    memcpy( baseAddress, CVPixelBufferGetBaseAddress(imageBuffer), bytesPerRow * height     );
    
    // size_t bufferSize = CVPixelBufferGetDataSize(imageBuffer);
    
    // Create a device-dependent RGB color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    // Create a bitmap graphics context with the sample buffer data
    
    //The context draws into a bitmap which is `width'
    //  pixels wide and `height' pixels high. The number of components for each
    //      pixel is specified by `space'
    CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
                                                 bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipFirst);
    
    // Create a Quartz image from the pixel data in the bitmap graphics context
    CGImageRef quartzImage = CGBitmapContextCreateImage(context);
    
    // Unlock the pixel buffer
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    
    // Free up the context and color space
    CGContextRelease(context);
    //CGColorSpaceRelease(colorSpace);
    
    // Create an image object from the Quartz image
    UIImage *image = [UIImage imageWithCGImage:quartzImage scale:1.0 orientation:UIImageOrientationRight];
    free(baseAddress);
    // Release the Quartz image
    CGImageRelease(quartzImage);
    return (image);
}

 

转载于:https://www.cnblogs.com/rayshen/p/4399363.html


http://www.niftyadmin.cn/n/1062225.html

相关文章

matlab中散点图的线性拟合_机器学习之线性回归(python)

1、概念线性回归&#xff08;一元线性回归&#xff09;二变量数据&#xff1a;对于每一个观察结果&#xff0c;二变量数据给出两个变量数值&#xff0c;其中一个变量收到某种方式控制&#xff0c;或是用来解释另一个变量&#xff0c;则这个变量叫做自变量或者解释变量&#xff…

代理模式——举例说明Java23种设计模式中的代理模式

代理模式——举例说明Java23种设计模式中的代理模式1. 概述1.1 定义1.2 组成部分2. 静态代理2.1 引出代理模式的一个简单业务场景2.2 静态代理例子1&#xff1a;2.3 静态代理例子2&#xff1a;3. 动态代理3.1 JDK代理&#xff08;接口代理&#xff09;3.1.1 JDK动态代理例子13.…

java -jar xxx.jar

之前用MyEclipse做了一个可执行jar&#xff0c;点击就可运行的。 今天突然不好用了&#xff0c;错误是&#xff1a; could not find the main class C:\123\abc.jar.Program will exit. 不知怎么回事&#xff0c;查看java的环境变量没啥问题。网上的建议是重装JDK&#xff0c;没…

Spring MVC-集成(Integration)-生成Excel示例(转载实践)

以下内容翻译自&#xff1a;https://www.tutorialspoint.com/springmvc/springmvc_excel.htm 说明&#xff1a;示例基于Spring MVC 4.1.6。 以下示例显示如何使用Spring Web MVC框架生成Excel。首先&#xff0c;让我们使用Eclipse IDE&#xff0c;并按照以下步骤使用Spring Web…

深入理解Java虚拟机——堆内存的结构分析

深入理解Java虚拟机——堆内存的结构分析1. 先简单介绍几个常用的 jvm 参数1.1 设置堆空间大小的参数1.1.1 -Xms &#xff08;-XX:InitalHeapSize&#xff09;1.1.2 -Xmx &#xff08;-XX:MaxHeapSize&#xff09;1.1.3 -Xmn、-XX:NewSize1.1.4 -XX:MaxNewSize1.1.5 -XX&#x…

Oracle查找重复数据

Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)转载于:https://www.cnblogs.com/handsomer/p/4409725.html

javascript飞机大战-----002游戏引擎

基本html布局 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Document</title><style>*{margin:0;padding:0;}.game{position:relative;width: 320px;height: 568px;margin: 50px auto;b…

深入理解Java虚拟机——关于方法区的变化 + JVM简单内存变化图

深入理解Java虚拟机——关于方法区的变化1. 前言1.1 运行时数据区1.2 关于永久代和方法区的“简”说法2. 方法区&#xff08;Method Area&#xff09;2.1 方法区简介2.2 方法区异常3. 永久代 元空间3.1 永久代&#xff08;Permanent Generation&#xff09;3.2 元空间&#xf…