GPUImage

As a note: if you run into the error "Unknown class GPUImageView in Interface Builder" or the like when trying to build an interface with Interface Builder, you may need to add -ObjC to your Other Linker Flags in your project's build settings.

GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];

videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

GPUImageFilter *customFilter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"CustomShader"];

GPUImageView *filteredVideoView = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, viewWidth, viewHeight)];

// Add the view somewhere so it's visible

[videoCamera addTarget:customFilter];

[customFilter addTarget:filteredVideoView];

[videoCamera startCameraCapture];

Objective-C interface. This interface lets you define input sources for

images and video, attach filters in a chain, and send the resulting

processed image or video to the screen, to a UIImage, or to a movie on

disk.

Images or frames of video are uploaded from source objects, which are

subclasses of GPUImageOutput. These include GPUImageVideoCamera (for

live video from an iOS camera), GPUImageStillCamera (for taking photos

with the camera), GPUImagePicture (for still images), and GPUImageMovie

(for movies). Source objects upload still image frames to OpenGL ES as

textures, then hand those textures off to the next objects in the

processing chain.

Filters and other subsequent elements in the chain conform to the

GPUImageInput protocol, which lets them take in the supplied or

processed texture from the previous link in the chain and do something

with it. Objects one step further down the chain are considered targets,

and processing can be branched by adding multiple targets to a single

output or filter.

For example, an application that takes in live video from the camera,

converts that video to a sepia tone, then displays the video onscreen

would set up a chain looking something like the following:

GPUImageVideoCamera -> GPUImageSepiaFilter -> GPUImageView

GPUImage needs a few other frameworks to be linked into your

application, so you'll need to add the following as linked libraries in

your application target:

CoreMedia

CoreVideo

OpenGLES

AVFoundation

QuartzCore

Filtering live video

To filter live video from an iOS device's camera, you can use code like the following:

GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];

videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

GPUImageFilter *customFilter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"CustomShader"];

GPUImageView *filteredVideoView = [[GPUImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, viewWidth, viewHeight)];

// Add the view somewhere so it's visible

[videoCamera addTarget:customFilter];

[customFilter addTarget:filteredVideoView];

[videoCamera startCameraCapture];

Capturing and filtering a still photo

To capture and filter still photos, you can use a process similar to

the one for filtering video. Instead of a GPUImageVideoCamera, you use a

GPUImageStillCamera:

stillCamera = [[GPUImageStillCamera alloc] init];

stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

filter = [[GPUImageGammaFilter alloc] init];

[stillCamera addTarget:filter];

GPUImageView *filterView = (GPUImageView *)self.view;

[filter addTarget:filterView];

[stillCamera startCameraCapture];

This will give you a live, filtered feed of the still camera's

preview video. Note that this preview video is only provided on iOS 4.3

and higher, so you may need to set that as your deployment target if you

wish to have this functionality.

Once you want to capture a photo, you use a callback block like the following:

[stillCamera capturePhotoProcessedUpToFilter:filter withCompletionHandler:^(UIImage *processedImage, NSError *error){

NSData *dataForJPEGFile = UIImageJPEGRepresentation(processedImage, 0.8);

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSError *error2 = nil;

if (![dataForJPEGFile writeToFile:[documentsDirectory stringByAppendingPathComponent:@"FilteredPhoto.jpg"] options:NSAtomicWrite error:&error2])

{

return;

}

}];

Processing a still image

There are a couple of ways to process a still image and create a

result. The first way you can do this is by creating a still image

source object and manually creating a filter chain:

UIImage *inputImage = [UIImage imageNamed:@"Lambeau.jpg"];

GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];

GPUImageSepiaFilter *stillImageFilter = [[GPUImageSepiaFilter alloc] init];

[stillImageSource addTarget:stillImageFilter];

[stillImageFilter useNextFrameForImageCapture];

[stillImageSource processImage];

UIImage *currentFilteredVideoFrame = [stillImageFilter imageFromCurrentFramebuffer];

The following is an example of how you would load a sample movie,

pass it through a pixellation filter, then record the result to disk as a

480 x 640 h.264 movie:

movieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];

pixellateFilter = [[GPUImagePixellateFilter alloc] init];

[movieFile addTarget:pixellateFilter];

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

unlink([pathToMovie UTF8String]);

NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];

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

[pixellateFilter addTarget:movieWriter];

movieWriter.shouldPassthroughAudio = YES;

movieFile.audioEncodingTarget = movieWriter;

[movieFile enableSynchronizedEncodingUsingMovieWriter:movieWriter];

[movieWriter startRecording];

[movieFile startProcessing];

GPUImage混合使用

首先在.h文件声明:

GPUImagePicture *staticPicture;

GPUImageOutput * brightnessFilter;//亮度

GPUImageOutput * contrastFilter;//对比度

NSMutableArray*arrayTemp;

UISlider*brightnessSlider;

UISlider* contrastSlider;

在.m文件中viewdidload中

UIImage * image = [UIImage imageNamed:@"sample1.jpg"];

staticPicture=[[GPUImagePicture alloc] initWithImage:image smoothlyScaleOutput:YES];

//亮度

brightnessFilter =[[GPUImageBrightnessFilter alloc] init];

CGRect mainScreenFrame=[[UIScreen mainScreen] applicationFrame];

GPUImageView* GPUView =[[GPUImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

[brightnessFilter forceProcessingAtSize:GPUView.sizeInPixels];

self.view=GPUView;

[brightnessFilter addTarget:GPUView];

brightnessSlider= [[UISlider alloc] initWithFrame:CGRectMake(25.0, mainScreenFrame.size.height -250, mainScreenFrame.size.width -50.0,40.0)];

[brightnessSlider addTarget:self action:@selector(updateSliderValue:) forControlEvents:UIControlEventValueChanged];

brightnessSlider.autoresizingMask= UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin;

brightnessSlider.minimumValue=0.0;

brightnessSlider.maximumValue=1.0;

brightnessSlider.tag=10;

brightnessSlider.value=0.0;

[GPUView addSubview:brightnessSlider];

[staticPicture processImage];

//对比度

contrastFilter =[[GPUImageContrastFilter alloc]init];

[contrastFilter forceProcessingAtSize:GPUView.sizeInPixels];

[contrastFilter addTarget:GPUView];

contrastSlider= [[UISlider alloc] initWithFrame:CGRectMake(25.0, mainScreenFrame.size.height -190, mainScreenFrame.size.width -50.0,40.0)];

[contrastSlider addTarget:self action:@selector(updateSliderValue:) forControlEvents:UIControlEventValueChanged];

contrastSlider.autoresizingMask= UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin;

contrastSlider.minimumValue=0.0;

contrastSlider.maximumValue=1.0;

contrastSlider.tag=11;

contrastSlider.value=0.0;

[GPUView addSubview:contrastSlider];

[staticPicture processImage];

//组合,这就是把你要添加的所有滤镜效果放进数组

[staticPicture addTarget:brightnessFilter];

staticPicture addTarget:contrastFilter];

arrayTemp=[[NSMutableArray alloc]initWithObjects:brightnessFilter,contrastFilter,nil];

pipeline= [[GPUImageFilterPipeline alloc]initWithOrderedFilters:arrayTemp input:staticPicture output:(GPUImageView*)self.view];

添加方法,用UISlider将调色做成可视化

- (void)updateSliderValue:(UISlider *)sender

{

NSInteger index= sender.tag -10;switch(index)

{case0:

{

GPUImageBrightnessFilter*GPU = (GPUImageBrightnessFilter *)brightnessFilter;

[GPU setBrightness:brightnessSlider.value];

[staticPicture processImage];

NSLog(@"亮度 =  %f",brightnessSlider.value);

}break;case1:{

GPUImageContrastFilter*GPU = (GPUImageContrastFilter *)contrastFilter;

[GPU setContrast:contrastSlider.value];

[staticPicture processImage];

NSLog(@"对比度 =  %f",contrastSlider.value);

}default:break;

}

}

https://github.com/BradLarson/GPUImage

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

推荐阅读更多精彩内容

  • 人:工程师 工具:建筑 方法:引导当地居民用自己已有的技术加入到再生产中 产品:民宿 国土部:允许深度贫困地区以出...
    刘冰杰阅读 256评论 0 0
  • java语言中,使用jdk提供的方法写文件一般有三种方式,关键类分别为FileOutputStream,Buffe...
    high_m阅读 311评论 0 0
  • 环境: win8 纯净环境(无任何开发语言工具的安装) 1.下载安装JAVA,Python,Android sdk...
    第八共同体阅读 302评论 0 0
  • 前段时间学习了小六洋葱阅读法,现在整理分享给大家。小六认为阅读就像吃饭睡觉一样,是我们每天不得不做的事情,应当融入...
    0edb135f3eb9阅读 18,147评论 19 307