編輯:關於 Android 程式設計
這篇記錄一個 Android 圖像處理的練習專案,把幾個經典的濾鏡演算法用 Java 實作一遍,並比對與 Photoshop 輸出的差異。
整體流程是:圖片讀入後先把像素全部取出放進陣列,再對陣列做運算,最後寫回新的 Bitmap。
· 灰階化:以 0.3×R + 0.59×G + 0.11×B 加權(註:常見寫法為 0.299/0.587/0.114,這裡取整後的近似值)。
· 反色:以 255 減去原像素值。
· 懷舊(復古)效果:使用 0.393×R + 0.769×G + 0.189×B 這組係數。
· 高斯模糊:以 3×3 矩陣做卷積,算子為 1/16 × {1,2,1, 2,4,2, 1,2,1}。因為每個像素都帶權重,模糊之餘仍能保留邊緣。
· 拉普拉斯銳化:3×3 算子 {1,1,1, 1,-8,1, 1,1,1}。
· 素描效果:參考 Photoshop 的圖層概念——第一層是灰階圖,第二層是灰階圖反相,接著做最小值濾波,最後兩層疊加。
圖層疊加採「顏色減淡」模式:以灰階圖為基色、處理過的圖層為混合色,結果色 = 基色 + 基色 × 混合色 / (255 − 混合色)。這個公式會讓基色往變亮的方向發展,對比度隨之提高,也就有了鉛筆素描的味道。
困難主要在於對 Bitmap 在記憶體中的儲存格式不夠熟悉,以及 Canvas 的部分屬性、ColorMatrix 都是第一次接觸,需要邊查 API 邊試。
收穫是對 Bitmap 有了較深的理解,也對 Android 平台繪圖的基礎建立了一次性的全貌認識,細節部分則養成了查文件的習慣。
懷舊效果:
public static Bitmap oldbmp(Bitmap bmp) {
int width = bmp.getWidth();
int height = bmp.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
int pixColor, pixR, pixG, pixb;
int newColor, newr, newg, newb;
int[] pixs = new int[width * height];
bmp.getPixels(pixs, 0, width, 0, 0, width, height); // 取出像素到陣列
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixColor = pixs[i * width + j];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixb = Color.blue(pixColor);
newr = (int) (0.393 * pixR + 0.769 * pixG + 0.189 * pixb);
newg = (int) (0.349 * pixR + 0.686 * pixG + 0.168 * pixb);
newb = (int) (0.272 * pixR + 0.534 * pixG + 0.131 * pixb);
// 注意鉗制範圍,否則會溢出成負數或錯誤顏色
newColor = Color.argb(255,
newr > 255 ? 255 : newr,
newg > 255 ? 255 : newg,
newb > 255 ? 255 : newb);
pixs[i * width + j] = newColor;
}
}
bitmap.setPixels(pixs, 0, width, 0, 0, width, height);
return bitmap;
}
灰階化(用 ColorMatrix 一次搞定,比逐像素運算快得多):
public static Bitmap gray(Bitmap bmp) {
Bitmap bitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
float[] data = {
0.3f, 0.59f, 0.11f, 0, 0,
0.3f, 0.59f, 0.11f, 0, 0,
0.3f, 0.59f, 0.11f, 0, 0,
0, 0, 0, 1, 0
};
ColorMatrix colorMatrix = new ColorMatrix(data);
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
canvas.drawBitmap(bmp, 0, 0, paint);
return bitmap;
}
高斯模糊(3×3 卷積):
for (int i = 1; i < height - 1; i++) {
for (int j = 1; j < width - 1; j++) {
count = 0;
newr = newg = newb = 0;
for (int m = -1; m < 2; m++) {
for (int n = -1; n < 2; n++) {
pixColor = pixs[(i + m) * width + j + n];
r = Color.red(pixColor);
g = Color.green(pixColor);
b = Color.blue(pixColor);
newr += r * data[count]; // 與算子逐格相乘
newg += g * data[count];
newb += b * data[count];
count++;
}
}
newr /= size; // 除以 16(算子總和)
newg /= size;
newb /= size;
newr = Math.min(255, Math.max(0, newr));
newg = Math.min(255, Math.max(0, newg));
newb = Math.min(255, Math.max(0, newb));
newpixs[i * width + j] = Color.argb(255, newr, newg, newb);
}
}
最小值濾波(素描效果中用來取得反相圖的線條):
for (int i = 1; i < height - 1; i++) {
for (int j = 1; j < width - 1; j++) {
temp = 255;
for (int m = -1; m < 2; m++) {
for (int n = -1; n < 2; n++) {
pixcolor = pixs[(i + m) * width + j + n];
temp = temp >= pixcolor ? pixcolor : temp;
}
}
newpixs[i * width + j] = temp;
}
}
圖層疊加(顏色減淡):
// Cdata 為原圖像素,fdata 為反相並經最小值濾波後的像素
for (int i = 1; i < height - 1; i++) {
for (int j = 1; j < width - 1; j++) {
tempr = tempb = tempg = 255;
for (int m = -1; m < 2; m++) {
for (int n = -1; n < 2; n++) {
pixColor = fdata[(i + m) * width + j + n];
r = Color.red(pixColor);
g = Color.green(pixColor);
b = Color.blue(pixColor);
tempr = r > tempr ? tempr : r;
tempg = g > tempg ? tempg : g;
tempb = b > tempb ? tempb : b;
}
}
pixColor = Cdata[i * width + j];
r = Color.red(pixColor);
g = Color.green(pixColor);
b = Color.blue(pixColor);
// 顏色減淡:結果色 = 基色 + 基色 × 混合色 / (255 - 混合色)
int endr = (int) (r + r * tempr / (256 - tempr));
int endg = (int) (g + g * tempg / (256 - tempg));
int endb = (int) (b + b * tempb / (256 - tempb));
temp[i * width + j] = Color.argb(255,
endr >= 255 ? 255 : endr,
endg >= 255 ? 255 : endg,
endb >= 255 ? 255 : endb);
}
}
左圖為 Photoshop 輸出的效果,右圖為本程式實作的結果,整體趨勢一致,差異主要來自係數取捨與邊界處理方式(本實作直接跳過邊界一圈,PS 則會做邊緣延伸)。
專案目標大致達成:對圖像處理的基礎演算法有了較深入的理解,特別是 Sketch 效果與濾鏡開發的常見套路都跑過一遍。整體收穫很大。
這份程式碼以教學為目的,逐像素運算與多層迴圈在大圖上會非常慢,實務上要注意:
1. 改用整型運算。灰階與懷舊的係數都可以放大 256 倍後用位移運算(>> 8)取代浮點乘法,速度差異明顯。
2. 避免 Color.red/green/blue 的頻繁呼叫。直接以位元運算 (pix >> 16) & 0xFF 取通道會快很多。
3. 能交給 ColorMatrix 就不要自己寫迴圈。灰階、反色、色調偏移這類線性變換,ColorMatrixColorFilter 走的是 native 實作,效能遠優於 Java 迴圈。
4. 注意記憶體。一張 1080p 的 ARGB_8888 圖約需 8MB,同一時間若同時存在原圖、陣列、中間結果與輸出,很容易 OOM。用完即 recycle(),並考慮改用 RGB_565(不支援透明但省一半記憶體)或先做縮圖。
5. 邊界處理。卷積類運算跳過邊界會產生一吋黑框,正式實作應做邊緣複製(clamp)或改用 RenderScript/RenderEffect 這類 GPU 加速方案。
仿 QQ 好友列表:PinnedHeaderExpandableListView 分組懸浮完整實作
先看一下完成品的效果。以下截圖是在平板上測試的,尺寸看起來偏大,重點放在互動行為上: 整個效果的關鍵是自訂一個 PinnedHeaderExpandableLis
Android AutoCompleteTextView 實現搜索歷史記錄
AutoCompleteTextView 常用於搜尋框,能在使用者輸入時顯示提示。若把提示來源改為歷史紀錄,就能讓搜尋體驗更貼近實際需求。 以 SharedPrefere
Android 自定義控件:打造閃閃發光的流光字體
效果介紹在小米的開機動畫和一些歡迎界面裡,我們經常能看到那種閃閃發光、流光溢彩的文字效果。看著很炫酷,其實實現原理相當簡單:自定義一個繼承自 TextView 的控件,用
Android 放大鏡效果的兩種實作方式
放大鏡效果的基本原理 在 Android 中製作放大鏡,核心概念是把原圖的一部分放大後,顯示在一個圓形區域中,並讓這個圓形區域跟著觸控位置移動。常見做法有兩種:一種使用