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

Android 圖片處理:ColorMatrix 調色相、飽和度與亮度

編輯:關於 Android 程式設計

要做圖片濾鏡,核心工具是 ColorMatrix。它用一個 4×5 的矩陣對每個像素的 RGBA 值做線性變換,能實現色調、飽和度、亮度三類調整。本文示範如何用三個 SeekBar 即時控制這三個參數。

一、先理解 RGBA 模型與三個名詞

  • 色調/色相(Hue):物件傳遞的顏色本身,例如紅、綠、藍。
  • 飽和度(Saturation):顏色的純度,從 0 到 100% 飽和來描述。0 就是灰階。
  • 亮度/明度(Lum):顏色相對的明暗程度。

三個對應的 ColorMatrix:

// 色相:用旋轉矩陣,0/1/2 分別代表 R/G/B 軸
ColorMatrix hueMatrix = new ColorMatrix();
hueMatrix.setRotate(0, hue);
hueMatrix.setRotate(1, hue);
hueMatrix.setRotate(2, hue);

// 飽和度:直接用內建的 setSaturation
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(saturation);

// 亮度:用縮放矩陣
ColorMatrix lumMatrix = new ColorMatrix();
lumMatrix.setScale(lum, lum, lum, 1);

二、主要程式碼

public class MainActivity extends Activity
        implements SeekBar.OnSeekBarChangeListener {

    private ImageView imageView;
    private SeekBar seekBar1, seekBar2, seekBar3;

    private static final int MAX = 255;
    private static final int IN = 127;

    private MediaPlayer player;

    float a, b, c;
    private Bitmap bitmap;

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

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.g);
        imageView = (ImageView) findViewById(R.id.imageView);
        seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
        seekBar2 = (SeekBar) findViewById(R.id.seekBar2);
        seekBar3 = (SeekBar) findViewById(R.id.seekBar3);

        seekBar1.setOnSeekBarChangeListener(this);
        seekBar2.setOnSeekBarChangeListener(this);
        seekBar3.setOnSeekBarChangeListener(this);

        seekBar1.setMax(MAX);
        seekBar2.setMax(MAX);
        seekBar3.setMax(MAX);

        seekBar1.setProgress(IN);
        seekBar2.setProgress(IN);
        seekBar3.setProgress(IN);

        imageView.setImageBitmap(bitmap);
    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        switch (arg0.getId()) {
            case R.id.seekBar1:
                // 色相:以中點 127 為基準,映射到 -180 ~ 180 度
                a = (arg1 - IN) * 1.0F / IN * 180;
                break;
            case R.id.seekBar2:
                // 飽和度:0.0 ~ 2.0,1.0 為原始值
                b = arg1 * 1.0F / IN;
                break;
            case R.id.seekBar3:
                // 亮度:0.0 ~ 2.0,1.0 為原始值
                c = arg1 * 1.0F / IN;
                break;
        }
        imageView.setImageBitmap(new ImageHelper().handleImageView(bitmap, a, b, c));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { }

    static class ImageHelper {

        public static Bitmap handleImageView(Bitmap bm, float hue, float sat, float lum) {
            Bitmap bm1 = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bm1);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

            // 三個矩陣
            ColorMatrix hm = new ColorMatrix();
            hm.setRotate(0, hue);
            hm.setRotate(1, hue);
            hm.setRotate(2, hue);

            ColorMatrix sm = new ColorMatrix();
            sm.setSaturation(sat);

            ColorMatrix lm = new ColorMatrix();
            lm.setScale(lum, lum, lum, 1);

            // 依序串接:色相 → 飽和度 → 亮度
            ColorMatrix imageMatrix = new ColorMatrix();
            imageMatrix.postConcat(hm);
            imageMatrix.postConcat(sm);
            imageMatrix.postConcat(lm);

            paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
            canvas.drawBitmap(bm, 0, 0, paint);

            return bm1;
        }
    }
}

三、postConcat 的順序很重要

矩陣乘法不可交換,postConcat 是「後乘」,也就是把新矩陣作用在既有結果之後。順序不同,視覺效果會不一樣。實務上先調色相、再調飽和度、最後調亮度,與人眼感知的直覺比較接近。

四、這段程式碼的效能問題

上面每拖動一次 SeekBar,就會 createBitmap 配置一張全新的大圖。手指滑動時每秒觸發幾十次 onProgressChanged,記憶體配置與回收的壓力極大,低階機會直接 OOM。

兩個改善方向:

1. 複用同一張 Bitmap 與 Canvas。把 Bitmap、Canvas、Paint、ColorMatrix 都提到成員變數,只更新矩陣值:

// 成員變數,只建立一次
private final Bitmap mOutput;
private final Canvas mCanvas;
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final ColorMatrix mMatrix = new ColorMatrix();

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    mMatrix.reset();
    mMatrix.postConcat(hueMatrix(progress));
    mMatrix.postConcat(saturationMatrix(progress));
    mPaint.setColorFilter(new ColorMatrixColorFilter(mMatrix));
    mCanvas.drawBitmap(mSource, 0, 0, mPaint);
    imageView.invalidate(); // 觸發重繪
}

2. 用 onProgressChanged 之外的節流。把運算丟到背景執行緒,或用 postDelayed 做 debounce,等使用者停下手指再計算最終結果。

五、另一個更省力的選擇

如果只是要調整亮度,Android 內建了現成屬性,完全不必動 Bitmap:

imageView.setImageAlpha(128); // 整體透明度

而在 XML 上也可以直接對 ImageView 套用 android:tint 搭配 PorterDuff.Mode 做簡單的著色效果,效能遠優於手動處理像素。

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