iOS GPUImage之GPUImageVideoCamera(3)


GPUImageVideoCamera 为实时摄影添加滤镜

示例:类似于直播App为主播添加美颜滤镜

需要添加两个滤镜文件    
[已上传至CSDN] http://download.csdn.net/detail/merrygoot/9564439
GPUImageBeautyFilter
GPUImageEmptyFilter

!当然你如果想测试 GPUImageVideoCamera 最简单的使用方法,
!
!可以直接使用GPUImage内置的GPUImageToonFilter(卡通滤镜)等。


//
//  ViewController.m
//  GPUImageDemo
//
//  Created by hj on 16/6/30.
//  Copyright © 2016年 hj. All rights reserved.
//

#import "ViewController.h"

#import "GPUImage.h"
#import "GPUImageBeautyFilter.h"
#import "GPUImageEmptyFilter.h"

@interface ViewController ()
{
    BOOL isBeauty;
}
@property (nonatomic,strong) GPUImageOutput   *filter;
@property (nonatomic,strong) GPUImageView                    *filterImageView;
@property (nonatomic,strong) GPUImageVideoCamera             *videoCamera;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化 videoCamera
     self.videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPresetHigh cameraPosition:AVCaptureDevicePositionBack];
     self.videoCamera.outputImageOrientation              = UIInterfaceOrientationPortrait;
     self.videoCamera.horizontallyMirrorFrontFacingCamera = NO;
     self.videoCamera.horizontallyMirrorRearFacingCamera  = NO;
    [self.videoCamera.captureSession setSessionPreset:AVCaptureSessionPresetHigh];

    // 初始化 filter
    self.filter = [[GPUImageBeautyFilter alloc] init];

    // 添加美颜滤镜
    [self.videoCamera addTarget:self.filter];

    // 添加显示视图
     self.filterImageView = [[GPUImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.filterImageView];
    [self.filter addTarget:self.filterImageView];

    // 开始进行相机捕获
    [self.videoCamera startCameraCapture];

    // 添加声音捕获
    // _videoCamera.audioEncodingTarget;

    // 监听获取 Buffer
    /**

     请额外在 GPUImageFramebuffer.m 中添加以下方法

     - (CVPixelBufferRef *)bytesBuffer
     {
     #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
     return renderTarget;
     #else
     return NULL; // TODO: do more with this on the non-texture-cache side
     #endif
     }

     **/
    __weak typeof(self) weakSelf = self;
    [_filter setFrameProcessingCompletionBlock:^(GPUImageOutput * filter, CMTime frameTime){
         CVPixelBufferRef *buffer = [filter.framebufferForOutput bytesBuffer];
    }];

}

// 点击屏幕实现美颜的开启与关闭功能
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.filter removeAllTargets];
    [self.videoCamera removeAllTargets];

    if (isBeauty) { // 正常
         self.filter = [[GPUImageEmptyFilter alloc] init];
        [self.filter addTarget:self.filterImageView];
        [self.videoCamera addTarget:_filter];
        isBeauty = NO;

    } else {        // 美颜
         self.filter = [[GPUImageBeautyFilter alloc] init];
        [self.filter addTarget:self.filterImageView];
        [self.videoCamera addTarget:_filter];
        isBeauty = YES;
    }
}

@end


测试:谁漂亮谁测 哈哈!

参考文章
GPUImage遇到的坑

你可能感兴趣的:(iOS GPUImage之GPUImageVideoCamera(3))