美文网首页
分享一个自定义相机

分享一个自定义相机

作者: ZYiDa | 来源:发表于2017-07-14 08:52 被阅读28次

第一步,先导入#import <AVFoundation/AVFoundation.h>
第二步,创建需要的对象

@property (nonatomic,strong) AVCaptureDevice *devides;
@property (nonatomic,strong) AVCaptureDeviceInput *input;//输入设备
@property (nonatomic,strong) AVCaptureStillImageOutput *imageOutput;//输出图片
@property (nonatomic,strong) AVCaptureSession *session;//会话
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;//预览层

第三部,UI设置

- (void)initCamera
{
    self.devides = [self cameraWithPosition:AVCaptureDevicePositionBack];
    self.input = [[AVCaptureDeviceInput alloc]initWithDevice:self.devides error:nil];
    self.imageOutput = [[AVCaptureStillImageOutput alloc]init];
    self.session  = [[AVCaptureSession alloc]init];
    self.session.sessionPreset = AVCaptureSessionPreset1280x720;

    if ([self.session canAddInput:self.input])
    {
        [self.session addInput:self.input];
    }
    if ([self.session canAddOutput:self.imageOutput])
    {
        [self.session addOutput:self.imageOutput];
    }

    /**输出预览层**/
    self.previewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];
    self.previewLayer.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer addSublayer:self.previewLayer];
    [self.session startRunning];

    /**在这里添加需要的图层,比如人脸识别时需要的脸型图层**/
    CALayer *faceRectLayer = [CALayer new];
    faceRectLayer.zPosition = 1;
    faceRectLayer.frame = CGRectMake(0, 0, WIDTH, HEIGHT );
    faceRectLayer.backgroundColor = [UIColor colorWithRed:200/255 green:200/255 blue:200/255 alpha:0].CGColor;
    faceRectLayer.contents = (__bridge id _Nullable)(self.previewImage.CGImage);
    [self.previewLayer addSublayer:faceRectLayer];

    /**切换前后相机**/
    change = [UIButton buttonWithType:UIButtonTypeCustom];
    change.frame = CGRectMake(0, 0, 40, 40);
    change.center = CGPointMake(WIDTH - 60, HEIGHT - 60);
    [change setBackgroundImage:[UIImage imageNamed:@"切换"] forState:UIControlStateNormal];
    [change addTarget:self action:@selector(changeCamera) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:change];


    /**拍照**/
    takePhoto = [UIButton buttonWithType:UIButtonTypeCustom];
    takePhoto.frame = CGRectMake(0, 0, 60, 60);
    takePhoto.center = CGPointMake(WIDTH/2, HEIGHT - 60);
    [takePhoto setBackgroundImage:[UIImage imageNamed:@"拍照"] forState:UIControlStateNormal];
    [takePhoto addTarget:self action:@selector(selectedToTakePhoto) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:takePhoto];

    /**返回**/
    backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 40, 40);
    backBtn.center = CGPointMake(60, HEIGHT - 60);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(selectedToDismiss) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
}

- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position{
    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    for ( AVCaptureDevice *device in devices )
        if ( device.position == position )
        {
            return device;
        }
    return nil;
}

第四步,切换前后相机

#pragma mark 切换前后相机
- (void)changeCamera
{
    NSUInteger cameraCount = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
    if (cameraCount > 1)
    {
        NSError *error;

        /**添加切换时的翻转动画**/
        CATransition *animation = [CATransition animation];
        animation.duration = 0.5f;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.type = @"oglFlip";

        AVCaptureDevice *newCamera = nil;
        AVCaptureDeviceInput *newInput = nil;

        /**获取另外一个摄像头的位置**/
        AVCaptureDevicePosition position = [[_input device] position];
        if (position == AVCaptureDevicePositionFront)
        {
            newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
            self.session.sessionPreset = AVCaptureSessionPreset1280x720;
            animation.subtype = kCATransitionFromLeft;//动画翻转方向
        }
        else
        {
            newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
            self.session.sessionPreset = AVCaptureSessionPreset1280x720;
            animation.subtype = kCATransitionFromRight;//动画翻转方向
        }

        /**生成新的输入**/
        newInput = [AVCaptureDeviceInput deviceInputWithDevice:newCamera error:nil];
        [self.previewLayer addAnimation:animation forKey:nil];
        if (newInput != nil)
        {
            [self.session beginConfiguration];
            [self.session removeInput:self.input];
            if ([self.session canAddInput:newInput])
            {
                [self.session addInput:newInput];
                self.input = newInput;

            }
            else
            {
                [self.session addInput:self.input];
            }
            [self.session commitConfiguration];

        }
        else if (error)
        {
            NSLog(@"Change carema failed.Error:%@", error);
        }
    }
}

第五步,拍照,获取image

#pragma mark 拍照
- (void)selectedToTakePhoto
{
    AVCaptureConnection *conntion = [self.imageOutput connectionWithMediaType:AVMediaTypeVideo];
    if (!conntion)
    {
        NSLog(@"拍照失败!");
        return;
    }
    [self.imageOutput captureStillImageAsynchronouslyFromConnection:conntion completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
     {
         if (imageDataSampleBuffer == nil)
         {
             return ;
         }
         NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
         UIImage *image = [UIImage imageWithData:imageData];
         if (image == nil)
         {
             return;
         }

         /**block监听拍照动作,获取image**/
         self.takePhotoImage(image);
     }];
}

相关文章

  • 自己写一个Android照相机应用(2)

    自定义相机 上一篇讲的是调用系统相机拍照然后显示在屏幕上,自定义一个相机就是自己相机的activity。1,首先是...

  • 分享一个自定义相机

    第一步,先导入#import 第二步,创建需要的对象 第三部,UI设置 第四步,切换前后相机 第五步,拍照,获取...

  • Android自定义相机

    CustomCamera android自定义相机 功能描述: 主要可自定义相机的各类按钮布局 相机拍照缩放功能 ...

  • Android CameraX结合LibYUV和GPUImage

    目录 前言 之前使用Camera实现了一个自定义相机滤镜(Android自定义相机滤镜[https://www.j...

  • Android自定义相机,添加水印

    很多app都要求自定义一个相机,类似违章查询拍照,美图相机之类的应用都要求自定义相机,网上的例子大多数我也看过,很...

  • iOS 之自定义相机

    项目最近要用到相机,但是界面自己设计了,所以系统相机就使不上了。所以研究了下自定义相机。 因为自定义相机的API在...

  • 相机小白自定义Camera实践

    背景 机缘巧合,需要自定义相机,几日折腾下来,对相机开发有了一定认识,做个小结。 既然是自定义相机,在设想里,相机...

  • iOS8之后的新建相册和保存图片到相册

    前一段写了一个自定义相机的功能,梳理了一下有关相机底层、图片处理和相册管理的问题。 首先说一下底层自定义相机的实现...

  • Android实现视频录制

    目录 效果展示 实现步骤 ●实现自定义相机这里代码比较简单就不多解释了,另外自定义相机中实现了一个用于返回Came...

  • Three.js笔记(八)相机(2)

    自定义控件 回到PerspectiveCamera透视相机上来。注释OrthographicCamera正交相机,...

网友评论

      本文标题:分享一个自定义相机

      本文链接:https://www.haomeiwen.com/subject/dahnhxtx.html