簡單的圖片處理器(設定色相,亮度,飽和度)

陳柏戈發表於2015-04-07

首先前臺佈局就是選擇的一張圖片,還有三個設定三種顏色引數的seekbar


<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" >


    <LinearLayout android:layout_height="0dp"
        android:layout_width="fill_parent"
        android:layout_weight="1">


        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/touxiang" />
        
    </LinearLayout>
    
     <LinearLayout android:layout_height="50dp"
        android:layout_width="fill_parent">


         <SeekBar
             android:id="@+id/rotateseekBar"
             android:layout_width="match_parent"
             android:layout_marginLeft="20dp"
             android:layout_marginRight="20dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
android:layout_gravity="center_vertical"/>
         
     </LinearLayout>
    
      <LinearLayout android:layout_height="50dp"
        android:layout_width="fill_parent">


         <SeekBar
             android:id="@+id/saturationseekBar"
             android:layout_width="match_parent"
             android:layout_marginLeft="20dp"
             android:layout_marginRight="20dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
android:layout_gravity="center_vertical"/>
         
     </LinearLayout>
    
      
       <LinearLayout android:layout_height="50dp"
        android:layout_width="fill_parent">


         <SeekBar
             android:id="@+id/lumseekBar"
             android:layout_width="match_parent"
             android:layout_marginLeft="20dp"
             android:layout_marginRight="20dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
android:layout_gravity="center_vertical"/>
         
     </LinearLayout>
    
     


</LinearLayout>



接下來在主佈局的後臺監聽seekbar的changed事件,對圖片進行調整處理


package com.example.imageswitch;


import ImageUtil.BitmapSwitch;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;


public class MainActivity extends BitmapSwitch implements OnSeekBarChangeListener {


private ImageView contentImageView;
private SeekBar rotateBar, satutationBar, lumBar;
private static int MAX_VALUE = 255;
private static int MID_VALUE = 127;
//儲存當前seekbar設定的值
private float rvalue,svalue,lvalue;
private Bitmap currentBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}


private void initView() {
contentImageView = (ImageView) findViewById(R.id.imageView1);
rotateBar = (SeekBar) findViewById(R.id.rotateseekBar);
satutationBar = (SeekBar) findViewById(R.id.saturationseekBar);
lumBar = (SeekBar) findViewById(R.id.lumseekBar);
currentBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.touxiang);

rotateBar.setOnSeekBarChangeListener(this);
satutationBar.setOnSeekBarChangeListener(this);
lumBar.setOnSeekBarChangeListener(this);
// 設定當前各種屬性的值,和各種屬性最大值
rotateBar.setMax(MAX_VALUE);
rotateBar.setProgress(MID_VALUE);
satutationBar.setMax(MAX_VALUE);
satutationBar.setProgress(MID_VALUE);
lumBar.setMax(MAX_VALUE);
lumBar.setProgress(MID_VALUE);
contentImageView.setImageBitmap(currentBitmap);
}


@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) {
// TODO Auto-generated method stub
// Bitmap bitmap=
switch (seekBar.getId()) {
case R.id.rotateseekBar:
rvalue=(float) ((progress-MID_VALUE)*1.0F/MID_VALUE*180);//將seekbar獲取的值轉化
break;


case R.id.saturationseekBar:
svalue=(float) ((progress)*1.0F/MID_VALUE);
break;


case R.id.lumseekBar:
lvalue=progress*1.0F/MID_VALUE;
break;
}
//currentBitmap=handBitmap(currentBitmap, rvalue, svalue, lvalue);
contentImageView.setImageBitmap(handBitmap(currentBitmap, rvalue, svalue, lvalue));
}


@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub


}


@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub


}


}



最主要的是實現根據這些引數調整bitmap的工具類

public Bitmap handBitmap(Bitmap bitmap,float rotate,float saturation,float lum){
Bitmap newBitmap=bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas=new Canvas(newBitmap);
Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);
//0,1,2分別設定R,G,B三種色值
ColorMatrix ratateColorMatrix=new ColorMatrix();
ratateColorMatrix.setRotate(0, rotate);
ratateColorMatrix.setRotate(1, rotate);
ratateColorMatrix.setRotate(2, rotate);

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

//設定亮度--四個引數是R,G,B,透明度(1表示全透明)
ColorMatrix lumColorMatrix=new ColorMatrix();
lumColorMatrix.setScale(lum, lum, lum, 1);

//將三種效果融合
ColorMatrix allColorMatrix=new ColorMatrix();
allColorMatrix.postConcat(ratateColorMatrix);
allColorMatrix.postConcat(saturationColorMatrix);
allColorMatrix.postConcat(lumColorMatrix);

paint.setColorFilter(new ColorMatrixColorFilter(allColorMatrix));
canvas.drawBitmap(bitmap, 0, 0, paint);

return newBitmap;

}


結果返回一個新的bitmap,再將這個bitmap設定到imageview上,就實現了對圖片的調整。。。

相關文章