Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 圖像處理大全:圓角、黑白、模糊、浮雕、哈哈鏡等十一種特效實作

編輯:關於 Android 程式設計

前陣子在 GitHub 上無意間挖到一份相當完整的圖像處理原始碼(可惜連結已經找不回來了),閒著沒事就整理出來跟大家分享,這裡也要感謝原作者的無私開源。

至於圖片的翻轉、傾斜、縮放這類常見操作網路上已經不勝枚舉,多半都是直接呼叫 Matrix 的現成方法,這裡就不再贅述。我們直接進入正題。

原圖:

一、圓角處理

效果:

程式碼:

 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; }


原理解析:

概念其實很單純——先在原圖上畫一個圓角矩形當作遮罩。這裡的 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)) 是我初次接觸時比較陌生的部分。Mode.SRC_IN 是一種繪圖混合模式,代表只顯示兩層圖案的交集,且交集處僅呈現上層圖像。換句話說,就是先畫出圓角矩形決定形狀,再把框內填滿圖片內容。PorterDuff.Mode 總共有十八種參數(如 ADD、CLEAR、DST、DST_ATOP、DST_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 類別自帶的 setSaturation() 方法把飽和度設為 0,就能得到灰階影像。值得注意的是,它底層其實是透過顏色矩陣的乘法來實現的,而矩陣乘法在后面幾個範例裡還會繼續出現。

三、黑白處理

效果:

程式碼:

 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; }

原理解析:

仔細對比就能發現,這張黑白圖和前面的灰白圖不同:灰白處理雖然去掉了顏色,但明暗層次仍在;而這張圖連層次都沒了,只剩下截然不同的純黑與純白。實作方式很直覺——把每個像素的 RGB 值取平均,高於 100 算白色,低於 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; }

原理解析:

還記得剛入門 Android 時做過 Gallery 的倒影特效,當時覺得相當漂亮,心想只要做出翻轉和傾斜就行了,沒想到原理正是如此。做法就是把原圖上下反轉,調整顏色營造出水面倒影的漸層感,再把兩張圖拼接起來。若想更逼真,可以在反轉的同時用 Matrix 加上一點傾斜角度,不過那樣就必須按比例重新計算合成後的高度,否則畫布會裝不下多餘的像素。

五、加舊(泛黃)處理

效果:

程式碼:

 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 值,操作時我們把這四個值視為一個 5 行 1 列的矩陣,最後一列固定為 1,如此一來用矩陣乘法就能輕鬆地讓某個或全部色彩分量按比例或常數增減。舉例來說,若某像素的 RGBA 為 {100,100,100,255}(整片灰),我們希望紅色加倍、其餘各加 10,就把它虛擬成 {100,100,100,255,1},再乘上對應的變換矩陣即可得到 {200,110,100,100}。泛黃照片的祕然就在於讓每個像素的 R、G 值各增加 50,紅綠混合後自然呈現出舊照片特有的暖黃色調。

如圖:

六、哈哈鏡處理

效果:

程式碼:

 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; } 

原理解析:

一直很好奇,單純的圖像處理為什麼作者要特地寫進 JNI 那層,速度到底能快多少?純 C 的程式碼實在懶得細看,等有心情再回來補充。就我推敲,原理應該是以哈哈鏡半徑為圓心,將每個像素的座標做位移與擴展,離中心越近的像素就被拉伸得越誇張。

七、放大鏡處理

直接上程式碼:

 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; }

原理解析:

觀察浮雕效果不難發現,它的特徵是在顏色發生跳變的地方刻出痕跡。127,127,127 是接近石頭質感的深灰色,這裡就取它當底色。演算法是將前一點的 RGBA 值減去當前點的值再加上 127,得到當前點的新顏色,藉此凸顯邊緣輪廓。

九、底片處理

效果:

程式碼:

 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); LOGE(Bitmap Buffer %d %d,cbuf[0],cbuf[1]); 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; }

原理解析:

演算法就是把每個點的 RGB 值取 255 的差值,出來的成品確實就是我們熟悉的底片風格,只不過我還沒完全想通為什麼這樣運算就能模擬底片,等搞懂了再回來更新。

十、油畫處理

效果:

程式碼:

 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; }

原理解析:

這個演算法值得按個讚,也順便讓我自嘲一下——看到效果圖時我習慣先猜原理,偏偏這個怎麼都想不出來。想想油畫是用畫筆一筆一筆塗出來的,運筆不會那麼精準,顏色難免會從該在的位置滑到隔壁去。於是演算法就取一個範圍內的隨機數,讓每個點採用「該點減去隨機偏移後的座標」的顏色,模擬出那種手繪暈染的質感。

十一、模糊處理

效果:

程式碼:

 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; } 

原理解析:

這個演算法其實是取周圍三點的平均值當作當前點的顏色,如此一來影像看起來就模糊了。目前只用了簡單的三點平均,如果把取樣範圍擴大,再改採加權平均而非單純平均,效果應該會更好。只不過處理速度實在太慢,而像 Muzei 這種軟體不僅運算飛快,還能呈現漸進式由清晰到模糊的過渡,顯然用的不是這種暴力演算法。至於它們背後的實作方式,我還在猜測中,等有結果再來更新。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved