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

Android 圖片調色實作:用 ColorMatrix 即時調整色度、飽和度與亮度

編輯:關於 Android 程式設計

最近在做圖片相關的應用,累積了一些常用做法。圖片色彩變換大致有三條路:一是色度變換,二是使用 ColorMatrix 顏色矩陣,三是直接操作像素點。今天複習第一種——它比較單一,能得到的效果類型也有限,但正好對應「換色度、飽和度、亮度」這種最常見的風格調整需求。

編碼思路

• 抽出一個圖片操作工具類
• 建立一個用於運算的 Bitmap 物件
• 使用 Canvas 與 Paint
• 以矩陣做調色處理,參數由外部控制
• 畫出結果 Bitmap 並回傳
• 供各個方法呼叫取得效果

版面配置

三支 SeekBar 分別控制色度、飽和度與亮度,即時反映到上方的 ImageView。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="320dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <TextView
            android:text="色 度"
            android:textSize="18dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <SeekBar
            android:id="@+id/hueBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="5" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <TextView
            android:text="飽和度"
            android:textSize="18dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <SeekBar
            android:id="@+id/saturationBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="5" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <TextView
            android:text="亮 度"
            android:textSize="18dp"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <SeekBar
            android:id="@+id/lumBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="5" />
    </LinearLayout>
</LinearLayout>

工具類的核心方法

三個 ColorMatrix 各自負責色度旋轉、飽和度與亮度縮放,再用 postConcat 串接成最終矩陣,套進 ColorMatrixColorFilter 後一次畫出結果。

public static Bitmap handleImageLikePS(Bitmap bp, float hue, float saturation, float lum) {
    Bitmap bitmap = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    ColorMatrix hueMatrix = new ColorMatrix();
    hueMatrix.setRotate(0, hue);
    hueMatrix.setRotate(1, hue);
    hueMatrix.setRotate(2, hue);

    ColorMatrix saturationMatrix = new ColorMatrix();
    saturationMatrix.setSaturation(saturation);

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

    ColorMatrix imageMatrix = new ColorMatrix();
    imageMatrix.postConcat(hueMatrix);
    imageMatrix.postConcat(saturationMatrix);
    imageMatrix.postConcat(lumMatrix);

    paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
    // 此處若換成 bitmap 會只套用一次,圖像將無法再被反覆編輯
    canvas.drawBitmap(bp, 0, 0, paint);
    return bitmap;
}

Activity 中的使用

package com.example.colormatrixdemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener {

    private Bitmap bitmap;
    private ImageView imageview;
    private SeekBar hueBar, saturationBar, lumBar;
    private float mHue, mSaturation, mLum;
    private static int MAXVALUE = 255, MIDVALUE = 127;

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

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.masuo);
        imageview = (ImageView) findViewById(R.id.imageview);
        hueBar = (SeekBar) findViewById(R.id.hueBar);
        saturationBar = (SeekBar) findViewById(R.id.saturationBar);
        lumBar = (SeekBar) findViewById(R.id.lumBar);

        hueBar.setOnSeekBarChangeListener(this);
        saturationBar.setOnSeekBarChangeListener(this);
        lumBar.setOnSeekBarChangeListener(this);

        hueBar.setMax(MAXVALUE);
        hueBar.setProgress(MIDVALUE);
        saturationBar.setMax(MAXVALUE);
        saturationBar.setProgress(MIDVALUE);
        lumBar.setMax(MAXVALUE);
        lumBar.setProgress(MIDVALUE);

        imageview.setImageBitmap(bitmap);
    }

    @Override
    public void onProgressChanged(SeekBar seekbar, int progress, boolean arg2) {
        switch (seekbar.getId()) {
            case R.id.hueBar:
                mHue = (progress - MIDVALUE) * 1.0F / MIDVALUE * 180;
                break;
            case R.id.saturationBar:
                mSaturation = progress * 1.0F / MIDVALUE;
                break;
            case R.id.lumBar:
                mLum = progress * 1.0F / MIDVALUE;
                break;
        }
        imageview.setImageBitmap(ImageTools.handleImageLikePS(bitmap, mHue, mSaturation, mLum));
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
    }

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

一個很容易寫錯的地方

工具類最後一定要拿「傳進來的參數 bp」去畫,而不是自己宣告的那個 bitmap,否則永遠拿不到即時變化的結果。我們宣告的 bitmap 只是一塊用來承接輸出的畫布,真正要處理的對象是參數。

總結

圖片處理的方法很多,其中直接操作像素點能做出最多花樣,老照片、浮雕、底片之類的效果都靠它;而顏色矩陣則是另一條相當經典又輕量的路線。這些技巧都值得花時間研究,能讓你的應用呈現更豐富的視覺表現。

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