GpuImage简单使用

 声明变量

@interface **********
{
    GPUImageVideoCamera *Camera;
    GPUImageOutput *Filters;
    GPUImageView *CameraShowView;
}

首先是Camera,一种是GPUImageStillCamera,另一个种为GPUImageVideoCamera.正如其命名,如果只是拍照使用前者.录制视频使用后者.

    //init, 第一个参数分辨率 第二个参数,前后摄像头选择
    Camera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionFront];
    Camera.outputImageOrientation = UIInterfaceOrientationPortrait;
    Camera.horizontallyMirrorFrontFacingCamera = NO;
    Camera.horizontallyMirrorRearFacingCamera = NO;

其次camera获取视频(图片)后,加以滤镜也就是filter, filter可以叠加,也可以自定义(比如美颜).

    //黑白滤镜
    Filters = [GPUImageColorPackingFilter new];
    //将将滤镜加至摄像头
    [Camera addTarget:Filters];

最后输出处理后的视频或图片,这里可以可以直接输出至屏幕,也可以使用GPUImageMovieWriter,也就是录制了.

    //用于显示视频的GPUImageView
    CameraShowView =   [[GPUImageView alloc] initWithFrame:CGRectMake(0, 64,self.view.frame.size.width,self.view.frame.size.width*1.25)];
    [CameraShowView setInputRotation:kGPUImageFlipHorizonal atIndex:0];
    CameraShowView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
    [self.view addSubview:CameraShowView];
    //将视频输出到->CameraShowView
    [Filters addTarget:CameraShowView];

当然也不要忘记启动

    //视频启动
    [Camera startCameraCapture];

 

其他

//前后摄像头切换
-(void)VideoPosition:(UIButton*)Button{

    [Camera rotateCamera];
    
    Button.selected = !Button.selected;
}

//闪关灯
-(void)LightBtn:(UIButton*)Btn{
    
    if (Camera.inputCamera.position == AVCaptureDevicePositionBack) {
        
        if (Btn.selected) {
            
            [Camera.inputCamera lockForConfiguration:nil];
            [Camera.inputCamera setTorchMode:AVCaptureTorchModeOff];
            [Camera.inputCamera unlockForConfiguration];
            
        }else{
            
            [Camera.inputCamera lockForConfiguration:nil];
            [Camera.inputCamera setTorchMode:AVCaptureTorchModeOn];
            [Camera.inputCamera unlockForConfiguration];
        }

        Btn.selected = !Btn.selected;
        
    }else{
        
        NSLog(@"当前使用前置摄像头,未能开启闪光灯");
        
    }
    

}

 

多滤镜叠加,

由于项目要求,视频在录制的时候,需要使用特殊的比例(1比1),而在GPUImageVideoCamera的默认配置里,并没有这个比例.

所以考虑到录制的过程中需要使用美颜的滤镜,所以需使用GPUImageFilterGroup.以下就是添加了一个裁剪的滤镜

    //其实也就是filter替换成FilterGroup
    _filterGroup = [[GPUImageFilterGroup alloc] init];
    //这里可以添加多个不同滤镜
    [self addGPUImageFilter:[[GPUImageCropFilter alloc] initWithCropRegion:CGRectMake(0.0, 0.0, 1.0,0.5625)]];
    

addGPUImageFilter

//添加滤镜
- (void)addGPUImageFilter:(GPUImageOutput<GPUImageInput> *)filter
{
    [_filterGroup addFilter:filter];
    
    GPUImageOutput<GPUImageInput> *newTerminalFilter = filter;
    
    NSInteger count = _filterGroup.filterCount;
    
    if (count == 1)
    {
        _filterGroup.initialFilters = @[newTerminalFilter];
        _filterGroup.terminalFilter = newTerminalFilter;
        
    } else
    {
        GPUImageOutput<GPUImageInput> *terminalFilter    = _filterGroup.terminalFilter;
        NSArray *initialFilters                          = _filterGroup.initialFilters;
        
        [terminalFilter addTarget:newTerminalFilter];
        
        _filterGroup.initialFilters = @[initialFilters[0]];
        _filterGroup.terminalFilter = newTerminalFilter;
    }
}

 

posted @ 2016-09-19 16:57  NGI.  阅读(4715)  评论(0编辑  收藏  举报
GitHub | M1989