GPUImage給相機丶視頻丶圖片添加濾鏡

使用GPUImage實現(xiàn)簡單的視頻丶相機的濾鏡是非常簡單的,實現(xiàn)的基本原理是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)系統(tǒng)提供多種,可以將GPUImageBeautifyFilter更換
GPUImageBeautifyFilter *beautifyFilter = [[GPUImageBeautifyFilter alloc] init];
[self.videoCamera addTarget:beautifyFilter];
[beautifyFilter addTarget:self.filterView];

拍攝照片的時候用到GPUImageStillCamera代替GPUImageVideoCamera.兩個均是GPUImageOutput的子類,用來作為數(shù)據(jù)源.
拍攝照片并且保存圖片(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類(參考落影l(fā)oyinglin視頻保存方式)

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.給靜態(tài)圖片添加濾鏡
用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這個數(shù)組,前邊用的GPUImageBeautifyFilter就是基于此,里邊實現(xiàn)美顏,磨皮等功能.此處粘貼其中部分代碼,詳細可直接下載GPUImageBeautifyFilter類,實現(xiàn)比較簡單.

// 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,功能很強大,希望懂得人指出錯誤,如果給些學(xué)習(xí)資料那是更好的.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末剩彬,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子惶翻,更是在濱河造成了極大的恐慌,老刑警劉巖溉苛,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件乐设,死亡現(xiàn)場離奇詭異,居然都是意外死亡想邦,警方通過查閱死者的電腦和手機两残,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進店門永毅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人人弓,你說我怎么就攤上這事沼死。” “怎么了崔赌?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵意蛀,是天一觀的道長耸别。 經(jīng)常有香客問我,道長县钥,這世上最難降的妖魔是什么秀姐? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮若贮,結(jié)果婚禮上省有,老公的妹妹穿的比我還像新娘。我一直安慰自己兜看,他們只是感情好锥咸,可當(dāng)我...
    茶點故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布狭瞎。 她就那樣靜靜地躺著细移,像睡著了一般。 火紅的嫁衣襯著肌膚如雪熊锭。 梳的紋絲不亂的頭發(fā)上弧轧,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天,我揣著相機與錄音碗殷,去河邊找鬼精绎。 笑死,一個胖子當(dāng)著我的面吹牛锌妻,可吹牛的內(nèi)容都是我干的代乃。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼仿粹,長吁一口氣:“原來是場噩夢啊……” “哼搁吓!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起吭历,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤堕仔,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后晌区,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體摩骨,經(jīng)...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年朗若,在試婚紗的時候發(fā)現(xiàn)自己被綠了恼五。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡哭懈,死狀恐怖灾馒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情银伟,我是刑警寧澤你虹,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布绘搞,位于F島的核電站,受9級特大地震影響傅物,放射性物質(zhì)發(fā)生泄漏夯辖。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一董饰、第九天 我趴在偏房一處隱蔽的房頂上張望蒿褂。 院中可真熱鬧,春花似錦卒暂、人聲如沸啄栓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽昙楚。三九已至,卻和暖如春诈嘿,著一層夾襖步出監(jiān)牢的瞬間堪旧,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工奖亚, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留淳梦,地道東北人。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓昔字,卻偏偏與公主長得像爆袍,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子作郭,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫陨囊、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,118評論 4 61
  • KVO Key-Value Observing 鍵值監(jiān)聽 KVO是一個觀察者模式所坯。觀察一個對象的屬性谆扎,注冊一個指定...
    HHHHHHHHHHD閱讀 5,373評論 2 55
  • 追來的很累堂湖,強求的不美 想你的人,總會主動找你 不想你的人状土,找了也是不睬不理无蜂。 一直的低三下四,只能貶低了自己蒙谓;再...
    初美靜靜閱讀 302評論 0 0
  • <接上期一方科幻 置換(上)> 上期我們講到了2217年12月21日發(fā)生宇宙置換現(xiàn)象前5年斥季,1年,2小時的時候地球...
    更好時代閱讀 169評論 0 0
  • 我們經(jīng)常會用到StringBuilder或StringBuffer類的append()方法,然而這里有個坑酣倾。 多了...
    lsh的學(xué)習(xí)筆記閱讀 2,787評論 0 0