圖像處理-顏色RGB修改,飽和度修改

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="R:" />

        <SeekBar
            android:id="@+id/seekBar_R"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="128" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="G:" />

        <SeekBar
            android:id="@+id/seekBar_G"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="128" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="B:" />

        <SeekBar
            android:id="@+id/seekBar_B"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="255"
            android:progress="128" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="飽和度:" />

        <SeekBar
            android:id="@+id/seekBar_sat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="200"
            android:progress="100" />
    </LinearLayout>
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Alpha:" />

        <SeekBar
            android:id="@+id/seekBar_Alpha"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="0" />
    </LinearLayout>
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>



package com.example.colorchange;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity implements OnSeekBarChangeListener
{
	private SeekBar bar_R,bar_G,bar_B,bar_Alpha,bar_sat;
	private ImageView iv;
	private Bitmap map;
	float rR=1,G=1,B=1,A=1,sat=1;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView) findViewById(R.id.iv);
		bar_R=(SeekBar) findViewById(R.id.seekBar_R);
		bar_G=(SeekBar) findViewById(R.id.seekBar_G);
		bar_B=(SeekBar) findViewById(R.id.seekBar_B);
		bar_Alpha=(SeekBar) findViewById(R.id.seekBar_Alpha);
		bar_sat=(SeekBar) findViewById(R.id.seekBar_sat);
		
		bar_R.setOnSeekBarChangeListener(this);
		bar_G.setOnSeekBarChangeListener(this);
		bar_B.setOnSeekBarChangeListener(this);
		bar_Alpha.setOnSeekBarChangeListener(this);
		bar_sat.setOnSeekBarChangeListener(this);
		map = BitmapFactory.decodeResource(getResources(), R.drawable.girl_before).
				copy(Bitmap.Config.ARGB_8888, true);
		iv.setImageBitmap(map);
	}

	private void changeColor(float R,float G,float B,float Alpha,float sat)
	{
		Bitmap finalmap=Bitmap.createBitmap(map.getWidth(), map.getHeight(), Bitmap.Config.ARGB_8888);
		ColorMatrix cm=new ColorMatrix();
		
		cm.set(new float[]{
				R,0,0,0,0,
				0,G,0,0,0,
				0,0,B,0,0,
				0,0,0,Alpha,0
		});
		//修改飽和度api,測試時候懶省事,與set方法衝突 不可同時使用,緣由見api源碼
//		cm.setSaturation(sat);
		
		
		Canvas canvas = new Canvas(finalmap);
		Paint paint = new Paint();
		paint.setStrokeWidth(5);
		paint.setColorFilter(new ColorMatrixColorFilter(cm));
		paint.setColor(Color.BLACK);
		Matrix matrix = new Matrix();
		canvas.drawBitmap(map, matrix, paint);
		iv.setImageBitmap(finalmap);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
	{
		
		if(seekBar==bar_R)
		{
			rR=progress/128f;
		}
		if(seekBar==bar_G)
		{
			G=progress/128f;
		}
		if(seekBar==bar_B)
		{
			B=progress/128f;
		}
		if(seekBar==bar_Alpha)
		{
			A=1-progress/100f;
		}
		if(seekBar==bar_sat)
		{
			sat=progress/100f;
		}
		changeColor(rR, G, B, A,sat);
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar)
	{
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar)
	{
	}

}
相關文章
相關標籤/搜索