android 图像颜色处理 图像的亮度变换 更改图片饱和度

来源:互联网 发布:软件图标矢量图 编辑:程序博客网 时间:2024/06/03 23:42

图像颜色处理

颜色矩阵 ColorMatrix cm = newColorMatrix();

paint.setColorFilter(newColorMatrixColorFilter(cm));

1 0 0 0 0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

New Red Value = 1*128 + 0*128 + 0*128+ 0*0 + 0

New Blue Value = 0*128 + 1*128 +0*128 + 0*0 + 0

New Green Value = 0*128 + 0*128 +1*128 + 0*0 + 0

New Alpha Value = 0*128 + 0*128 +0*128 + 1*0 + 0

ColorMatrix cm = newColorMatrix();

cm.set(new float[] {

2, 0, 0, 0, 0,

0, 1, 0, 0, 0,

0, 0, 1, 0, 0,

0, 0, 0, 1, 0

});

paint.setColorFilter(newColorMatrixColorFilter(cm));


图像的亮度变换

ColorMatrix cm = newColorMatrix();

float contrast = 2;

cm.set(new float[] {

contrast, 0, 0, 0, 0,

0, contrast, 0, 0, 0,

0, 0, contrast, 0, 0,

0, 0, 0, 1, 0 });

paint.setColorFilter(newColorMatrixColorFilter(cm));


更改图片饱和度

ColorMatrix cm = newColorMatrix();

cm.setSaturation(.5f);

paint.setColorFilter(newColorMatrixColorFilter(cm));


---------------------------------------------------------------------


public class MainActivity extends Activity {
private ImageView iv_src;
private ImageView iv_dest;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_src = (ImageView) findViewById(R.id.iv_src);
iv_src.setImageBitmap(BitmapFactory.decodeFile("/sdcard/tom.png"));
iv_dest = (ImageView) findViewById(R.id.iv_dest);


}


public void click(View view) {
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/tom.png");
Bitmap source = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), bitmap.getConfig());
Matrix matrix = new Matrix();



ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
2, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0
});
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));



Bitmap baseBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, false);

Canvas canvas = new Canvas(baseBitmap);

canvas.drawBitmap(bitmap, matrix, paint);
iv_dest.setImageBitmap(baseBitmap);


}


}

  ------------------------------------------------------


public class MainActivity extends Activity implements OnSeekBarChangeListener {
private ImageView iv;
private SeekBar sb_red;
private SeekBar sb_green;
private SeekBar sb_blue;
private SeekBar sb_brightness; //亮度
private SeekBar sb_saturation; //饱和度


private Bitmap preBitmap;
private Bitmap afterBitmap;
private Canvas canvas;
ColorMatrix matrix;
private Paint paint;


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


private void initData() {
this.iv = (ImageView) this.findViewById(R.id.iv);
this.sb_red = (SeekBar) this.findViewById(R.id.sb_red);
this.sb_green = (SeekBar) this.findViewById(R.id.sb_green);
this.sb_blue = (SeekBar) this.findViewById(R.id.sb_blue);
this.sb_brightness = (SeekBar) this.findViewById(R.id.sb_brightness);
this.sb_saturation = (SeekBar) this.findViewById(R.id.sb_saturation);


Options opts = new Options();
opts.inSampleSize = 2;
// 从资源文件获取一张不可修改的图片
this.preBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.after,opts );
// 创建一张可以被修改的空白图片
this.afterBitmap = Bitmap.createBitmap(
this.preBitmap.getWidth(), this.preBitmap.getHeight(),
this.preBitmap.getConfig());
// 以空白图片为模版创建一张画布
this.canvas = new Canvas(this.afterBitmap);
// 创建画笔
this.paint = new Paint();
// 创建颜色矩阵
this.matrix = new ColorMatrix();
this.canvas.drawBitmap(this.preBitmap, new Matrix(), this.paint);
this.iv.setImageBitmap(afterBitmap);

}


private void initLintener() {
this.sb_red.setOnSeekBarChangeListener(this);
this.sb_green.setOnSeekBarChangeListener(this);
this.sb_blue.setOnSeekBarChangeListener(this);
this.sb_brightness.setOnSeekBarChangeListener(this);
this.sb_saturation.setOnSeekBarChangeListener(this);
}


@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
switch (seekBar.getId()) {
case R.id.sb_red:
this.matrix.set(new float[]{
progress/128f,0,0,0,0,//红色
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0

});

break;


case R.id.sb_green:
this.matrix.set(new float[]{
1,0,0,0,0,
0,progress/128f,0,0,0,//绿色
0,0,1,0,0,
0,0,0,1,0

});
break;
case R.id.sb_blue:
this.matrix.set(new float[]{
1,0,0,0,0,
0,1,0,0,0,
0,0,progress/128f,0,0, // 蓝色
0,0,0,1,0

});
break;
case R.id.sb_brightness:
this.matrix.set(new float[]{
progress/128f,0,0,0,0,
0,progress/128f,0,0,0,
0,0,progress/128f,0,0,
0,0,0,1,0

});
break;
case R.id.sb_saturation:
this.matrix.setSaturation(progress/128f);
break;
}
this.paint.setColorFilter(new ColorMatrixColorFilter(matrix));
this.canvas.drawBitmap(this.preBitmap, new Matrix(), this.paint);
this.iv.setImageBitmap(afterBitmap);
}


@Override
public void onStartTrackingTouch(SeekBar seekBar) {


}


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


}


}


整理自ppt和相应源码


0 0