GPUImage学习

前言

  1. 一些自带滤镜
    //磨皮GPUImageBilateralFilter
    //绿巨人GPUImageHueFilter
    //移行幻影GPUImageLowPassFilter
    //怀旧GPUImageMonochromeFilter、GPUImageSepiaFilter
    //黑白GPUImageOpeningFilter
    //像素GPUImagePixellateFilter
    //油画GPUImagePosterizeFilter、GPUImageSmoothToonFilter
    //素描GPUImageSketchFilter、GPUImageThresholdSketchFilter
    //--------------GPUImageSkinToneFilter
    //血轮眼GPUImageSwirlFilter
  1. 支持滤镜摄像,滤镜相机,滤镜图片,滤镜视频

 // - GPUImageVideoCamera (for live video from an iOS camera) 
// - GPUImageStillCamera (for taking photos with the camera)
 // - GPUImagePicture (for still images)
 // - GPUImageMovie (for movies)

一.使用滤镜生成UIImage对象

  1. 创建一个滤镜类
  2. 使用GPUImagePicture加载滤镜
  3. processImage呈现图像
  4. useNextFrameForImageCapture持续图片呈现(必须要加这句不然会生成nil)
  5. imageFromCurrentFramebuffer生成图片


+ (UIImage *)applySoftEleganceFilter:(UIImage *)image
{
    GPUImageSoftEleganceFilter *filter = [[GPUImageSoftEleganceFilter alloc] init];
    [filter forceProcessingAtSize:CGSizeMake(image.size.width / 2.0, image.size.height / 2.0)];
    GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:image];
    [pic addTarget:filter];
    
    [pic processImage];
    [filter useNextFrameForImageCapture];
    return [filter imageFromCurrentFramebuffer];
}
滤镜效果

二.使用滤镜相机

  1. 定义相机的输出页面
   GPUImageView *cameraView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 44,GBWidth, GBWidth + 145 * GBScale - 44)];
    //设置为水平镜像,因为磨人是前置,会有镜像
    [cameraView setInputRotation:kGPUImageFlipHorizonal atIndex:0];
    cameraView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
    [self.view addSubview:cameraView];
    self.cameraView = cameraView;
  1. 定义滤镜操作对象
   stillCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:@"AVCaptureSessionPresetPhoto" cameraPosition:AVCaptureDevicePositionFront];
   stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
  1. 定义滤镜需要的对象
//初始化自己需要的滤镜
filter = [[GPUImageBilateralFilter alloc] init];
  1. 添加滤镜
 [stillCamera addTarget:filter];
  1. 将滤镜添加到相机
[filter addTarget:cameraView];
  1. 开启滤镜
[stillCamera startCameraCapture];
  1. 拍照
   GPUImageStillCamera *stillCamera;
    GPUImageOutput<GPUImageInput> *filter, *secondFilter, *terminalFilter;
    
    GPUImagePicture *memoryPressurePicture1, *memoryPressurePicture2;
- (void)setupCameraView {
    //定义相机输出界面
    GPUImageView *cameraView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 44,GBWidth, GBWidth + 145 * GBScale - 44)];
    //设置为水平镜像,因为磨人是前置,会有镜像
    [cameraView setInputRotation:kGPUImageFlipHorizonal atIndex:0];
    cameraView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
    [self.view addSubview:cameraView];
    self.cameraView = cameraView;
    //定义滤镜操作对象
    stillCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:@"AVCaptureSessionPresetPhoto" cameraPosition:AVCaptureDevicePositionBack];
    stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    //定义需要的滤镜
    filter = [[GPUImagePixellateFilter alloc] init];
    [stillCamera addTarget:filter];
    //滤镜添加到相机中
    [filter addTarget:cameraView];
    //开启滤镜
    [stillCamera startCameraCapture];

    [self.cameraView setInputRotation:kGPUImageFlipHorizonal atIndex:0];
}

三.使用滤镜录像

1.创建录像机对象GPUImageVideoCamera
2.创建滤镜对象
3.创建视频的输出页面
4.创建视频写入GPUImageMovieWriter
5.添加视频写入添加滤镜
6.开始录像
7.保存

    GPUImageVideoCamera *videoCamera;
    GPUImageOutput<GPUImageInput> *filter;
    GPUImageMovieWriter *movieWriter;
   videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];
    
    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    videoCamera.horizontallyMirrorFrontFacingCamera = NO;
    videoCamera.horizontallyMirrorRearFacingCamera = NO;
    
    filter = [[GPUImageSepiaFilter alloc] init];
    [videoCamera addTarget:filter];
    
    GPUImageView *filterView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 44,GBWidth, GBWidth + 145 * GBScale - 44)];
    filterView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill;
    [self.view addSubview:filterView];
    NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
    unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
    NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
    
    movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(480.0, 640.0)];
    movieWriter.encodingLiveVideo = YES;
    [filter addTarget:movieWriter];
    [filter addTarget:filterView];
    
    [videoCamera startCameraCapture];
    
    double delayToStartRecording = 0.5;
    dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, delayToStartRecording * NSEC_PER_SEC);
    dispatch_after(startTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"Start recording");
        
        videoCamera.audioEncodingTarget = movieWriter;
        [movieWriter startRecording];
 
        double delayInSeconds = 10.0;
        dispatch_time_t stopTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(stopTime, dispatch_get_main_queue(), ^(void){
            
            [filter removeTarget:movieWriter];
            videoCamera.audioEncodingTarget = nil;
            [movieWriter finishRecording];
            NSLog(@"Movie completed");
            
            ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
            if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:movieURL])
            {
                [library writeVideoAtPathToSavedPhotosAlbum:movieURL completionBlock:^(NSURL *assetURL, NSError *error)
                 {
                     dispatch_async(dispatch_get_main_queue(), ^{
                         
                         if (error) {
                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed"
                                                                            delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                             [alert show];
                         } else {
                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved" message:@"Saved To Photo Album"
                                                                            delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                             [alert show];
                         }
                     });
                 }];
            }
            //            [videoCamera.inputCamera lockForConfiguration:nil];
            //            [videoCamera.inputCamera setTorchMode:AVCaptureTorchModeOff];
            //            [videoCamera.inputCamera unlockForConfiguration];
        });
    });

Demo下载链接:https://pan.baidu.com/s/1miKwbfq

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容