2018-03-18 图片处理(四)----颜色调节

2018-03-20  本文已影响0人  紫杉叶子
笔记如下


如图:
Video_2018-03-16_102658.gif

5x4 matrix for transforming the color+alpha components of a Bitmap. The matrix is stored in a single array, and its treated as follows:

  [   a,  b,  c,  d, e, //这一行专门处理红色的色系
      f,  g,  h,  i, j, //这一行专门处理绿色的色系
      k,  l,  m,  n, o, //这一行专门处理蓝色的色系
      p,  q,  r,  s, t    ] 

When applied to a color [r, g, b, a], the resulting color is computed as (after clamping)

    R' = a*R + b*G + c*B + d*A + e; 
    G' = f*R + g*G + h*B + i*A + j; 
    B' = k*R + l*G + m*B + n*A + o; 
    A' = p*R + q*G + r*B + s*A + t; //透明度

Set this colormatrix to identity:

  [ 1 0 0 0 0 - red vector
    0 1 0 0 0 - green vector 
    0 0 1 0 0 - blue vector 
    0 0 0 1 0 ] - alpha vector 

接下来就是具体的设置方法

        //redPercent,greenPercent ,bluePercent 初始为1,这样就可以动态的改变指定要改变的颜色
        if (seekBar == skb_red){
            this.redPercent = percent;
        }else if(seekBar == skb_green){
            this.greenPercent = percent;
        }else if(seekBar == skb_blue){
            this.bluePercent = percent;
        }


        Paint paint = new Paint();

        //2.去处理图片的颜色
        //设置颜色过滤
        //vactor是0-2的区间
        //下面这个是画笔颜色矩阵规则,也就是调节颜色的,
        float[] cm = new float[]{
                1 * redPercent,0,0,0,0,//red vactor
                0,1 * greenPercent,0,0,0,//green vactor
                0,0,1 * bluePercent,0,0,//blue vactor
                0,0,0,1,0,//alpha vactor
        };
        paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(cm)));




实现源码:
layout中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.chen.color.MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="靑----------------------------------------红"
        />
    <SeekBar
        android:progress="50"
        android:id="@+id/skb_red"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="紫---------------------------------------绿"
        />
    <SeekBar
        android:progress="50"
        android:id="@+id/skb_green"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="黄--------------------------------------蓝"
        />
    <SeekBar
        android:progress="50"
        android:id="@+id/skb_blue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv"
        />

</LinearLayout>

MainActivity中

package com.chen.color;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;



public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    private ImageView iv;
    private SeekBar skb_red;
    private SeekBar skb_green;
    private SeekBar skb_blue;
    private Bitmap srcBitmap;

    private float redPercent = 1f;
    private float greenPercent = 1f;
    private float bluePercent = 1f;

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

        iv = (ImageView) findViewById(R.id.iv);

        skb_red = (SeekBar) findViewById(R.id.skb_red);
        skb_green = (SeekBar) findViewById(R.id.skb_green);
        skb_blue = (SeekBar) findViewById(R.id.skb_blue);

        //andorid6.0及以上,需要动态过去权限
        requestPermission();

        skb_red.setOnSeekBarChangeListener(this);
        skb_green.setOnSeekBarChangeListener(this);
        skb_blue.setOnSeekBarChangeListener(this);


        String path = "storage/emulated/0/6.jpg";
        srcBitmap = BitmapFactory.decodeFile(path);

        Bitmap copyBitmap = Bitmap.createBitmap(srcBitmap.getWidth()*2,srcBitmap.getHeight()*2,srcBitmap.getConfig());
        Canvas canvas = new Canvas(copyBitmap);
        Paint paint = new Paint();
        Matrix matrix = new Matrix();
        matrix.setScale(2f,2f);
        canvas.drawBitmap(srcBitmap,matrix,paint);


        iv.setImageBitmap(copyBitmap);

    }

    private void requestPermission(){
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ //表示未授权时
            //进行授权
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
        }else{
            //调用打电话的方法
            Toast.makeText(this, "已经获取了..", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        // seek的进度改变的回调

        //停止拖动seekbar的回调
        
        float percent = progress / 50f;


        if (seekBar == skb_red){
            this.redPercent = percent;
        }else if(seekBar == skb_green){
            this.greenPercent = percent;
        }else if(seekBar == skb_blue){
            this.bluePercent = percent;
        }



        //去改变图片的颜色

        //1.去获得拷贝
        Bitmap copyBitmap = Bitmap.createBitmap(srcBitmap.getWidth()*2,srcBitmap.getHeight()*2,srcBitmap.getConfig());

        Canvas canvas = new Canvas(copyBitmap);
        Paint paint = new Paint();

        //2.去处理图片的颜色
        //设置颜色过滤
        //vactor是0-2的区间
        //下面这个是画笔颜色矩阵规则,也就是调节颜色的,
        float[] cm = new float[]{
                1 * redPercent,0,0,0,0,//red vactor
                0,1 * greenPercent,0,0,0,//green vactor
                0,0,1 * bluePercent,0,0,//blue vactor
                0,0,0,1,0,//alpha vactor
        };
        paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(cm)));

        Matrix matrix = new Matrix();
        matrix.setScale(2f,2f);


        canvas.drawBitmap(srcBitmap,matrix,paint);


        //3.将处理的结果展示
        iv.setImageBitmap(copyBitmap);


    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        //开始拖动seekBar的回调


    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        /*//停止拖动seekbar的回调

        int progress = seekBar.getProgress();
        float percent = progress / 50f;


        if (seekBar == skb_red){
            this.redPercent = percent;
        }else if(seekBar == skb_green){
            this.greenPercent = percent;
        }else if(seekBar == skb_blue){
            this.bluePercent = percent;
        }



        //去改变图片的颜色

        //1.去获得拷贝
        Bitmap copyBitmap = Bitmap.createBitmap(srcBitmap.getWidth()*2,srcBitmap.getHeight()*2,srcBitmap.getConfig());

        Canvas canvas = new Canvas(copyBitmap);
        Paint paint = new Paint();

        //2.去处理图片的颜色
        //设置颜色过滤
        //vactor是0-2的区间
        //下面这个是画笔颜色矩阵规则,也就是调节颜色的,
        float[] cm = new float[]{
                1 * redPercent,0,0,0,0,//red vactor
                0,1 * greenPercent,0,0,0,//green vactor
                0,0,1 * bluePercent,0,0,//blue vactor
                0,0,0,1,0,//alpha vactor
        };
        paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(cm)));

        Matrix matrix = new Matrix();
        matrix.setScale(2f,2f);


        canvas.drawBitmap(srcBitmap,matrix,paint);


        //3.将处理的结果展示
        iv.setImageBitmap(copyBitmap);*/

    }
}

上一篇下一篇

猜你喜欢

热点阅读