日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

android 图像处理(黑白,模糊,浮雕,圆角,镜像,底片,油画,灰白,加旧)...

發(fā)布時(shí)間:2025/3/17 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 图像处理(黑白,模糊,浮雕,圆角,镜像,底片,油画,灰白,加旧)... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

圓角處理

public?static?Bitmap?getRoundedCornerBitmap(Bitmap?bitmap,?float?roundPx)?{Bitmap?output?=?Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),?Config.ARGB_8888);Canvas?canvas?=?new?Canvas(output);final?int?color?=?0xff424242;final?Paint?paint?=?new?Paint();final?Rect?rect?=?new?Rect(0,?0,?bitmap.getWidth(),?bitmap.getHeight());final?RectF?rectF?=?new?RectF(rect);paint.setAntiAlias(true);canvas.drawARGB(0,?0,?0,?0);paint.setColor(color);canvas.drawRoundRect(rectF,?roundPx,?roundPx,?paint);paint.setXfermode(new?PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(bitmap,?rect,?rect,?paint);return?output;}

這個(gè)就簡(jiǎn)單了,實(shí)際上是在原圖片上畫了一個(gè)圓角遮罩。對(duì)于paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));方法我剛看到也是一知半解Mode.SRC_IN參數(shù)是個(gè)畫圖模式,該類型是指只顯示兩層圖案的交集部分,且交集部位只顯示上層圖像。實(shí)際就是先畫了一個(gè)圓角矩形的過濾框,于是形狀有了,再將框中的內(nèi)容填充為圖片。該參數(shù)總過有十八種。

?

灰白處理

public?static?Bitmap?toGrayscale(Bitmap?bmpOriginal)?{int?width,?height;height?=?bmpOriginal.getHeight();width?=?bmpOriginal.getWidth();Bitmap?bmpGrayscale?=?Bitmap.createBitmap(width,?height,Bitmap.Config.RGB_565);Canvas?c?=?new?Canvas(bmpGrayscale);Paint?paint?=?new?Paint();ColorMatrix?cm?=?new?ColorMatrix();cm.setSaturation(0);ColorMatrixColorFilter?f?=?new?ColorMatrixColorFilter(cm);paint.setColorFilter(f);c.drawBitmap(bmpOriginal,?0,?0,?paint);return?bmpGrayscale;}

?這個(gè)也沒什么好說的,就是利用了ColorMatrix 類自帶的設(shè)置飽和度的方法setSaturation()。不過其方法內(nèi)部實(shí)現(xiàn)的更深一層是利用顏色矩陣的乘法實(shí)現(xiàn)的,對(duì)于顏色矩陣的乘法下面還有使用。

?

黑白處理

public?static?Bitmap?toHeibai(Bitmap?mBitmap)?{int?mBitmapWidth?=?0;int?mBitmapHeight?=?0;mBitmapWidth?=?mBitmap.getWidth();mBitmapHeight?=?mBitmap.getHeight();Bitmap?bmpReturn?=?Bitmap.createBitmap(mBitmapWidth,?mBitmapHeight,Bitmap.Config.ARGB_8888);int?iPixel?=?0;for?(int?i?=?0;?i?<?mBitmapWidth;?i++)?{for?(int?j?=?0;?j?<?mBitmapHeight;?j++)?{int?curr_color?=?mBitmap.getPixel(i,?j);int?avg?=?(Color.red(curr_color)?+?Color.green(curr_color)?+?Color.blue(curr_color))?/?3;if?(avg?>=?100)?{iPixel?=?255;}?else?{iPixel?=?0;}int?modif_color?=?Color.argb(255,?iPixel,?iPixel,?iPixel);bmpReturn.setPixel(i,?j,?modif_color);}}return?bmpReturn;}

其實(shí)看圖片效果就能看出來,這張圖片不同于灰白處理的那張,不同之處是灰白處理雖然沒有了顏色,但是黑白的程度層次依然存在,而此張圖片連層次都沒有了,只有兩個(gè)區(qū)別十分明顯的黑白顏色。實(shí)現(xiàn)的算法也很簡(jiǎn)單,對(duì)于每個(gè)像素的rgb值求平均數(shù),如果高于100算白色,低于100算黑色。不過感覺100這個(gè)標(biāo)準(zhǔn)值太大了,導(dǎo)致圖片白色區(qū)域太多,把它降低點(diǎn)可能效果會(huì)更好。

?

鏡像處理

?public?static?Bitmap?createReflectionImageWithOrigin(Bitmap?bitmap)?{final?int?reflectionGap?=?4;int?width?=?bitmap.getWidth();int?height?=?bitmap.getHeight();Matrix?matrix?=?new?Matrix();matrix.preScale(1,?-1);Bitmap?reflectionImage?=?Bitmap.createBitmap(bitmap,?0,?height?/?2,width,?height?/?2,?matrix,?false);Bitmap?bitmapWithReflection?=?Bitmap.createBitmap(width,(height?+?height?/?2),?Config.ARGB_8888);Canvas?canvas?=?new?Canvas(bitmapWithReflection);canvas.drawBitmap(bitmap,?0,?0,?null);Paint?deafalutPaint?=?new?Paint();canvas.drawRect(0,?height,?width,?height?+?reflectionGap,?deafalutPaint);canvas.drawBitmap(reflectionImage,?0,?height?+?reflectionGap,?null);Paint?paint?=?new?Paint();LinearGradient?shader?=?new?LinearGradient(0,?bitmap.getHeight(),?0,bitmapWithReflection.getHeight()?+?reflectionGap,?0x70ffffff,0x00ffffff,?TileMode.CLAMP);paint.setShader(shader);//?Set?the?Transfer?mode?to?be?porter?duff?and?destination?inpaint.setXfermode(new?PorterDuffXfermode(Mode.DST_IN));//?Draw?a?rectangle?using?the?paint?with?our?linear?gradientcanvas.drawRect(0,?height,?width,?bitmapWithReflection.getHeight()+?reflectionGap,?paint);return?bitmapWithReflection;}

記得去年android入門時(shí)做過gallery的倒影特效,當(dāng)時(shí)感覺很漂亮,想著需要作出反轉(zhuǎn)和傾斜就可以了,原來他也是這么做的。原理就是將原圖片反轉(zhuǎn)一下,調(diào)整一 下它的顏色作出倒影效果,再將兩張圖片續(xù)加在一起,不過如果在反轉(zhuǎn)的同時(shí)再利用Matrix加上一些傾斜角度就更好了,不過那樣做的話加工后的圖片的高度需要同比例計(jì)算出來,不能簡(jiǎn)單的相加了,否則就圖片大小就容不下現(xiàn)有的像素內(nèi)容。

?

加舊處理

public?static?Bitmap?testBitmap(Bitmap?bitmap)?{Bitmap?output?=?Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),?Config.RGB_565);Canvas?canvas?=?new?Canvas(output);Paint?paint?=?new?Paint();ColorMatrix?cm?=?new?ColorMatrix();float[]?array?=?{?1,?0,?0,?0,?50,?0,?1,?0,?0,?50,?0,?0,?1,?0,?0,?0,?0,0,?1,?0?};cm.set(array);paint.setColorFilter(new?ColorMatrixColorFilter(cm));canvas.drawBitmap(bitmap,?0,?0,?paint);return?output;}

?其實(shí)每張圖片的存儲(chǔ)都是存的每個(gè)像素的rgba值,而對(duì)其操作的時(shí)候又將其四個(gè)數(shù)值定位一個(gè)5行1列的矩陣,最后一行值為1,這樣一來利用矩陣對(duì)其操作確實(shí)方便了好多,矩陣的乘法可以輕松的實(shí)現(xiàn)某個(gè)或全部分量按比例或加常熟的增加或減少。 比如現(xiàn)有一張圖片,其每個(gè)point的rgba值為{100,100,100,255}也就是灰色全圖,我們希望其紅色部位增加一倍,剩余部分增加十。就可以將其值虛擬為五行一列矩陣:{100 ,100,100,255,1} 再讓這個(gè)矩陣:{2,0,0,0,0換行 0,1,0,0,10換行 0,0,1,0,10換行 0,,0,0,1,10} 乘以它。得到{ 200,110,100,100} 。 這個(gè)泛黃照片的處理算法原理就是讓每個(gè)像素點(diǎn)rg值增加50,rg值相混合就得到了黃色。

?

浮雕處理

?public?static?Bitmap?toFuDiao(Bitmap?mBitmap)?{int?mBitmapWidth?=?0;int?mBitmapHeight?=?0;mBitmapWidth?=?mBitmap.getWidth();mBitmapHeight?=?mBitmap.getHeight();Bitmap?bmpReturn?=?Bitmap.createBitmap(mBitmapWidth,?mBitmapHeight,Bitmap.Config.RGB_565);int?preColor?=?0;int?prepreColor?=?0;preColor?=?mBitmap.getPixel(0,?0);for?(int?i?=?0;?i?<?mBitmapWidth;?i++)?{for?(int?j?=?0;?j?<?mBitmapHeight;?j++)?{int?curr_color?=?mBitmap.getPixel(i,?j);int?r?=?Color.red(curr_color)?-?Color.red(prepreColor)?+?127;int?g?=?Color.green(curr_color)?-?Color.red(prepreColor)?+?127;int?b?=?Color.green(curr_color)?-?Color.blue(prepreColor)?+?127;int?a?=?Color.alpha(curr_color);int?modif_color?=?Color.argb(a,?r,?g,?b);bmpReturn.setPixel(i,?j,?modif_color);prepreColor?=?preColor;preColor?=?curr_color;}}Canvas?c?=?new?Canvas(bmpReturn);Paint?paint?=?new?Paint();ColorMatrix?cm?=?new?ColorMatrix();cm.setSaturation(0);ColorMatrixColorFilter?f?=?new?ColorMatrixColorFilter(cm);paint.setColorFilter(f);c.drawBitmap(bmpReturn,?0,?0,?paint);return?bmpReturn;}

觀察浮雕就不難發(fā)現(xiàn),其實(shí)浮雕的特點(diǎn)就是在顏色有跳變的地方就刻條痕跡。127,127,127為深灰色,近似于石頭的顏色,此處取該顏色為底色。算法是將上一個(gè)點(diǎn)的rgba值減去當(dāng)前點(diǎn)的rgba值然后加上127得到當(dāng)前點(diǎn)的顏色。

?

油畫處理

?public?static?Bitmap?toYouHua(Bitmap?bmpSource)?{Bitmap?bmpReturn?=?Bitmap.createBitmap(bmpSource.getWidth(),bmpSource.getHeight(),?Bitmap.Config.RGB_565);int?color?=?0;int?Radio?=?0;int?width?=?bmpSource.getWidth();int?height?=?bmpSource.getHeight();Random?rnd?=?new?Random();int?iModel?=?10;int?i?=?width?-?iModel;while?(i?>?1)?{int?j?=?height?-?iModel;while?(j?>?1)?{int?iPos?=?rnd.nextInt(100000)?%?iModel;color?=?bmpSource.getPixel(i?+?iPos,?j?+?iPos);bmpReturn.setPixel(i,?j,?color);j?=?j?-?1;}i?=?i?-?1;}return?bmpReturn;}

贊一下這個(gè)算法,其實(shí)應(yīng)該說鄙視下自己,在看到效果圖的時(shí)候,我會(huì)先猜一下原理,但是這個(gè)始終沒有想出來。其實(shí)油畫因?yàn)槭怯卯嫻P畫的,彩筆畫的時(shí)候沒有那么精確會(huì)將本該這點(diǎn)的顏色滑到另一個(gè)點(diǎn)處。算法實(shí)現(xiàn)就是取一個(gè)一定范圍內(nèi)的隨機(jī)數(shù),每個(gè)點(diǎn)的顏色是該點(diǎn)減去隨機(jī)數(shù)坐標(biāo)后所得坐標(biāo)的顏色。

?

模糊處理

public?static?Bitmap?toMohu(Bitmap?bmpSource,?int?Blur)?{int?mode?=?5;Bitmap?bmpReturn?=?Bitmap.createBitmap(bmpSource.getWidth(),bmpSource.getHeight(),?Bitmap.Config.ARGB_8888);int?pixels[]?=?new?int[bmpSource.getWidth()?*?bmpSource.getHeight()];int?pixelsRawSource[]?=?new?int[bmpSource.getWidth()*?bmpSource.getHeight()?*?3];int?pixelsRawNew[]?=?new?int[bmpSource.getWidth()*?bmpSource.getHeight()?*?3];bmpSource.getPixels(pixels,?0,?bmpSource.getWidth(),?0,?0,bmpSource.getWidth(),?bmpSource.getHeight());for?(int?k?=?1;?k?<=?Blur;?k++)?{for?(int?i?=?0;?i?<?pixels.length;?i++)?{pixelsRawSource[i?*?3?+?0]?=?Color.red(pixels[i]);pixelsRawSource[i?*?3?+?1]?=?Color.green(pixels[i]);pixelsRawSource[i?*?3?+?2]?=?Color.blue(pixels[i]);}int?CurrentPixel?=?bmpSource.getWidth()?*?3?+?3;for?(int?i?=?0;?i?<?bmpSource.getHeight()?-?3;?i++)?{for?(int?j?=?0;?j?<?bmpSource.getWidth()?*?3;?j++)?{CurrentPixel?+=?1;int?sumColor?=?0;sumColor?=?pixelsRawSource[CurrentPixel-?bmpSource.getWidth()?*?3];sumColor?=?sumColor?+?pixelsRawSource[CurrentPixel?-?3];sumColor?=?sumColor?+?pixelsRawSource[CurrentPixel?+?3];sumColor?=?sumColor+?pixelsRawSource[CurrentPixel+?bmpSource.getWidth()?*?3];pixelsRawNew[CurrentPixel]?=?Math.round(sumColor?/?4);}}for?(int?i?=?0;?i?<?pixels.length;?i++)?{pixels[i]?=?Color.rgb(pixelsRawNew[i?*?3?+?0],pixelsRawNew[i?*?3?+?1],?pixelsRawNew[i?*?3?+?2]);}}bmpReturn.setPixels(pixels,?0,?bmpSource.getWidth(),?0,?0,bmpSource.getWidth(),?bmpSource.getHeight());return?bmpReturn;}

算法實(shí)現(xiàn)其實(shí)是取每三點(diǎn)的平均值做為當(dāng)前點(diǎn)顏色,這樣看上去就變得模糊了。這個(gè)算法是三點(diǎn)的平均值,如果能夠?qū)⒎秶鷶U(kuò)大,并且不是單純的平均值,而是加權(quán)平均肯定效果會(huì)更好。不過處理速度實(shí)在是太慢了,而Muzei這種軟件在處理的時(shí)候,不僅僅速度特別快,而且還有逐漸變模糊的變化過程,顯然人家不是用這種算法實(shí)現(xiàn)的。他們的實(shí)現(xiàn)方法正在猜測(cè)中,實(shí)現(xiàn)后也來更新。

轉(zhuǎn)載于:https://my.oschina.net/yolinfeng/blog/408074

總結(jié)

以上是生活随笔為你收集整理的android 图像处理(黑白,模糊,浮雕,圆角,镜像,底片,油画,灰白,加旧)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。