Android改變圖像的飽和度、亮度和對比度

使用到了ColorMatrix。

Java代碼:

package com.figo.imgedit;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ImgeditActivity extends Activity {
	/** Called when the activity is first created. */
	private Bitmap srcBitmap, dstBitmap;
	private String pathName = "/sdcard/testimg.jpg";

	private ImageView dstimage = null;
	private SeekBar SaturationseekBar = null;
	private SeekBar BrightnessseekBar = null;
	private SeekBar ContrastseekBar = null;
	private int imgHeight, imgWidth;

	public static final int PICTURE = 0;
	public static final int MAX_WIDTH = 240;
	public static final int MAX_HEIGHT = 240;
	private Uri imageUri;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		dstimage = (ImageView) findViewById(R.id.dstImageView);
		SaturationseekBar = (SeekBar) findViewById(R.id.Saturationseekbar);
		BrightnessseekBar = (SeekBar) findViewById(R.id.Brightnessseekbar);
		ContrastseekBar = (SeekBar) findViewById(R.id.Contrastseekbar);

		srcBitmap = BitmapFactory.decodeFile(pathName);
		dstimage.setImageBitmap(srcBitmap);
		imgHeight = srcBitmap.getHeight();
		imgWidth = srcBitmap.getWidth();

		dstBitmap = Bitmap.createBitmap(imgWidth, imgHeight, Config.ARGB_8888);

		SaturationseekBar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					// 當拖動條的滑塊位置發生改變時觸發該方法
					public void onProgressChanged(SeekBar arg0, int progress,
							boolean fromUser) {
						// 創建一個相同尺寸的可變的位圖區,用於繪製調色後的圖片
						Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight,
								Config.ARGB_8888);
						ColorMatrix cMatrix = new ColorMatrix();
						// 設置飽和度
						cMatrix.setSaturation((float) (progress / 100.0));

						Paint paint = new Paint();
						paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));

						Canvas canvas = new Canvas(bmp);
						// 在Canvas上繪製一個已經存在的Bitmap。這樣,dstBitmap就和srcBitmap一摸一樣了
						canvas.drawBitmap(srcBitmap, 0, 0, paint);

						dstimage.setImageBitmap(bmp);

					}

					public void onStartTrackingTouch(SeekBar bar) {
					}

					public void onStopTrackingTouch(SeekBar bar) {
					}
				});

		BrightnessseekBar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					// 當拖動條的滑塊位置發生改變時觸發該方法
					public void onProgressChanged(SeekBar arg0, int progress,
							boolean fromUser) {
						Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight,
								Config.ARGB_8888);
						int brightness = progress - 127;
						ColorMatrix cMatrix = new ColorMatrix();
						cMatrix.set(new float[] { 1, 0, 0, 0, brightness, 0, 1,
								0, 0, brightness,// 改變亮度
								0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 });

						Paint paint = new Paint();
						paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));

						Canvas canvas = new Canvas(bmp);
						// 在Canvas上繪製一個已經存在的Bitmap。這樣,dstBitmap就和srcBitmap一摸一樣了
						canvas.drawBitmap(srcBitmap, 0, 0, paint);
						dstimage.setImageBitmap(bmp);

					}

					public void onStartTrackingTouch(SeekBar bar) {
					}

					public void onStopTrackingTouch(SeekBar bar) {
					}
				});

		ContrastseekBar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					// 當拖動條的滑塊位置發生改變時觸發該方法
					public void onProgressChanged(SeekBar arg0, int progress,
							boolean fromUser) {
						Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight,
								Config.ARGB_8888);
						// int brightness = progress - 127;
						float contrast = (float) ((progress + 64) / 128.0);
						ColorMatrix cMatrix = new ColorMatrix();
						cMatrix.set(new float[] { contrast, 0, 0, 0, 0, 0,
								contrast, 0, 0, 0,// 改變對比度
								0, 0, contrast, 0, 0, 0, 0, 0, 1, 0 });

						Paint paint = new Paint();
						paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));

						Canvas canvas = new Canvas(bmp);
						// 在Canvas上繪製一個已經存在的Bitmap。這樣,dstBitmap就和srcBitmap一摸一樣了
						canvas.drawBitmap(srcBitmap, 0, 0, paint);

						dstimage.setImageBitmap(bmp);
					}

					public void onStartTrackingTouch(SeekBar arg0) {
						// TODO Auto-generated method stub

					}

					public void onStopTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub
					}
				});
	}

	/**
	 * 需要加載的圖片可能是大圖,我們需要對其進行合適的縮小處理
	 * 
	 * @param imageUri
	 */
	private Bitmap getSrcImage(Uri imageUri) {
		try {
			BitmapFactory.Options ops = new BitmapFactory.Options();
			ops.inJustDecodeBounds = true;
			Bitmap bmp = BitmapFactory.decodeStream(this.getContentResolver()
					.openInputStream(imageUri), null, ops);
			int wRatio = (int) Math.ceil(ops.outWidth / (float) MAX_WIDTH);
			int hRatio = (int) Math.ceil(ops.outHeight / (float) MAX_HEIGHT);

			if (wRatio > 1 && hRatio > 1) {
				if (wRatio > hRatio) {
					ops.inSampleSize = wRatio;
				} else {
					ops.inSampleSize = hRatio;
				}
			}

			ops.inJustDecodeBounds = false;
			bmp = BitmapFactory.decodeStream(this.getContentResolver()
					.openInputStream(imageUri), null, ops);

			return bmp;

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.e(this.getClass().getName(), e.getMessage());
		}

		return null;
	}
}
佈局文件,比較簡單:

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

	<ImageView android:id="@+id/dstImageView" android:scaleType="fitCenter"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:maxWidth="240px" android:maxHeight="240px" />

	<LinearLayout android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:orientation="vertical">
		<LinearLayout android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:orientation="horizontal"
			android:layout_alignParentBottom="true" android:gravity="center_vertical">

			<TextView android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="@string/Saturation" />

			<!-- 定義一個拖動條,並改變它的滑塊外觀 -->
			<SeekBar android:id="@+id/Saturationseekbar"
				android:layout_width="fill_parent" android:layout_height="wrap_content"
				android:progress="100" android:max="200" />
		</LinearLayout>

		<LinearLayout android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:orientation="horizontal"
			android:gravity="center_vertical">

			<TextView android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="@string/Brightness" />

			<!-- 定義一個拖動條,並改變它的滑塊外觀 -->
			<SeekBar android:id="@+id/Brightnessseekbar"
				android:layout_width="fill_parent" android:layout_height="wrap_content"
				android:progress="127" android:max="255" />
		</LinearLayout>

		<LinearLayout android:layout_width="fill_parent"
			android:layout_height="wrap_content" android:orientation="horizontal"
			android:gravity="center_vertical">

			<TextView android:layout_width="wrap_content"
				android:layout_height="wrap_content" android:text="@string/Contrast" />

			<!-- 定義一個拖動條,並改變它的滑塊外觀 -->
			<SeekBar android:id="@+id/Contrastseekbar"
				android:layout_width="fill_parent" android:layout_height="wrap_content"
				android:progress="63" android:max="127" />
		</LinearLayout>
	</LinearLayout>
</LinearLayout>

運行結果:



發佈了20 篇原創文章 · 獲贊 190 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章