iOS 从摄像头获取YUV420SP数据
来源:互联网 发布:nga数据库6.0 编辑:程序博客网 时间:2024/05/17 06:58
需要引入两个库
#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 *)captureOutputdidOutputSampleBuffer:(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>
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显示
- iOS 从摄像头获取YUV420SP数据
- iOS直接获取摄像头数据
- iOS直接获取摄像头数据
- iOS直接获取摄像头数据
- IOS获取摄像头数据 显示当地视频
- iOS 从网络获取数据
- iOS 获取摄像头视频
- YUV420SP
- jpeg的数据从A8送入到videoM3解码,解码成yuv420sp
- 从摄像头/相册获取图片
- 从摄像头中获取视频
- iOS总结(从网络上获取数据)
- iOS-从plist文件获取数据
- iOS获取摄像头是否允许
- 利用Video4Linux获取摄像头数据
- 利用Video4Linux获取摄像头数据
- IOS4直接获取摄像头数据
- 大华摄像头获取yuv数据
- 今天创建博客了
- mysql insert_select ID 自增长
- 斯坦福机器学习课程 Exercise 习题二
- android 动画之 属性动画propertyAnimator(一)
- atitit.网络文件访问协议.unc smb nfs ftp http的区别
- iOS 从摄像头获取YUV420SP数据
- sqlite基础语法
- bootstrap学习网站
- MapReduce Kmeans聚类算法
- 在项目中常见的问题,是时候总结点常见的小的问题了
- Ext.js5的数组表格(3)
- Atitit.论图片类型 垃圾文件的识别与清理 流程与设计原则 与api概要设计 v2 pbj
- Linux 下shell编程
- c小基础