2019-03-22

GPUImage 之 GPUImageVideoCamera 实现美颜相机功能,并将美颜后的视频保存到手机相册

//
//  CameraVideoViewController.m
//  GPUImageUse
//
//  Created by SunLu on 2019/3/21.
//  Copyright © 2019 Sl. All rights reserved.
//

#import "CameraVideoViewController.h"
#import 
#import 
@interface CameraVideoViewController ()
@property(nonatomic,strong) GPUImageVideoCamera *camera;//必须h定义
@property(nonatomic,strong) GPUImageView *filterView;
@property(nonatomic,strong) GPUImageFilterGroup *filterGroup;
@property(nonatomic,strong) GPUImageMovieWriter *movieWriter;
@end

@implementation CameraVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self buildGPUImageVideoCamera];
    
    UIButton *button = [[UIButton alloc] init];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-60);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    button.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [button addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 40;
    
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //view 不再显示时,停止录制(可根据需要写一个button控制)
    self.camera.audioEncodingTarget = nil;
    [self.movieWriter finishRecording];
    [self.filterGroup removeTarget:self.movieWriter];
}
-(void)buildGPUImageVideoCamera
{
    
    /********************** 将摄像头视频加美颜处理后显示在屏幕 *********************/
    
    //创建camera 视频
    self.camera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionBack];
    self.camera.horizontallyMirrorFrontFacingCamera = YES;
    self.camera.horizontallyMirrorRearFacingCamera  = YES;
    self.camera.outputImageOrientation = UIInterfaceOrientationPortrait;
    [self.camera.captureSession setSessionPreset:AVCaptureSessionPreset1280x720];
    [self.camera addAudioInputsAndOutputs];
    [self.camera rotateCamera];//切换相机
    
    //滤镜组
    self.filterGroup = [[GPUImageFilterGroup alloc] init];
    

    GPUImageBilateralFilter *bilateralFilter = [[GPUImageBilateralFilter alloc] init];//磨皮
    //bilateralFilter.distanceNormalizationFactor = 10.0;
    GPUImageExposureFilter *exposureFilter = [[GPUImageExposureFilter alloc] init];       //曝光
    GPUImageBrightnessFilter *brightnessFilter = [[GPUImageBrightnessFilter alloc] init];     //美白
    brightnessFilter.brightness = 0.2;
    GPUImageSaturationFilter *satureationFilter = [[GPUImageSaturationFilter alloc] init];    //饱和
    satureationFilter.saturation = 0.5;
    
    
    //
    [self.filterGroup addTarget:bilateralFilter];
    [self.filterGroup addTarget:exposureFilter];
    [self.filterGroup addTarget:brightnessFilter];
    [self.filterGroup addTarget:satureationFilter];

    //设置滤镜连接关系
    [bilateralFilter addTarget:exposureFilter];
    [exposureFilter addTarget:brightnessFilter];
    [brightnessFilter addTarget:satureationFilter];

//    设置group 起点、终点
    self.filterGroup.initialFilters = @[bilateralFilter];
    self.filterGroup.terminalFilter = satureationFilter;
    
    //设置GPUImage的响应链
    [self.camera addTarget:self.filterGroup];
    
    //创建视频播放视图
    self.filterView = [[GPUImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.filterView];
    self.filterView.backgroundColor = [UIColor greenColor];
    [self.filterGroup addTarget:self.filterView];
    
    //开始采集视频
    [self.camera startCameraCapture];
    
    
    
    
    /********************** 录制添加美颜效果后的摄像头视频,并保存到相册 *********************/
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *videoSavePath = [docDir stringByAppendingString:@"/Video_1.mp4"];
    unlink([videoSavePath UTF8String]);
    //初始化这个地址,不能用UrlWithString的方法
    NSURL *saveUrl = [NSURL fileURLWithPath:videoSavePath];
    
    self.movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:saveUrl size:CGSizeMake(720, 1280)];
    //开启声音采集
    self.movieWriter.encodingLiveVideo = YES;//声音跟视频同步
    self.movieWriter.shouldPassthroughAudio = YES;
    self.movieWriter.hasAudioTrack=YES;
    
    [self.filterGroup addTarget:self.movieWriter];
    self.camera.audioEncodingTarget = self.movieWriter;
    [self.movieWriter startRecording];
    
    __weak typeof(self) weakSelf = self;
    [weakSelf.movieWriter setCompletionBlock:^{
        UISaveVideoAtPathToSavedPhotosAlbum(videoSavePath, weakSelf, @selector(video:didFinishSavingWithError:contextInfo:), nil);
    }];

}
//拍照
-(void)takePhoto
{
    UIImage *image = [self snapsHotView:self.filterView];
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (UIImage *)snapsHotView:(UIView *)view
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size,YES,[UIScreen mainScreen].scale);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}




//保存 image 到相册回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    
}
//保存 video 到相册回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    
}
@end

你可能感兴趣的:(2019-03-22)