GPUImage首页投稿(暂停使用,暂停投稿)iOS Developer

GPUImage给相机丶视频丶图片添加滤镜

2016-07-13  本文已影响1323人  RainyHand

使用GPUImage实现简单的视频丶相机的滤镜是非常简单的,实现的基本原理是GPUImageStillCamera/GPUImageVideoCamera(照片/视频)捕获图像->GPUImageFilter滤镜->GPUImageView显示.
1.拍摄照片丶视频的时候添加滤镜
在拍摄视频和照片的时候可以添加单个或者一个Group滤镜

//开启前置摄像头640*480
self.videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionFront];
self.videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
self.videoCamera.horizontallyMirrorFrontFacingCamera = YES;
//用来显示的view
self.filterView = [[GPUImageView alloc] initWithFrame:self.view.frame];
self.filterView.center = self.view.center;
[self.view addSubview:self.filterView];
//开始获取视频
[self.videoCamera startCameraCapture];
//滤镜(此处用的是封装好的一个group)系统提供多种,可以将GPUImageBeautifyFilter更换
GPUImageBeautifyFilter *beautifyFilter = [[GPUImageBeautifyFilter alloc] init];
[self.videoCamera addTarget:beautifyFilter];
[beautifyFilter addTarget:self.filterView];

拍摄照片的时候用到GPUImageStillCamera代替GPUImageVideoCamera.两个均是GPUImageOutput的子类,用来作为数据源.
拍摄照片并且保存图片(AssetsLibrary/AssetsLibrary.h框架IOS9以后推出PhotoKit框架)

-(void)savePhoto{
[self.photoCamera capturePhotoAsJPEGProcessedUpToFilter:self.filterView withCompletionHandler:^(NSData *processedJPEG, NSError *error){
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageDataToSavedPhotosAlbum:processedJPEG metadata:self.photoCamera.currentCaptureMetadata completionBlock:^(NSURL *assetURL, NSError *error2)
{
 if (error2) {
    //错误
  }
else {
     //成功
 }
   }];
  }];
}

滤镜视频的保存用的是GPUImageMovieWriter类(参考落影loyinglin视频保存方式)

NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];

unlink([pathToMovie UTF8String]);

NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

_movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(640.0, 480.0)];

self.videoCamera.audioEncodingTarget = _movieWriter;

_movieWriter.encodingLiveVideo = YES;

[self.videoCamera startCameraCapture];

[_movieWriter startRecording];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[beautifyFilter removeTarget:_movieWriter];

[_movieWriter finishRecording];

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

 if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(pathToMovie)) {
[library writeVideoAtPathToSavedPhotosAlbum:movieURL completionBlock:^(NSURL *assetURL, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^{

if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"视频保存失败" message:nil
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"视频保存成功" message:nil
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
});
}];
}
else {
NSLog(@"error mssg)");
}
});

注:移除滤镜

[self.videoCamera removeAllTargets];
[self.videoCamera addTarget:self.filterView];

2.给静态图片添加滤镜
用GPUImagePicture处理源图像->添加GPUImageFilter滤镜->GPUImageView显示.

-(void)picture
{
GPUImageView *showView = [[GPUImageView alloc] initWithFrame:self.view.frame];
self.view = showView;
UIImage *inputImage = [UIImage imageNamed:@"1.jpg"];
//_showPicture处理源图片
_showPicture = [[GPUImagePicture alloc] initWithImage:inputImage];
//滤镜
_sepiaFilter = [[GPUImageBeautifyFilter alloc] init];
[_sepiaFilter forceProcessingAtSize:showView.sizeInPixels];
[_showPicture addTarget:_sepiaFilter];
[_sepiaFilter addTarget:showView];
[_showPicture processImage];
}

3.混合滤镜两种方式
1.直接添加多个滤镜

GPUImagePicture *finalyOne  = [[GPUImagePicture alloc]initWithImage:neadImage];
[finalyOne addTarget:passthroughFilter1];
[finalyOne processImage];
[finalyOne addTarget:passthroughFilter2];
[finalyOne processImage];

2.通过GPUImageFilterGroup,GPUImageFilterGroup是多个filter集合,terminalFilter是最终合成的滤镜,在group中添加或者删除是操作的terminalFilters这个数组,前边用的GPUImageBeautifyFilter就是基于此,里边实现美颜,磨皮等功能.此处粘贴其中部分代码,详细可直接下载GPUImageBeautifyFilter类,实现比较简单.

// First pass: face smoothing filter
_bilateralFilter = [[GPUImageBilateralFilter alloc] init];
_bilateralFilter.distanceNormalizationFactor = 4.0;
[self addFilter:_bilateralFilter];
// Second pass: edge detection
_cannyEdgeFilter = [[GPUImageCannyEdgeDetectionFilter alloc] init];
[self addFilter:_cannyEdgeFilter];
// Third pass: combination bilateral, edge detection and origin
_combinationFilter = [[GPUImageCombinationFilter alloc] init];
[self addFilter:_combinationFilter];
// Adjust HSB
_hsbFilter = [[GPUImageHSBFilter alloc] init];
[_hsbFilter adjustBrightness:1.1];
[_hsbFilter adjustSaturation:1.1];
[_bilateralFilter addTarget:_combinationFilter];
[_cannyEdgeFilter addTarget:_combinationFilter];
[_combinationFilter addTarget:_hsbFilter];
self.initialFilters = [NSArray arrayWithObjects:_bilateralFilter,_cannyEdgeFilter,_combinationFilter,nil];
self.terminalFilter = _hsbFilter;

刚开始接触GPUImage,功能很强大,希望懂得人指出错误,如果给些学习资料那是更好的.

上一篇下一篇

猜你喜欢

热点阅读