视频滤镜

GPUImage原生美颜

GPUImage原生美颜

步骤一:使用Cocoapods导入GPUImage
步骤二:创建视频源GPUImageVideoCamera
步骤三:创建最终目的源:GPUImageView
步骤四:创建滤镜组(GPUImageFilterGroup),需要组合亮度(GPUImageBrightnessFilter)和双边滤波(GPUImageBilateralFilter)这两个滤镜达到美颜效果.
步骤五:设置滤镜组链
步骤六:设置GPUImage处理链,从数据源 => 滤镜 => 最终界面效果
步骤七:开始采集视频
注意点:

SessionPreset最好使用AVCaptureSessionPresetHigh,会自动识别,如果用太高分辨率,当前设备不支持会直接报错
GPUImageVideoCamera必须要强引用,否则会被销毁,不能持续采集视频.
必须调用startCameraCapture,底层才会把采集到的视频源,渲染到GPUImageView中,就能显示了。
GPUImageBilateralFilter的distanceNormalizationFactor值越小,磨皮效果越好,distanceNormalizationFactor取值范围: 大于1。

#import "ViewController.h"
#import 
@interface ViewController ()
@property (nonatomic,strong)GPUImageVideoCamera * videoCamera;
@property (nonatomic,strong)GPUImageBilateralFilter * bilateralFilter;
@property (nonatomic,strong)GPUImageBrightnessFilter * brightnessFilter;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    GPUImageVideoCamera * videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPresetHigh cameraPosition:AVCaptureDevicePositionBack];
    videoCamera.outputImageOrientation =  UIInterfaceOrientationPortrait;
    self.videoCamera = videoCamera;
    
    GPUImageView * GPUImg = [[GPUImageView alloc] initWithFrame:self.view.bounds];
    [self.view insertSubview:GPUImg atIndex:0];
    
    //创建过滤:磨皮,美白,组合滤镜
    GPUImageFilterGroup * groupFiler = [[GPUImageFilterGroup alloc] init];
    GPUImageBilateralFilter * bilateralFilter = [[GPUImageBilateralFilter alloc] init];
    self.bilateralFilter = bilateralFilter;
    GPUImageBrightnessFilter * brightnessFilter =[[GPUImageBrightnessFilter alloc] init];
    self.brightnessFilter = brightnessFilter;
    
    [groupFiler addTarget:bilateralFilter];
    [groupFiler addFilter:brightnessFilter];
    
    //设置滤镜组链
    [self.bilateralFilter addTarget:brightnessFilter];
    [groupFiler setInitialFilters:@[self.bilateralFilter]];
    groupFiler.terminalFilter = brightnessFilter;
    
    //设置GPUImage响应链,从数据源--》过滤--》最终界面
    [self.videoCamera addTarget:groupFiler];
    [groupFiler addTarget:GPUImg];
    //开始采集视频
    [self.videoCamera startCameraCapture];
}


利用美颜滤镜实现

步骤一:使用Cocoapods导入GPUImage
步骤二:导入GPUImageBeautifyFilter文件夹
步骤三:创建视频源GPUImageVideoCamera
步骤四:创建最终目的源:GPUImageView
步骤五:创建最终美颜滤镜:GPUImageBeautifyFilter
步骤六:设置GPUImage处理链,从数据源 => 滤镜 => 最终界面效果
注意:

切换美颜效果原理:移除之前所有处理链,重新设置处理链

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 创建视频源
    // SessionPreset:屏幕分辨率,AVCaptureSessionPresetHigh会自适应高分辨率
    // cameraPosition:摄像头方向
    GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPresetHigh cameraPosition:AVCaptureDevicePositionFront];
    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    _videoCamera = videoCamera;
    // 创建最终预览View
    GPUImageView *captureVideoPreview = [[GPUImageView alloc] initWithFrame:self.view.bounds];
    [self.view insertSubview:captureVideoPreview atIndex:0];
    _captureVideoPreview = captureVideoPreview;
    // 设置处理链
    [_videoCamera addTarget:_captureVideoPreview];
    // 必须调用startCameraCapture,底层才会把采集到的视频源,渲染到GPUImageView中,就能显示了。
    // 开始采集视频
    [videoCamera startCameraCapture];
}
- (IBAction)openBeautifyFilter:(UISwitch *)sender {
    // 切换美颜效果原理:移除之前所有处理链,重新设置处理链
    if (sender.on) {
        // 移除之前所有处理链
        [_videoCamera removeAllTargets];
        // 创建美颜滤镜
        GPUImageBeautifyFilter *beautifyFilter = [[GPUImageBeautifyFilter alloc] init];
        // 设置GPUImage处理链,从数据源 => 滤镜 => 最终界面效果
        [_videoCamera addTarget:beautifyFilter];
        [beautifyFilter addTarget:_captureVideoPreview];
    } else {
        // 移除之前所有处理链
        [_videoCamera removeAllTargets];
        [_videoCamera addTarget:_captureVideoPreview];
    }
}

iOS10闪退 崩溃 This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.

NSCameraUsageDescription    
cameraDesciption
NSContactsUsageDescription
contactsDesciption
NSMicrophoneUsageDescription
microphoneDesciption
NSPhotoLibraryUsageDescription
photoLibraryDesciption

你可能感兴趣的:(视频滤镜)