关闭

Android图像处理(一)色调、饱和度、亮度

标签: 图像处理Android色调饱和度亮度
2208人阅读 评论(0) 收藏 举报
分类:


色调

ColorMatrix hueMatrix = new ColorMatrix();

hueMatrix.setRotate(0, hue); 0-代表R

hueMatrix.setRotate(1, hue); 1-代表G

hueMatrix.setRotate(2, hue); 2-代表B


饱和度

ColorMatrix saturationMatrix = new ColorMatrix();

saturationMatrix.setSaturation(saturation);


亮度

ColorMatrix lumMatrix = new ColorMatrix();

lumMatrix.setScale(lum, lum, lum, 1);


public class PrimaryColor extends Activity implements SeekBar.OnSeekBarChangeListener {

    private ImageView mImageView;

    private SeekBar mSeekbarhue, mSeekbarSaturation, mSeekbarLum;

    private static int MAX_VALUE = 255;
    private static int MID_VALUE = 127;

    private float mHue, mSaturation, mLum;

    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.primary_color);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test3);
        mImageView = (ImageView) findViewById(R.id.imageView);
        mSeekbarhue = (SeekBar) findViewById(R.id.seekbarHue);
        mSeekbarSaturation = (SeekBar) findViewById(R.id.seekbarSaturation);
        mSeekbarLum = (SeekBar) findViewById(R.id.seekbarLum);

        mSeekbarhue.setOnSeekBarChangeListener(this);
        mSeekbarSaturation.setOnSeekBarChangeListener(this);
        mSeekbarLum.setOnSeekBarChangeListener(this);

        mSeekbarhue.setMax(MAX_VALUE);
        mSeekbarSaturation.setMax(MAX_VALUE);
        mSeekbarLum.setMax(MAX_VALUE);

        mSeekbarhue.setProgress(MID_VALUE);
        mSeekbarSaturation.setProgress(MID_VALUE);
        mSeekbarLum.setProgress(MID_VALUE);

        mImageView.setImageBitmap(bitmap);
    }

    @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:
                //饱和度
                mSaturation = progress * 1.0F / MID_VALUE;
                break;
            case R.id.seekbarLum:
                //亮度
                mLum = progress * 1.0F / MID_VALUE;
                break;
        }
        mImageView.setImageBitmap(ImageHelper.handleImageEffect(bitmap, mHue, mSaturation, mLum));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}



实现步骤

1.实现静态方法

2.创建Bitmap对象

3.创建画笔对象

4.调整图像效果

5.返回图像


public class ImageHelper {

    /**
     *
     * @param bm 图像 (不可修改)
     * @param hue 色相
     * @param saturation 饱和度
     * @param lum 亮度
     * @return
     */
    public static Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, floatlum) {

        Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bmp);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0, hue); // R
        hueMatrix.setRotate(1, hue); // G
        hueMatrix.setRotate(2, hue); // B

        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);

        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum, lum, lum, 1);

        //融合
        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);

        paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        canvas.drawBitmap(bm, 0, 0, paint);

        return bmp;
    }

}

xml代码中设置控件的监听事件,在Button标签里设置onClick属性调用需要执行的方法,该方法:

访问权限必须是public类型

必须无返回值

方法名要与xml定义方法名相同

参数列表必须是View类型的参数


图像处理,其实就是研究不同的颜色矩阵对图像的处理效果


public class ColorMatrix extends Activity {

    private ImageView mImageView;
    private GridLayout mGroup;
    private Bitmap bitmap;

    private int mEtWidth, mEtHeight;

    private EditText[] mEts = new EditText[20];

    //对应颜色矩阵
    private float[] mColorMatrix = new float[20];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.color_matrix);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test1);
        mImageView = (ImageView) findViewById(R.id.imageView);
        mGroup = (GridLayout) findViewById(R.id.group);
        mImageView.setImageBitmap(bitmap);

        //为什么我的代码中自动优化成Lambda表达式了??? = =
        mGroup.post(new Runnable() {

            @Override
            public void run() {
                //控件绘制完成后再去获取宽高
                //若直接在onCreate中去获取
                //则会得到两个0
                //绘制后执行
                mEtWidth = mGroup.getWidth() / 5;
                mEtHeight = mGroup.getHeight() / 5;
                addEts();
                initMatrix();//赋值
            }
        });
    }

    private void getMatrix() {
        for (int i = 0; i < 20; i++) {
            mColorMatrix[i] = Float.valueOf(mEts[i].getText().toString());
        }
    }

    private void setImageMatrix() {
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        android.graphics.ColorMatrix colorMatrix = new android.graphics.ColorMatrix();
        colorMatrix.set(mColorMatrix);

        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, 0, 0, paint);
        mImageView.setImageBitmap(bmp);
    }

    public void btnChange(View view) {
        getMatrix();
        setImageMatrix();
    }

    public void btnReset(View view) {
        initMatrix();
        getMatrix();
        setImageMatrix();
    }

    private void addEts() {
        for (int i = 0; i < 20; i++) {
            EditText editText = new EditText(ColorMatrix.this);
            mEts[i] = editText;
            mGroup.addView(editText, mEtWidth, mEtHeight);
        }
    }

    /**
     * 初始矩阵
     * 4行5列
     * 0/6/12/18 为1
     * 其余为0
     */
    private void initMatrix() {
        for (int i = 0; i < 20; i++) {
            if (i % 6 == 0) {
                mEts[i].setText(String.valueOf(1));
            } else {
                mEts[i].setText(String.valueOf(0));
            }
        }
    }

}




怀旧效果

|0.393 0.769 0.189 0 0|

|0.349 0.686 0.168 0 0|

|0.272 0.534 0.131 0 0|

|0         0         0         1 0|


https://github.com/LiuchangDuan/demo


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:68477次
    • 积分:1896
    • 等级:
    • 排名:千里之外
    • 原创:120篇
    • 转载:45篇
    • 译文:0篇
    • 评论:7条
    最新评论