您现在的位置是: 首页 > 热门轿车 热门轿车

image quartz

2024-10-31 15:17:15 40人已围观

简介image quartz   最近有些日子没和大家见面了,今天我想和大家聊一聊“image quartz”的话题。如果你对这个领域还比较陌生,那么这篇文章就是为你而写的,让我们一起来探索其中的奥秘吧。1.如何在获取onCameraFrame得到的帧的部分内容2.avfoundation怎么获取帧数据

image quartz

       最近有些日子没和大家见面了,今天我想和大家聊一聊“image quartz”的话题。如果你对这个领域还比较陌生,那么这篇文章就是为你而写的,让我们一起来探索其中的奥秘吧。

1.如何在获取onCameraFrame得到的帧的部分内容

2.avfoundation怎么获取帧数据

如何在获取onCameraFrame得到的帧的部分内容

       #import <AVFoundation/AVFoundation.h>

       // 创建并配置一个捕获会话并且启用它

       - (void)setupCaptureSession

       {

        NSError *error = nil;

        // 创建session

        AVCaptureSession *session = [[AVCaptureSession alloc] init];

        // 可以配置session以产生解析度较低的视频帧,如果你的处理算法能够应付(这种低解析度)。

        // 我们将选择的设备指定为中等质量。

        session.sessionPreset = AVCaptureSessionPresetMedium;

        // 找到一个合适的AVCaptureDevice

        AVCaptureDevice *device = [AVCaptureDevice

        defaultDeviceWithMediaType:AVMediaTypeVideo];

        // 用device对象创建一个设备对象input,并将其添加到session

        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device

        error:&error];

        if (!input) {

        // 处理相应的错误

        }

        [session addInput:input];

        // 创建一个VideoDataOutput对象,将其添加到session

        AVCaptureVideoDataOutput *output = [[[AVCaptureVideoDataOutput alloc] init] autorelease];

        [session addOutput:output];

        // 配置output对象

        dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);

        [output setSampleBufferDelegate:self queue:queue];

        dispatch_release(queue);

        // 指定像素格式

        output.videoSettings =

        [NSDictionary dictionaryWithObject:

        [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]

        forKey:(id)kCVPixelBufferPixelFormatTypeKey];

       // 如果你想将视频的帧数指定一个顶值, 例如15ps

        // 可以设置minFrameDuration(该属性在iOS 5.0中弃用)

        output.minFrameDuration = CMTimeMake(1, 15);

        // 启动session以启动数据流

        [session startRunning];

        // 将session附给实例变量

        [self setSession:session];

       }

       // 抽样缓存写入时所调用的委托程序

       - (void)captureOutput:(AVCaptureOutput *)captureOutput

        didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer

        fromConnection:(AVCaptureConnection *)connection

       {

        // 通过抽样缓存数据创建一个UIImage对象

        UIImage *image = [self imageFromSampleBuffer:sampleBuffer];

        < 此处添加使用该image对象的代码 >

       }

       // 通过抽样缓存数据创建一个UIImage对象

       - (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);

       }

avfoundation怎么获取帧数据

       解决方法:

       #pragma mark Convert SampleBuffer to UIImage

       // Works only if pixel format is kCVPixelFormatType_420YpCbCr8BiPlanarFullRange

       - (UIImage *)convertSampleBufferToUIImageSampleBuffer:(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 plane pixel buffer

       void *baseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);

       // Get the number of bytes per row for the plane pixel buffer

       size_t bytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer,0);

       // Get the pixel buffer width and height

       size_t width = CVPixelBufferGetWidth(imageBuffer);

       size_t height = CVPixelBufferGetHeight(imageBuffer);

       // Create a device-dependent gray color space

       CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

       // Create a bitmap graphics context with the sample buffer data

       CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,

       bytesPerRow, colorSpace, kCGImageAlphaNone);

       // 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];

       // Release the Quartz image

       CGImageRelease(quartzImage);

       return (image);

       }

       为了完成实时的捕获,首先初始化一个AVCaputureSession对象用于创建一个捕获会话(session),我们可以使用AVCaptureSession对象将AV输入设备的数据流以另一种形式转换到输出。

       然后,我们初始化一个AVCaptureDeviceInput对象,以创建一个输入数据源,该数据源为捕获会话(session)提供视频数据,再调用addInput方法将创建的输入添加到AVCaptureSession对象。

       接着初始化一个AVCaptureVideoDataOuput对象,以创建一个输出目标,然后调用addOutput方法将该对象添加到捕获会话中。

       AVCaptureVideoDataOutput可用于处理从视频中捕获的未经压缩的帧。一个AVCaptureVideoDataOutput实例能处理许多其他多媒体API能处理的视频帧,你可以通过captureOutput:didOutputSampleBuffer:fromConnection:这个委托方法获取帧,使用setSampleBufferDelegate:queue:设置抽样缓存委托和将应用回调的队列。AVCaptureVideoDataOutputSampleBuffer对象的委托必须采用AVCaptureVideoDataOutputSampleBufferDelegate协议,使用sessionPreset协议来制定输出品质。

       我们可以通过调用捕获会话的startRunning方法启动从输入到输出的数据流,通过stopRunning方法来停止数据流。

       列表1给出了一个例子。setupCaptureSession创建了一个捕获会话,添加了一个视频输入提供提视频帧,一个输出目标获取捕获的帧,然后启动从输入到输出的数据流。当捕获会话正在运行时,使用captureOut:didOutputSampleBuffer:fromConnection方法将被捕获的视频抽样帧发送给抽样缓存委托,然后每个抽样缓存(CMSampleBufferRef)被转换成imageFromSampleBuffer中的一个UIImage对象。

       ---------------------------

       列表1:使用AV Foundation设置一个捕获设备录制视频并将是视频帧保存为UIImage对象。

       #import <AVFoundation/AVFoundation.h>

       // 创建并配置一个捕获会话并且启用它

       - (void)setupCaptureSession

       {

        NSError *error = nil;

        // 创建session

        AVCaptureSession *session = [[AVCaptureSession alloc] init];

        // 可以配置session以产生解析度较低的视频帧,如果你的处理算法能够应付(这种低解析度)。

        // 我们将选择的设备指定为中等质量。

        session.sessionPreset = AVCaptureSessionPresetMedium;

        // 找到一个合适的AVCaptureDevice

        AVCaptureDevice *device = [AVCaptureDevice

        defaultDeviceWithMediaType:AVMediaTypeVideo];

        // 用device对象创建一个设备对象input,并将其添加到session

        AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device

        error:&error];

        if (!input) {

        // 处理相应的错误

        }

        [session addInput:input];

        // 创建一个VideoDataOutput对象,将其添加到session

        AVCaptureVideoDataOutput *output = [[[AVCaptureVideoDataOutput alloc] init] autorelease];

        [session addOutput:output];

        // 配置output对象

        dispatch_queue_t queue = dispatch_queue_create("myQueue", NULL);

        [output setSampleBufferDelegate:self queue:queue];

        dispatch_release(queue);

        // 指定像素格式

        output.videoSettings =

        [NSDictionary dictionaryWithObject:

        [NSNumber numberWithInt:kCVPixelFormatType_32BGRA]

        forKey:(id)kCVPixelBufferPixelFormatTypeKey];

       // 如果你想将视频的帧数指定一个顶值, 例如15ps

        // 可以设置minFrameDuration(该属性在iOS 5.0中弃用)

        output.minFrameDuration = CMTimeMake(1, 15);

        // 启动session以启动数据流

        [session startRunning];

        // 将session附给实例变量

        [self setSession:session];

       }

       // 抽样缓存写入时所调用的委托程序

       - (void)captureOutput:(AVCaptureOutput *)captureOutput

        didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer

        fromConnection:(AVCaptureConnection *)connection

       {

        // 通过抽样缓存数据创建一个UIImage对象

        UIImage *image = [self imageFromSampleBuffer:sampleBuffer];

        < 此处添加使用该image对象的代码 >

       }

       // 通过抽样缓存数据创建一个UIImage对象

       - (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);

       }

       好了,关于“image quartz”的话题就讲到这里了。希望大家能够通过我的讲解对“image quartz”有更全面、深入的了解,并且能够在今后的工作中更好地运用所学知识。