修改图片颜色 色相 饱和度 亮度 ColorMatrix

Android开发中经常会遇到一些简单的图片处理,比如修改图片的颜色,饱和度,亮度等。今天就给大家介绍一下简单的图片处理。

基础知识

颜色的三要素
  1. 色调(色相/颜色):物体的颜色;
  2. 饱和度(彩度):色彩的鲜艳度。颜色的纯度:0(灰)~100%(饱和);
  3. 亮度(明度):眼睛对光源和物体表面的明暗程度的感觉,主要是由光线强弱决定的一种视觉经验。
ARGB模型

A:alpha,透明度;
R:red,红色;
G:green,绿色;
B:blue,蓝色;

屏幕是由一定的像素点组合而成的,比如1920*1080,而每一个像素点都是由argb模型组合成的。

代码

  1. 布局文件
<?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,我会单独写一个博客继续讲解的,谢谢大家!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值