常見的圖像特效處理算法(基于android)

素材

圓角處理

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; }
實際就是先畫了一個圓角矩形的過濾框缴啡,于是形狀有了处铛,再將框中的內(nèi)容填充為圖片。對于Mode.SRC_IN的講解
輸出效果:

圓角

灰白處理

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; }
就是利用了ColorMatrix 類自帶的設(shè)置飽和度的方法setSaturation()。不過其方法內(nèi)部實現(xiàn)的更深一層是利用顏色矩陣的乘法實現(xiàn)的。
輸出效果:

灰白

黑白處理

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; }
闕值變換。其實看圖片效果就能看出來,這張圖片不同于灰白處理的那張,不同之處是灰白處理雖然沒有了顏色晤斩,但是黑白的程度層次依然存在,而此張圖片連層次都沒有了姆坚,只有兩個區(qū)別十分明顯的黑白顏色澳泵。實現(xiàn)的算法也很簡單,對于每個像素的rgb值求平均數(shù)兼呵,如果高于100算白色兔辅,低于100算黑色腊敲。
輸出效果:

黑白

鏡像處理

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 in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint); return bitmapWithReflection; }
就是將原圖片反轉(zhuǎn)一下,調(diào)整一 下它的顏色作出倒影效果维苔,再將兩張圖片續(xù)加在一起碰辅。
輸出效果:

鏡像

泛黃處理

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; }
其實每張圖片的存儲都是存的每個像素的rgba值,而對其操作的時候又將其四個數(shù)值定位一個5行1列的矩陣介时,最后一行值為1没宾,這樣一來利用矩陣對其操作確實方便了好多,矩陣的乘法可以輕松的實現(xiàn)某個或全部分量按比例或加常熟的增加或減少沸柔。 比如現(xiàn)有一張圖片循衰,其每個point的rgba值為{100,100勉失,100,255}也就是灰色全圖羹蚣,我們希望其紅色部位增加一倍,剩余部分增加十乱凿。就可以將其值虛擬為五行一列矩陣:{100 ,100,100,255,1} 再讓這個矩陣:{2,0,0,0,0換行 0,1,0,0,10換行 0,0,1,0,10換行 0,,0,0,1,10} 乘以它咽弦。得到{ 200,110,100,100} 徒蟆。 這個泛黃照片的處理算法原理就是讓每個像素點(diǎn)rg值增加50,rg值相混合就得到了黃色型型。
輸出效果:

泛黃

哈哈鏡處理

jintArray Java_com_spore_meitu_jni_ImageUtilEngine_toHahajing (JNIEnv* env,jobject thiz, jintArray buf, jint width, jint height,jint centerX, jint centerY, jint radius, jfloat multiple) { jint * cbuf; cbuf = (*env)->GetIntArrayElements(env, buf, 0); int newSize = width * height; jint rbuf[newSize]; float xishu = multiple; int real_radius = (int)(radius / xishu); int i = 0, j = 0; for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { int curr_color = cbuf[j * width + i]; int pixR = red(curr_color); int pixG = green(curr_color); int pixB = blue(curr_color); int pixA = alpha(curr_color); int newR = pixR; int newG = pixG; int newB = pixB; int newA = pixA; int distance = (int) ((centerX - i) * (centerX - i) + (centerY - j) * (centerY - j)); if (distance < radius * radius) { int src_x = (int) ((float) (i - centerX) / xishu); int src_y = (int) ((float) (j - centerY) / xishu); src_x = (int)(src_x * (sqrt(distance) / real_radius)); src_y = (int)(src_y * (sqrt(distance) / real_radius)); src_x = src_x + centerX; src_y = src_y + centerY; int src_color = cbuf[src_y * width + src_x]; newR = red(src_color); newG = green(src_color); newB = blue(src_color); newA = alpha(src_color); } newR = min(255, max(0, newR)); newG = min(255, max(0, newG)); newB = min(255, max(0, newB)); newA = min(255, max(0, newA)); int modif_color = ARGB(newA, newR, newG, newB); rbuf[j * width + i] = modif_color; } } jintArray result = (*env)->NewIntArray(env, newSize); (*env)->SetIntArrayRegion(env, result, 0, newSize, rbuf); (*env)->ReleaseIntArrayElements(env, buf, cbuf, 0); return result; }
根據(jù)哈哈鏡的半徑段审,以中心點(diǎn)為圓心,每個像素點(diǎn)的坐標(biāo)位移并擴(kuò)展闹蒜,離中心點(diǎn)越近的就擴(kuò)展越大寺枉。
輸出效果:

哈哈鏡

放大鏡處理

jintArray Java_com_spore_meitu_jni_ImageUtilEngine_toFangdajing (JNIEnv* env,jobject thiz, jintArray buf, jint width, jint height,jint centerX, jint centerY, jint radius, jfloat multiple) { jint * cbuf; cbuf = (*env)->GetIntArrayElements(env, buf, 0); int newSize = width * height; jint rbuf[newSize]; float xishu = multiple; int real_radius = (int)(radius / xishu); int i = 0, j = 0; for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { int curr_color = cbuf[j * width + i]; int pixR = red(curr_color); int pixG = green(curr_color); int pixB = blue(curr_color); int pixA = alpha(curr_color); int newR = pixR; int newG = pixG; int newB = pixB; int newA = pixA; int distance = (int) ((centerX - i) * (centerX - i) + (centerY - j) * (centerY - j)); if (distance < radius * radius) { int src_x = (int)((float)(i - centerX) / xishu + centerX); int src_y = (int)((float)(j - centerY) / xishu + centerY); int src_color = cbuf[src_y * width + src_x]; newR = red(src_color); newG = green(src_color); newB = blue(src_color); newA = alpha(src_color); } newR = min(255, max(0, newR)); newG = min(255, max(0, newG)); newB = min(255, max(0, newB)); newA = min(255, max(0, newA)); int modif_color = ARGB(newA, newR, newG, newB); rbuf[j * width + i] = modif_color; } } jintArray result = (*env)->NewIntArray(env, newSize); (*env)->SetIntArrayRegion(env, result, 0, newSize, rbuf); (*env)->ReleaseIntArrayElements(env, buf, cbuf, 0); return result; }

浮雕處理

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; }
其實浮雕的特點(diǎn)就是在顏色有跳變的地方就刻條痕跡。127绷落,127,127為深灰色姥闪,近似于石頭的顏色,此處取該顏色為底色砌烁。算法是將上一個點(diǎn)的rgba值減去當(dāng)前點(diǎn)的rgba值然后加上127得到當(dāng)前點(diǎn)的顏色筐喳。
輸出效果:

浮雕

底片處理

jintArray Java_com_spore_meitu_jni_ImageUtilEngine_toDipian (JNIEnv* env,jobject thiz, jintArray buf, jint width, jint height) { jint * cbuf; cbuf = (*env)->GetIntArrayElements(env, buf, 0); int newSize = width * height; jint rbuf[newSize]; int count = 0; int preColor = 0; int prepreColor = 0; int color = 0; preColor = cbuf[0]; int i = 0; int j = 0; int iPixel = 0; for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { int curr_color = cbuf[j * width + i]; int r = 255 - red(curr_color); int g = 255 - green(curr_color); int b = 255 - blue(curr_color); int a = alpha(curr_color); int modif_color = ARGB(a, r, g, b); rbuf[j * width + i] = modif_color; } } jintArray result = (*env)->NewIntArray(env, newSize); (*env)->SetIntArrayRegion(env, result, 0, newSize, rbuf); (*env)->ReleaseIntArrayElements(env, buf, cbuf, 0); return result; }
輸出效果:

底片

油畫處理

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; }
油畫因為是用畫筆畫的,彩筆畫的時候沒有那么精確會將本該這點(diǎn)的顏色滑到另一個點(diǎn)處函喉。算法實現(xiàn)就是取一個一定范圍內(nèi)的隨機(jī)數(shù)避归,每個點(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; }
取每三點(diǎn)的平均值做為當(dāng)前點(diǎn)顏色管呵。真正的高斯模糊應(yīng)該取周圍點(diǎn)點(diǎn)加權(quán)平均梳毙,而且權(quán)重符合正態(tài)分布。不過處理速度實在是太慢了捐下,對于及時性較強(qiáng)的高斯模糊應(yīng)該是用opengl著色實現(xiàn)渲染账锹。
輸出效果:

模糊

注:以上算法并非全部原創(chuàng)堂氯,但是很早以前接觸的,實在找不到出處了牌废,致歉咽白。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市鸟缕,隨后出現(xiàn)的幾起案子晶框,更是在濱河造成了極大的恐慌,老刑警劉巖懂从,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件授段,死亡現(xiàn)場離奇詭異,居然都是意外死亡番甩,警方通過查閱死者的電腦和手機(jī)侵贵,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來缘薛,“玉大人窍育,你說我怎么就攤上這事⊙珉剩” “怎么了漱抓?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長恕齐。 經(jīng)常有香客問我乞娄,道長,這世上最難降的妖魔是什么显歧? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任仪或,我火速辦了婚禮,結(jié)果婚禮上士骤,老公的妹妹穿的比我還像新娘范删。我一直安慰自己,他們只是感情好敦间,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布瓶逃。 她就那樣靜靜地躺著,像睡著了一般廓块。 火紅的嫁衣襯著肌膚如雪厢绝。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天带猴,我揣著相機(jī)與錄音昔汉,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛靶病,可吹牛的內(nèi)容都是我干的会通。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼娄周,長吁一口氣:“原來是場噩夢啊……” “哼涕侈!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起煤辨,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤裳涛,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后众辨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體端三,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年鹃彻,在試婚紗的時候發(fā)現(xiàn)自己被綠了郊闯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡蛛株,死狀恐怖团赁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情泳挥,我是刑警寧澤然痊,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站屉符,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏锹引。R本人自食惡果不足惜矗钟,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望嫌变。 院中可真熱鬧吨艇,春花似錦、人聲如沸腾啥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽倘待。三九已至疮跑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間凸舵,已是汗流浹背祖娘。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留啊奄,地道東北人渐苏。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓掀潮,卻偏偏與公主長得像,于是被迫代替她去往敵國和親琼富。 傳聞我的和親對象是個殘疾皇子仪吧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

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