基于GPUImage的自定义相机 --- 拍照录像



判断相册权限

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    {
        //无权限
        DbpAlertView *alert = [[DbpAlertView alloc]initWithTitle:NSLocalizedString(@"HCC_set", @"此功能需您到:设置->隐私->相机里面打开权限") SubTitile:@"" Image:[UIImage imageNamed:@"Alert_DG_WAR"] CancelButton:@"" OkButton:NSLocalizedString(@"alert_OK", @"确定") andAlertTag:@"105"];
        //        alert.delegate = self;
        [alert show];
        return;
    }

创建相机

录视频采用 GPUImageVideoCamera 做输入源

GPUImageVideoCamera摄像头用于实时拍摄视频

GPUImageStillCamera摄像头用于实时拍摄照片

GPUImagePicture用于处理已经拍摄好的图片

GPUImageMovie用于处理已经拍摄好的视频

//初始化videocamera 录视频输入源
    videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
    //设置照片的方向为设备的定向
    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

创建两层  GPUImageView 显示两种滤镜,上层添加拖拽手势用于切换滤镜。底层正常显示,上层根据 手势偏移不断变化 

   layer.mask

    filteredVideoView = [[GPUImageView alloc] initWithFrame:self.view.bounds];
    [filteredVideoView setFillMode:kGPUImageFillModePreserveAspectRatioAndFill];
    [self.view addSubview:filteredVideoView];
    
    nextFilteredVideoView = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self applyMaskToNextVideoView:CGRectMake(0, 0, 0, self.view.bounds.size.height)];
    [nextFilteredVideoView setFillMode:kGPUImageFillModePreserveAspectRatioAndFill];
    [self.view addSubview:nextFilteredVideoView];
    
    //拖拽手势:用于切换滤镜
    UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [nextFilteredVideoView addGestureRecognizer:panGestureRecognizer];


//    给两层添加滤镜
    [self changeFilter:[filters objectAtIndex:0] targetVideoView:filteredVideoView];
    [self changeFilter:[self getFilterAtIndex:filterIndex + 1] targetVideoView:nextFilteredVideoView];
  //开始捕获
    [videoCamera startCameraCapture];



创建  GPUImageMovieWriter 用于写入捕获的数据流

   pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
//    判断路径是否存在
    unlink([pathToMovie UTF8String]);
    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
    movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:self.view.bounds.size];

//    添加滤镜
    [[filters objectAtIndex:0] addTarget:movieWriter];
    //添加音频输入
    videoCamera.audioEncodingTarget = movieWriter;
    movieWriter.shouldPassthroughAudio = YES;


拍照按钮自定义,点击拍照

-(void)tapPressBtnState:(UIGestureRecognizerState)state{
    if (state == UIGestureRecognizerStateBegan) {
        开始写入数据
        [movieWriter startRecording];
    }else if (state == UIGestureRecognizerStateEnded){
	获取当前数据流的图像,
        [filters[filterIndex] useNextFrameForImageCapture];
        UIImage *img = [filters[filterIndex] imageFromCurrentFramebuffer];
        结束录制
        [movieWriter finishRecordingWithCompletionHandler:^{
            EditorViewController *testVC = [[EditorViewController alloc]init];
            testVC.photoImage = img;
            testVC.filterIndex = filterIndex;
            testVC.delegate = self;
            [self presentViewController:testVC animated:YES completion:^{
                //
                videoCamera.audioEncodingTarget = nil;
                [videoCamera stopCameraCapture];
                
            }];
        }];
    }
}



长按实现录像

-(void)longPressBtnState:(UIGestureRecognizerState)state
{
    if (state == UIGestureRecognizerStateBegan) {
        [movieWriter startRecording];
    }else if (state == UIGestureRecognizerStateEnded){
        [movieWriter finishRecordingWithCompletionHandler:^{
            EditorViewController *testVC = [[EditorViewController alloc]init];
            testVC.stickArr = [self getStikersArr];
            testVC.delegate = self;
            testVC.filterIndex = filterIndex;
	    获取视频路径
            testVC.videoPath = pathToMovie;
            [self presentViewController:testVC animated:YES completion:^{
                videoCamera.audioEncodingTarget = nil;
                [videoCamera stopCameraCapture];
            }];
        }];
    }
}















自定义录制button ,点击拍照,长按可录像

创建底层背景圆环

        CAShapeLayer *circleLayer = [CAShapeLayer layer];
        //创建一个园
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
                              CGRectMake(0.0f, 0.0f, 116*_scrale_x, 116*_scrale_x)];
        [circleLayer setPath:[path CGPath]];
        // 边缘线的颜色
        [circleLayer setStrokeColor:[[UIColor whiteColor] CGColor]];
          // 线条宽度
        [circleLayer setLineWidth:8.0f];
        // 闭环填充的颜色
        [circleLayer setFillColor:[[UIColor clearColor] CGColor]];
        [[self layer] addSublayer:circleLayer];



创建进度的圆弧

	progressLayer = [CAShapeLayer layer];
        progressPath = [UIBezierPath bezierPath];
//        画圆弧
//        center:圆心的坐标
//        radius:半径
//        startAngle:起始的弧度
//        endAngle:圆弧结束的弧度
//        clockwise:YES为顺时针,No为逆时针
        [progressPath addArcWithCenter:CGPointMake(58*_scrale_x,58*_scrale_x)
                                       radius: 58*_scrale_x
                                   startAngle: - M_PI_2
                                     endAngle: -M_PI_2 + M_PI * 2 * 0.005
                                    clockwise:YES];
         
         [progressLayer setPath:[progressPath CGPath]];
       [progressLayer setStrokeColor:[UIColorFromRGB(0x2CD1B1) CGColor]];
        [progressLayer setLineWidth:8.0f];
        //圆角
        progressLayer.lineCap = kCALineCapRound;
        [progressLayer setFillColor:[[UIColor clearColor] CGColor]];
        [[self layer] addSublayer:progressLayer];



给button 添加 手势

	UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
        [self addGestureRecognizer:longPress];
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPress:)];
        [self addGestureRecognizer:tapGes];

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    代理回调长按状态
    if ([self.delegate respondsToSelector:@selector(longPressBtnState:)]) {
        [self.delegate longPressBtnState:gesture.state];
    }
    if (gesture.state == UIGestureRecognizerStateBegan ) {
         self.progressTimer.fireDate = [NSDate distantPast];
//        pop动画,长按时进度条放大
        POPBasicAnimation * scaleUpAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
        scaleUpAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.6, 1.6)];
        scaleUpAnimation.duration = 1.5f;
        [self.layer pop_addAnimation:scaleUpAnimation forKey:@"scaleUpAnimation"];
    }
    else if(gesture.state == UIGestureRecognizerStateChanged){
    }
    else if(gesture.state == UIGestureRecognizerStateEnded ) {
       self.progressTimer.fireDate = [NSDate distantFuture];
        self.progress = 0;
        [self setProgressValue];
        POPBasicAnimation * scaleDownAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
        scaleDownAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.0, 1.0)];
        
        [self.layer pop_addAnimation:scaleDownAnimation forKey:@"scaleDownAnimation"];
    }
}


 
  








你可能感兴趣的:(基于GPUImage的自定义相机 --- 拍照录像)