iOS 从摄像头获取YUV420SP数据
2015-12-17 18:32
447 查看
需要引入两个库
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
所在类需遵守AVCaptureVideoDataOutputSampleBufferDelegate代理
设置Session
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange];
表示设置摄像头返回的数据类型为YUV420SP类型
[self.captureSessionsetSessionPreset:AVCaptureSessionPreset640x480];
设置分辨率
AVCaptureVideoDataOutputSampleBufferDelegate代理中的操作
将YUV420SP的数据重新排列为YUV420P
Byte *buf =
malloc(width * height *3/
2);
memcpy(buf, imageAddress, width * height);
size_t a = width * height;
size_t b = width * height *
5 / 4;
for (NSInteger i =0; i < width * height/
2; i ++) {
memcpy(buf + a, imageAddress + width * height + i ,1);
a++;
i++;
memcpy(buf + b, imageAddress + width * height + i,1);
b++;
}
buf中的数据就是YUV420P的数据可以供OpenGL ES显示
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
所在类需遵守AVCaptureVideoDataOutputSampleBufferDelegate代理
设置Session
<span style="font-size:18px;">- (void)setSession { _captureInput = [[AVCaptureDeviceInput alloc]initWithDevice:[self getFrontCameraDevice] error:nil]; AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init]; captureOutput.alwaysDiscardsLateVideoFrames = YES; dispatch_queue_t queue; queue = dispatch_queue_create("cameraQueue", NULL); [captureOutput setSampleBufferDelegate:self queue:queue]; NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange]; NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key]; [captureOutput setVideoSettings:videoSettings]; self.captureSession = [[AVCaptureSession alloc] init]; [self.captureSession addInput:_captureInput]; [self.captureSession addOutput:captureOutput]; [self.captureSession setSessionPreset:AVCaptureSessionPreset640x480]; }</span>
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange];
表示设置摄像头返回的数据类型为YUV420SP类型
[self.captureSessionsetSessionPreset:AVCaptureSessionPreset640x480];
设置分辨率
<span style="font-size:18px;">/** * 获取前置摄像头 * * @return 摄像头设备 */ - (AVCaptureDevice *)getFrontCameraDevice{ NSArray *cameras= [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *camera in cameras) { if ([camera position] == AVCaptureDevicePositionFront) { return camera; } } return nil; }</span>
AVCaptureVideoDataOutputSampleBufferDelegate代理中的操作
<span style="font-size:18px;">#pragma mark AVCaptureSession - delegate - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); // Lock the base address of the pixel buffer CVPixelBufferLockBaseAddress(imageBuffer, 0); CMTime pts = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); CMTime duration = CMSampleBufferGetDuration(sampleBuffer); // Get the number of bytes per row for the plane pixel buffer void *imageAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0); size_t width = CVPixelBufferGetWidth(imageBuffer); size_t height = CVPixelBufferGetHeight(imageBuffer); Byte *buf = malloc(width * height * 3/ 2); memcpy(buf, imageAddress, width * height); size_t a = width * height; size_t b = width * height * 5 / 4; for (NSInteger i = 0; i < width * height/ 2; i ++) { memcpy(buf + a, imageAddress + width * height + i , 1); a++; i++; memcpy(buf + b, imageAddress + width * height + i, 1); b++; } CVPixelBufferUnlockBaseAddress(imageBuffer,0); }</span>
将YUV420SP的数据重新排列为YUV420P
Byte *buf =
malloc(width * height *3/
2);
memcpy(buf, imageAddress, width * height);
size_t a = width * height;
size_t b = width * height *
5 / 4;
for (NSInteger i =0; i < width * height/
2; i ++) {
memcpy(buf + a, imageAddress + width * height + i ,1);
a++;
i++;
memcpy(buf + b, imageAddress + width * height + i,1);
b++;
}
buf中的数据就是YUV420P的数据可以供OpenGL ES显示
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- C#实现控制摄像头的类
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- Android 开发随手笔记之使用摄像头拍照
- .net平台推送ios消息的实现方法
- C#实现调用本机摄像头实例
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- IOS 改变键盘颜色代码
- 举例详解iOS开发过程中的沙盒机制与文件