很早之前下了一个大佬的博客,忘了地址了(不好意思),之后找到会贴出来,感觉对学习GPUImage非常有帮助,简单易懂,所以介绍给大家,首先来看效果:
步骤:
- 初始化GPUImageStillCamera相机,并设置相机尺寸和采用哪个摄像头,以及相机方向。
- 创建滤镜,并添加到摄像头上。
- 初始化一个GPUImageView添加到view上,并添加为滤镜的输出承载。
- 开启相机。
其实核心的代码就下面这些,其他相关代码看demo,其中GPUImageBeautifyFilter
这个美颜滤镜是这位大神写的。
#pragma mark 相机动态渲染
-(void)cameraFilter {
//初始化相机,第一个参数表示相册的尺寸,第二个参数表示前后摄像头
_camera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionFront];
// 相机方向
_camera.outputImageOrientation = UIInterfaceOrientationPortrait;
//哈哈镜效果
GPUImageStretchDistortionFilter *stretchDistortionFilter = [[GPUImageStretchDistortionFilter alloc] init];
//亮度
GPUImageBrightnessFilter *BrightnessFilter = [[GPUImageBrightnessFilter alloc] init];
//伽马线滤镜
GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc] init];
//边缘检测
GPUImageXYDerivativeFilter *XYDerivativeFilter = [[GPUImageXYDerivativeFilter alloc] init];
//怀旧
GPUImageSepiaFilter *sepiaFilter = [[GPUImageSepiaFilter alloc] init];
//反色
GPUImageColorInvertFilter *invertFilter = [[GPUImageColorInvertFilter alloc] init];
//饱和度
GPUImageSaturationFilter *saturationFilter = [[GPUImageSaturationFilter alloc] init];
//美颜
GPUImageBeautifyFilter *beautyFielter = [[GPUImageBeautifyFilter alloc] init];
//素描
GPUImageSketchFilter *sketchFilter = [[GPUImageSketchFilter alloc] init];
//黑白
GPUImageMonochromeFilter *thresholdFilter = [[GPUImageMonochromeFilter alloc] init];
// 滤镜数组
_filterArr = @[stretchDistortionFilter,BrightnessFilter,gammaFilter,XYDerivativeFilter,sepiaFilter,invertFilter,saturationFilter,beautyFielter,sketchFilter,thresholdFilter];
// 初始化GPUImageView
_GPUImageView = [[GPUImageView alloc] initWithFrame:self.view.bounds];
[_camera addTarget:sketchFilter];
[sketchFilter addTarget:_GPUImageView];
_currentFilter = sketchFilter;
[self.view addSubview:_GPUImageView];
[_camera startCameraCapture];
}