AVFoundation摄像头使用(纯属娱乐)
来源:互联网 发布:武装直升机 知乎 编辑:程序博客网 时间:2024/05/02 15:38
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<AVCaptureVideoDataOutputSampleBufferDelegate>
@property(nonatomic,strong)AVCaptureSession *session;
@property(nonatomic,strong)AVCaptureVideoPreviewLayer *previewLayer;
@property(nonatomic,strong)UIView *preview;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.preview=[[UIViewalloc]init];
self.preview.frame=self.view.frame;
[self.viewaddSubview:self.preview];
[selfsetupCaptureSession];
}
-(void)setupCaptureSession{
//建立Session
self.session=[[AVCaptureSessionalloc]init];
//设置采集的质量
if([self.sessioncanSetSessionPreset:AVCaptureSessionPreset640x480]){
self.session.sessionPreset=AVCaptureSessionPreset640x480;
}else{
self.session.sessionPreset=AVCaptureSessionPresetHigh;
}
// NSArray *devices=[AVCaptureDevice devices];
// for (AVCaptureDevice *device in devices) {
// NSLog(@"设备名称:%@",[device localizedName]);
// //是否有Camera
// if ([device hasMediaType:AVMediaTypeVideo]) {
// //如果该设备的位置是后置的
// if([device position]==AVCaptureDevicePositionBack){
// NSLog(@"back:%@",[device localizedName]);
// }else{
// //前置摄像头
// NSLog(@"front:%@",[device localizedName]);
// }
// }
// }
//获取摄像头设备
AVCaptureDevice *device=[AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
AVCaptureDeviceInput *input=[AVCaptureDeviceInputdeviceInputWithDevice:deviceerror:&error];
if (!input) {
NSLog(@"%@",error);
return;
}
[self.sessionaddInput:input];
AVCaptureVideoDataOutput *output=[[AVCaptureVideoDataOutputalloc]init];
[self.sessionaddOutput:output];
dispatch_queue_t queue=dispatch_queue_create("DLG",NULL);
[output setSampleBufferDelegate:selfqueue:queue];
// output.videoSettings=[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
[self.sessionstartRunning];
AVCaptureVideoPreviewLayer *preViewLayer=[AVCaptureVideoPreviewLayerlayerWithSession:self.session];
preViewLayer.frame=self.preview.bounds;
preViewLayer.videoGravity=AVLayerVideoGravityResizeAspectFill;
[self.preview.layeraddSublayer:preViewLayer];
if (![self.sessionisRunning]) {
[self.sessionstartRunning];
}
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSLog(@"%s",__FUNCTION__);
NSArray *inputs=self.session.inputs;
for (AVCaptureDeviceInput *inputin inputs) {
AVCaptureDevice *device=input.device;
//是否有Camera
if ([devicehasMediaType:AVMediaTypeVideo]) {
AVCaptureDevicePosition position=device.position;
AVCaptureDevice *newCamera=nil;
AVCaptureDeviceInput *newInput=nil;
if(position==AVCaptureDevicePositionBack){
//后置摄像头
newCamera = [selfcameraWithPosition:AVCaptureDevicePositionFront];
}else{
//前置摄像头
newCamera = [selfcameraWithPosition:AVCaptureDevicePositionBack];
}
newInput=[AVCaptureDeviceInputdeviceInputWithDevice:newCameraerror:nil];
[self.sessionbeginConfiguration];
[self.sessionremoveInput:input];
[self.sessionaddInput:newInput];
[self.sessioncommitConfiguration];
break;
}
}
}
//获取前置摄像头设备或者后置摄像头设备
-(AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position{
NSArray *devices=[AVCaptureDevicedevicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *devicein devices) {
if (device.position==position) {
return device;
}
}
returnnil;
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection{
return;
}
@end
备注:更深了解请到 https://github.com/DLG90/dlg_AVFoundation
- AVFoundation摄像头使用(纯属娱乐)
- 纯属娱乐
- 纯属娱乐
- Ubuntu安装使用yum(纯属娱乐)
- Ubuntu安装使用yum(纯属娱乐)
- 电脑性能测试(纯属娱乐)
- AVFoundation使用
- html + js实现抽奖 纯属娱乐
- 通过AVFoundation获取摄像头数据[Swift]
- 通过AVFoundation框架获取摄像头数据
- 大阪第51天——纯属娱乐
- [转帖]纯属娱乐——变形金刚vs天网
- 测试 色彩占卦你的内心秘密(纯属娱乐)
- 自己没事重写了array_chunk函数,纯属娱乐
- linux 内核动态内存分配测试(纯属娱乐哈)
- CF305B----用大数水过的模拟(纯属娱乐)
- 一道逻辑推理题的程序实现(纯属娱乐)
- 消息队列实现简单即时通信(纯属娱乐)
- HDU 5810 Balls and Boxes(概率)
- 【快速学习】学习cocos2dx之旅(1)
- 适配器模式
- 在WinServer上安装小红伞杀毒软件的经验总结
- ffmpeg错误码
- AVFoundation摄像头使用(纯属娱乐)
- php 二
- [CC] Cocos2d-x热更新后台实现
- [数据结构]二叉查找树-基础版
- Freemarker中Configuration的setClassForTemplateLoading方法参数问题
- 策略模式
- Jmeter带cookie请求
- Lua 教程-04
- java复习(5)—String、StringBuilder和StringBuffer