1.废话前言[可略过]
前两天和一个做IOS的朋友在一起聊天,他现在做人脸识别.他告诉我说他们公司的同事说这个图片太丑了,每次到公司看到自己的照片整个人心情都不好了,手动撸一个滤镜吧.听完朋友的话,突然想到现在好多的美颜相机什么的,竟然可以把人拍得这么好看,加上自己以前做聊天的时候也有拍照录像,可是当时没有做滤镜处理.所以我想一窥滤镜的究竟,于是有了此文
2.GPUImage集成
虽说是要做滤镜处理,可我还是没有那么丧心病狂真是全部自己写啊,我到github上找到了一个很好的滤镜框架GPUImage
地址:https://github.com/wangyansnow/GPUImage
由于使用Cocoapods的的集成方式,过于简单,github上也有,这里略过,我主要讲怎么手动集成
1.点击我的Demo
地址:https://github.com/wangyansnow/WYImageFilter 下载之
2.把GPUImage整个文件夹拖到项目中
3.添加项目依赖的库
- CoreMedia
- CoreVideo
- OpenGLES
- AVFoundation
- QuartzCore
4.使用的时候导入#import "GPUImage.h"
3.简单用法
#import "GPUImage.h"
#import "ViewController.h"
#define VIDEO_RECT CGRectMake(40, 64, 240, 320)
@implementation ViewController
{
GPUImageStillCamera *_stillCamera; /// 照相机
GPUImageView *_frameView; /// 图像框
GPUImageBeautifyFilter *_beautifyFilter; /// 美颜滤镜
}
- (void)viewDidLoad {
[super viewDidLoad];
[self beautifyVideo];
}
- (void)beautifyVideo {
_stillCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionFront];
_stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
_beautifyFilter = [GPUImageBeautifyFilter new];
_frameView = [[GPUImageView alloc] initWithFrame:VIDEO_RECT];
[_beautifyFilter addTarget:_frameView];
[_stillCamera addTarget:_beautifyFilter];
[self.view addSubview:_frameView];
[_stillCamera startCameraCapture];
}
- (IBAction)takePhotoBtnClick {
[_stillCamera capturePhotoAsPNGProcessedUpToFilter:_beautifyFilter withCompletionHandler:^(NSData *processedPNG, NSError *error) {
if (error) NSLog(@"error = %@", error);
UIImage *image = [UIImage imageWithData:processedPNG];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:VIDEO_RECT];
imageView.image = image;
[self.view addSubview:imageView];
NSLog(@"image = %@", image);
NSLog(@"currentThread = %@", [NSThread currentThread]);
}];
}
@end
网友评论