AVFoundation摄像头使用(纯属娱乐)

#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

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dlg1992/article/details/52371967
个人分类: ios开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭