版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HandsomeFuHS/article/details/70312900
Android开发中经常会遇到一些简单的图片处理,比如修改图片的颜色,饱和度,亮度等。今天就给大家介绍一下简单的图片处理。
基础知识
颜色的三要素
- 色调(色相/颜色):物体的颜色;
- 饱和度(彩度):色彩的鲜艳度。颜色的纯度:0(灰)~100%(饱和);
- 亮度(明度):眼睛对光源和物体表面的明暗程度的感觉,主要是由光线强弱决定的一种视觉经验。
ARGB模型
A:alpha,透明度;
R:red,红色;
G:green,绿色;
B:blue,蓝色;
屏幕是由一定的像素点组合而成的,比如1920*1080,而每一个像素点都是由argb模型组合成的。
代码
- 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_margin="10dp"
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp" />
<SeekBar
android:id="@+id/seekBarHue"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<SeekBar
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/seekBarSaturation"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/seekBarLum"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
2.PrimaryColorActivity
public class PrimaryColorActivity extends Activity implements SeekBar.OnSeekBarChangeListener {
private ImageView imageView;
//修改色相
private SeekBar seekBarHue;
//修改饱和度
private SeekBar seekBarSaturation;
//修改亮度
private SeekBar seekBarLum;
//seekbar最大值
private static final int MAX_VALUE = 255;
//seekbar中间值 起始值
private static final int MID_VALUE = 127;
//当前图片的色相,饱和度,亮度
private float mHue, mSaturation, mLum;
//显示的图片
private Bitmap bitmap;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_primary);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.baikal1);
imageView = (ImageView) findViewById(R.id.imageView);
seekBarHue = (SeekBar) findViewById(R.id.seekBarHue);
seekBarSaturation = (SeekBar) findViewById(R.id.seekBarSaturation);
seekBarLum = (SeekBar) findViewById(R.id.seekBarLum);
seekBarHue.setOnSeekBarChangeListener(this);
seekBarSaturation.setOnSeekBarChangeListener(this);
seekBarLum.setOnSeekBarChangeListener(this);
seekBarHue.setMax(MAX_VALUE);
seekBarSaturation.setMax(MAX_VALUE);
seekBarLum.setMax(MAX_VALUE);
seekBarHue.setProgress(MID_VALUE);
seekBarSaturation.setProgress(MID_VALUE);
seekBarLum.setProgress(MID_VALUE);
imageView.setImageBitmap(bitmap);
}
//mHue, mSaturation, mLum的计算公式为经验公式 不是固定的
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
switch (seekBar.getId()) {
case R.id.seekBarHue:
mHue = (progress - MID_VALUE) * 1.0f / MID_VALUE * 180;
break;
case R.id.seekBarSaturation:
//饱和度取值 0 - 2
mSaturation = progress * 1.0f / MID_VALUE;
break;
case R.id.seekBarLum:
//亮度取值 0 - 2
mLum = progress * 1.0f / MID_VALUE;
break;
}
imageView.setImageBitmap(ImageHelper.handleImageEffect(bitmap, mHue, mSaturation, mLum));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
3.ImageHelper
public class ImageHelper {
/**
* 色相
* 饱和度
* 亮度
*/
public static Bitmap handleImageEffect(Bitmap bitmap, float hue, float saturation, float lum) {
//传进来的bitmap默认不能修改 所以再创建一个bm
Bitmap bm = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//画布
Canvas canvas = new Canvas(bm);
//抗锯齿
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//颜色矩阵
ColorMatrix hueMatrix = new ColorMatrix();
//修改色相 0 red 1 green 2 blue
hueMatrix.setRotate(0, hue);
hueMatrix.setRotate(1, hue);
hueMatrix.setRotate(2, hue);
//修改饱和度
ColorMatrix saturationMatrix = new ColorMatrix();
saturationMatrix.setSaturation(saturation);
//修改亮度
ColorMatrix lumMatrix = new ColorMatrix();
//r g b a 1 表示全不透明
lumMatrix.setScale(lum, lum, lum, 1);
//组合Matrix
ColorMatrix imageMatrix = new ColorMatrix();
imageMatrix.postConcat(hueMatrix);
imageMatrix.postConcat(saturationMatrix);
imageMatrix.postConcat(lumMatrix);
//为画笔设置颜色过滤器
paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
//在canvas上照着bitmap画
canvas.drawBitmap(bitmap, 0, 0, paint);
return bm;
}
}
现在将程序运行,移动seekbar,应该就可以看到效果了。
总结:图片处理其实就是利用ColorMatrix对每一个像素点的处理,对于ColorMatrix,我会单独写一个博客继续讲解的,谢谢大家!