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

Android 圖像色調、飽和度與亮度處理實作

編輯:關於 Android 程式設計

先複習一下圖片的 RGBA 模型:R 是紅(Red)、G 是綠(Green)、B 是藍(Blue)、A 是透明度(Alpha)。這四個通道的各種組合構成了我們看到的所有顏色。而色調、飽和度、亮度這三個概念,本質上都是對這組數值所做的線性變換。

Android 提供了 ColorMatrix 這個工具,正好可以對應這三種調整。

工具類實作

public class ImageTools {

    /**
     * 對圖片進行色調、飽和度與亮度處理
     *
     * @param bitmap 原圖
     * @param sx     色調
     * @param bhd    飽和度
     * @param ld     亮度
     */
    public static Bitmap getColorImage(Bitmap bitmap, float sx, float bhd, float ld) {
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        // 色調:分別對 R、G、B 三軸做旋轉
        ColorMatrix sxMatrix = new ColorMatrix();
        sxMatrix.setRotate(0, sx);
        sxMatrix.setRotate(1, sx);
        sxMatrix.setRotate(2, sx);

        // 飽和度
        ColorMatrix bhdMatrix = new ColorMatrix();
        bhdMatrix.setSaturation(bhd);

        // 亮度
        ColorMatrix ldMatrix = new ColorMatrix();
        ldMatrix.setScale(ld, ld, ld, 1);

        // 依序串接三種效果
        ColorMatrix mixMatrix = new ColorMatrix();
        mixMatrix.postConcat(sxMatrix);
        mixMatrix.postConcat(bhdMatrix);
        mixMatrix.postConcat(ldMatrix);

        paint.setColorFilter(new ColorMatrixColorFilter(mixMatrix));

        canvas.drawBitmap(bmp, 0, 0, paint);
        return bmp;
    }
}

這裡有個細節值得說明:範例中把 bmp 同時作為畫布來源與輸出目標。實測可行,但嚴格來說這麼做會有自我覆蓋的疑慮,較穩妥的寫法是先建立一個空白的目標 Bitmap,再把原始 bitmap 畫上去:

Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
        Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawBitmap(bitmap, 0, 0, paint);   // 畫的是原始 bitmap

在 Activity 中搭配 SeekBar 即時調整

public class ColorMainActivity extends Activity
        implements SeekBar.OnSeekBarChangeListener {

    private ImageView colorIv;
    private SeekBar sxBar, bhdBar, ldBar;
    private static final int MIN_COLOR = 100;
    private static final int MAX_COLOR = 255;
    private float sx, bhd, ld;
    private Bitmap bmp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.color_main);
        initViews();
    }

    private void initViews() {
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        colorIv = (ImageView) findViewById(R.id.color_iv);
        colorIv.setImageBitmap(bmp);

        sxBar = (SeekBar) findViewById(R.id.sx_seekbar);
        bhdBar = (SeekBar) findViewById(R.id.bhd_seekbar);
        ldBar = (SeekBar) findViewById(R.id.ld_seekbar);

        sxBar.setOnSeekBarChangeListener(this);
        sxBar.setMax(MAX_COLOR);
        sxBar.setProgress(MIN_COLOR);

        bhdBar.setOnSeekBarChangeListener(this);
        bhdBar.setMax(MAX_COLOR);
        bhdBar.setProgress(MIN_COLOR);

        ldBar.setOnSeekBarChangeListener(this);
        ldBar.setMax(MAX_COLOR);
        ldBar.setProgress(MIN_COLOR);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.sx_seekbar:
                sx = (progress - MIN_COLOR) * 1.0f / MIN_COLOR * 180;
                break;
            case R.id.bhd_seekbar:
                bhd = progress * 1.0f / MIN_COLOR;
                break;
            case R.id.ld_seekbar:
                ld = progress * 1.0f / MIN_COLOR;
                break;
        }
        colorIv.setImageBitmap(ImageTools.getColorImage(bmp, sx, bhd, ld));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {}

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {}
}

這裡的換算值得留意:飽和度與亮度的中性值是 1.0f,所以把 SeekBar 的 100 當基準點,滑桿值除以 100 剛好讓中間位置對應 1.0;色調則是以角度表示,因此用 (progress - 100) / 100 * 180 映射到 -180°~180° 的範圍。

版面檔

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/color_iv"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="15dp" />

    <SeekBar
        android:id="@+id/sx_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/color_iv" />

    <SeekBar
        android:id="@+id/bhd_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/sx_seekbar"
        android:layout_marginTop="10dp" />

    <SeekBar
        android:id="@+id/ld_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/bhd_seekbar"
        android:layout_marginTop="10dp" />
</RelativeLayout>

效能提醒

onProgressChanged 在拖動 SeekBar 時會以極高頻率觸發,每次都重新配置一張 Bitmap,很快就會造成記憶體壓力與卡頓。若要用在正式產品,建議加上節流(例如用 postDelayed 合併同一幀內的多次變更),並在更換圖片時回收舊的 Bitmap。另外若只是要調整亮度,用 PorterDuff 或 ColorMatrixColorFilter 直接套在 ImageView 的 ColorFilter 上會更省資源,不必重新產生一張 Bitmap。

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