android 图像颜色处理 图像的亮度变换 更改图片饱和度
2015-06-29 00:44
751 查看
图像颜色处理
颜色矩阵 ColorMatrix cm = new
ColorMatrix();
paint.setColorFilter(new
ColorMatrixColorFilter(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 = 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.setColorFilter(new
ColorMatrixColorFilter(cm));
图像的亮度变换
ColorMatrix cm = new
ColorMatrix();
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(new
ColorMatrixColorFilter(cm));
更改图片饱和度
ColorMatrix cm = new
ColorMatrix();
cm.setSaturation(.5f);
paint.setColorFilter(new
ColorMatrixColorFilter(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和相应源码
颜色矩阵 ColorMatrix cm = new
ColorMatrix();
paint.setColorFilter(new
ColorMatrixColorFilter(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 = 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.setColorFilter(new
ColorMatrixColorFilter(cm));
图像的亮度变换
ColorMatrix cm = new
ColorMatrix();
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(new
ColorMatrixColorFilter(cm));
更改图片饱和度
ColorMatrix cm = new
ColorMatrix();
cm.setSaturation(.5f);
paint.setColorFilter(new
ColorMatrixColorFilter(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和相应源码
相关文章推荐
- android 图形平移 镜子效果 倒影效果
- android listview addHeaderView和addFooterView详解
- android 图形旋转
- android 中 图形缩放
- Android5.0 学习笔记(一)
- Android打包jar 和使用第三方库
- 详解android:scaleType属性
- github android 项目地址
- Android Activity去除标题栏和状态栏
- Android推送进阶课程学习笔记
- 关于Android Studio SDK在线更新问题
- 22.AndroidManifest中original-package标签
- Android编程之SparseArray<E>详解
- 关于 android 中 postDelayed方法的讲解
- 初次使用Android Studio遇到的问题
- Android与web服务器数据交互编程---1
- Android中Dialog设置外部点击事件
- Android getWidth和getMeasuredWidth
- Android 自定义View (一)
- Android Material Design之Toolbar与Palette实践