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

Android 圖像處理實作:用像素運算做出底片、懷舊與浮雕效果

編輯:關於 Android 程式設計

上一篇談過用色彩矩陣改圖片顏色,這次換個思路——直接動每個像素的 RGBA 數值。

原理其實很樸素:一張圖片由無數緊密排列的像素點組成,每個像素點攜帶 R、G、B、A 四個通道的值。只要逐一改寫這些值,整體色調就跟著改變了。本文用這個思路實作三個效果:底片、懷舊、浮雕。

先把畫面排出來

建立一個版面配置檔,用兩層 LinearLayout 均分四個 ImageView,分別顯示原圖與三種處理結果:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/image2"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ImageView
            android:id="@+id/image3"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/image4"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

四個格子依序用來放:原圖、底片、懷舊、浮雕。

效果一:底片

底片最簡單,把每個通道的值用 255 減掉即可。

public Bitmap handleNegative(Bitmap bt) {
    int width = bt.getWidth();
    int height = bt.getHeight();
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    int[] oldPix = new int[width * height];
    int[] newPix = new int[width * height];

    // 把原圖像素全部取出來
    bt.getPixels(oldPix, 0, width, 0, 0, width, height);

    for (int i = 0; i < oldPix.length; i++) {
        int color = oldPix[i];
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        int a = Color.alpha(color);

        // 底片變換:255 減去各通道值
        r = 255 - r;
        g = 255 - g;
        b = 255 - b;

        newPix[i] = Color.argb(a, clamp(r), clamp(g), clamp(b));
    }

    bmp.setPixels(newPix, 0, width, 0, 0, width, height);
    return bmp;
}

// 運算結果必須夾在 0~255 之間,否則會出現色塊異常
private int clamp(int v) {
    return v < 0 ? 0 : (v > 255 ? 255 : v);
}

效果二:懷舊

懷舊(老照片)效果靠一組加權係數把 RGB 往暖色調拉,畫面會呈現泛黃的褐色調。

public Bitmap handleOld(Bitmap bt) {
    int width = bt.getWidth();
    int height = bt.getHeight();
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    int[] oldPix = new int[width * height];
    int[] newPix = new int[width * height];
    bt.getPixels(oldPix, 0, width, 0, 0, width, height);

    for (int i = 0; i < oldPix.length; i++) {
        int color = oldPix[i];
        int r0 = Color.red(color);
        int g0 = Color.green(color);
        int b0 = Color.blue(color);
        int a = Color.alpha(color);

        // 注意:右邊必須用原始的 r0/g0/b0,不可邊算邊覆寫
        int r = (int) (0.393 * r0 + 0.769 * g0 + 0.189 * b0);
        int g = (int) (0.349 * r0 + 0.686 * g0 + 0.168 * b0);
        int b = (int) (0.272 * r0 + 0.534 * g0 + 0.131 * b0);

        newPix[i] = Color.argb(a, clamp(r), clamp(g), clamp(b));
    }

    bmp.setPixels(newPix, 0, width, 0, 0, width, height);
    return bmp;
}

效果三:浮雕

浮雕的思路是讓每個像素與它前一個像素做差值,再平移到中間灰階(127)附近,於是邊緣被凸顯、平坦區域趨於單色,看起來就像壓印上去的浮雕。

public Bitmap handleRelief(Bitmap bt) {
    int width = bt.getWidth();
    int height = bt.getHeight();
    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    int[] oldPix = new int[width * height];
    int[] newPix = new int[width * height];
    bt.getPixels(oldPix, 0, width, 0, 0, width, height);

    for (int i = 0; i < oldPix.length; i++) {
        int color = oldPix[i];
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        int a = Color.alpha(color);

        // 取前一個像素;i 為 0 時避免陣列越界,直接取自己
        int prev = oldPix[i == 0 ? 0 : i - 1];
        int r1 = Color.red(prev);
        int g1 = Color.green(prev);
        int b1 = Color.blue(prev);

        int nr = r1 - r + 127;
        int ng = g1 - g + 127;
        int nb = b1 - b + 127;

        newPix[i] = Color.argb(a, clamp(nr), clamp(ng), clamp(nb));
    }

    bmp.setPixels(newPix, 0, width, 0, 0, width, height);
    return bmp;
}

幾個容易忽略的細節

  • 通道取值要用對方法:Color.red()、Color.green()、Color.blue() 各自對應一個通道,寫成連續三次 red() 是常見筆誤。
  • 加權運算要用暫存變數:算 G 的時候需要原始的 R 值,若已把 R 覆寫掉,結果就會失真。
  • 務必做邊界檢查:任何運算結果都可能超出 0~255,未夾住範圍會直接造成顏色錯亂。
  • 大圖慎用純像素迴圈:全圖遍歷在 Java 層執行相當耗時,實務上會搭配背景執行緒,或改用 Bitmap 的內建色彩矩陣 API。

把這三個函式串起來,一個陽春的「手機 PS」雛形就出來了。理解了像素這一層,後續要加亮度、對比度、伽瑪校正都只是換公式而已。

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