Android通过调整色光三原色给图片调色,Android调色相亮度饱和度,从相册选择图片并调色

从相册选择一张图片,通过 SeekBar 调图片的色相(红绿蓝)、饱和度、亮度。
.
开发工具:Android Studio
每一步操作均有详细注释,代码直接复制可用
在这里插入图片描述
在这里插入图片描述

源码介绍:

源码结构:
在这里插入图片描述

主要模块:调色的封装类 【ImageHelper.java】

public class ImageHelper {

    /**
     * 图像色光三原色调整
     *
     * @param bitmap  被调整的Bitmap图像
     * @param hue     色相值 -180 - 180
     * @param saturation  饱和度 1-2
     * @param lum   亮度 1-2
     * @return bmp 调整后的 Bitmap 图像
     */
    public static Bitmap handleImgeEffect(Bitmap bitmap, float hue, float saturation, float lum){

        L.e("\n颜色参数" + "\n色相:" + hue + "\n饱和度:" + saturation + "\n亮度:" + lum);
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);  //32位ARGB位图(最高模式)
        Canvas canvas = new Canvas(bmp);  //创建一张与传进来的图片大小一样的画布
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  //创建颜色画笔,并设置抗锯齿 ANTI_ALIAS_FLAG

        //色相
        ColorMatrix hueMatrix = new ColorMatrix();  //创建颜色矩阵
        hueMatrix.setRotate(0,hue);  //0:RED  1:GREEN   2:BLUE
        hueMatrix.setRotate(1,hue);
        hueMatrix.setRotate(2,hue);

        //饱和度
        ColorMatrix saturationMatrix = new ColorMatrix();  //创建颜色矩阵
        saturationMatrix.setSaturation(saturation);

        //亮度
        ColorMatrix lumMatrix = new ColorMatrix();  //创建颜色矩阵
        lumMatrix.setScale(lum,lum,lum,1);  //R,G,B,A

        //融合所有效果
        ColorMatrix imageMatrix = new ColorMatrix();  //创建颜色矩阵
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);

        //调用画笔,将效果重新画到新的画布上面
        paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        canvas.drawBitmap(bitmap,0,0,paint);

        return bmp;  //返回新的图像
    }
}

Log类,用于调试打印 【L.java】

public class L {

    //开关
    public static final boolean DEBUG = true;
    //TAG
    public static final String TAG = "Smartbutler";

    public static void e(String text){
        if(DEBUG){
            Log.e(TAG,text);
        }
    }
}

主要的业务逻辑(主类,启动类)【MainActivity.java】

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener {
    private SeekBar SeekBar01;
    private SeekBar SeekBar02;
    private SeekBar SeekBar03;
    private ImageView ImageView;
    private Button Button;

    private static int MAX_VALUE = 255;  //SeekBar最大值为 255 颜色值
    private static int MID_VALUE = 127;  //SeekBar中间值

    private float mHue, mStauration, mLum;  //色相/饱和度/亮度

    private Bitmap mBitmap = null;  //待处理的图像

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

        //从源文件中取出一张图片
        mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.aaaaa);

        initView();
    }

    private void initView() {

        SeekBar01 = findViewById(R.id.SeekBar01);
        SeekBar02 = findViewById(R.id.SeekBar02);
        SeekBar03 = findViewById(R.id.SeekBar03);
        ImageView = findViewById(R.id.ImageView);
        Button = findViewById(R.id.Button);

        //设置监听事件
        SeekBar01.setOnSeekBarChangeListener(this);
        SeekBar02.setOnSeekBarChangeListener(this);
        SeekBar03.setOnSeekBarChangeListener(this);
        Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setImage();
            }
        });

        //设置最大值
        SeekBar01.setMax(MAX_VALUE);
        SeekBar02.setMax(MAX_VALUE);
        SeekBar03.setMax(MAX_VALUE);

        //设置默认值 MID_VALUE (进度条在中间)
        SeekBar01.setProgress(MID_VALUE);
        SeekBar02.setProgress(MID_VALUE);
        SeekBar03.setProgress(MID_VALUE);

        //设置图片显示
        ImageView.setImageBitmap(mBitmap);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        L.e("滑动位置:" + progress);
        //SeekBar滚动时
        switch (seekBar.getId()) {
            case R.id.SeekBar01:
                mHue = (progress - MID_VALUE) * 1.0F / MID_VALUE * 180;   //公式
                break;
            case R.id.SeekBar02:
                mStauration = (progress * 1.0F) / MID_VALUE;    //公式 饱和度范围 0-2
                break;
            case R.id.SeekBar03:
                mLum = (progress * 1.0F) / MID_VALUE;   //公式  亮度范围 0-2
                break;
        }
        Bitmap bitmap = ImageHelper.handleImgeEffect(mBitmap, mHue, mStauration, mLum);  //调用封装类,实现调整图片
        ImageView.setImageBitmap(bitmap);  //重新设置图片
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }


    private final String IMAGE_TYPE = "image/*";
    private final int IMAGE_CODE = 0; // 这里的IMAGE_CODE是自己任意定义的

    private void setImage() {
        // TODO Auto-generated method stub
        // 使用intent调用系统提供的相册功能,使用startActivityForResult是为了获取用户选择的图片的地址
        Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
        getAlbum.setType(IMAGE_TYPE);
        startActivityForResult(getAlbum, IMAGE_CODE);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // RESULT_OK 是系统自定义得一个常量
        if (resultCode != RESULT_OK) {
            L.e("onActivityResult,返回的resultCode出错");
            return;
        }
        Bitmap bm = null;
        // 外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
        ContentResolver resolver = getContentResolver();
        // 判断接收的Activity是不是选择图片的
        if (requestCode == IMAGE_CODE) {
            try {
                // 获得图片的地址Uri
                Uri originalUri = data.getData();
                // 新建一个字符串数组用于存储图片地址数据。
                String[] proj = { MediaStore.Images.Media.DATA };
                // android系统提供的接口,用于根据uri获取数据
                Cursor cursor = managedQuery(originalUri, proj, null, null, null);
                // 获得用户选择图片的索引值
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                L.e("获得用户选择图片的索引值:" + column_index);
                // 将游标移至开头 ,防止引起队列越界
                cursor.moveToFirst();
                // 根据图片的URi生成bitmap
                mBitmap = MediaStore.Images.Media.getBitmap(resolver, originalUri);
                // 显得到bitmap图片
                ImageView.setImageBitmap(mBitmap);
            } catch (IOException e) {
                Log.e("getImg", e.toString());
            }
        }
    }
}

只有一个布局文件【activity_main.xml】

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/ImageView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="#d9d9d9"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="色相"/>
        <SeekBar
            android:id="@+id/SeekBar01"
            android:layout_width="match_parent"
            android:layout_marginTop="5dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="饱和度"/>
        <SeekBar
            android:id="@+id/SeekBar02"
            android:layout_width="match_parent"
            android:layout_marginTop="5dp"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="亮度"/>
        <SeekBar
            android:id="@+id/SeekBar03"
            android:layout_width="match_parent"
            android:layout_marginTop="5dp"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/Button"
            android:layout_width="match_parent"
            android:layout_marginTop="20dp"
            android:layout_height="wrap_content"
            android:text="从相册获取图片"/>
    </LinearLayout>

</LinearLayout>

默认图片,可以自己找图片代替,随后在 MainActivity.java 类中修改与你的图片命名一样即可。
在这里插入图片描述

App下载:https://aifabu.com/aueM

如果嫌麻烦,给你准备了源码:

https://download.csdn.net/download/erp_lxkun_jak/11829141

我是 易君,和你一起在安卓开发之路

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值