【Android图像处理】图像处理之-色相、饱和度、亮度调节
在Android中提供了不同的通道对图片的色相、饱和度、亮度值进行修改,并且进行其中一个修改的时候不会影响其他的值。因此可以将三种调节的代码放到同一个方法中。
下面是具体的代码:
/** * @param bm * @param hue 色相 * @param saturation 饱和度 * @param lum 亮度 * @return */ public static Bitmap ImageEffect(Bitmap bm,float hue,float saturation,float lum){ Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(),bm.getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //色相调节 ColorMatrix hueMatrix = new ColorMatrix(); hueMatrix.setRotate(0, hue); hueMatrix.setRotate(1, hue); hueMatrix.setRotate(2, hue); //饱和度调节 ColorMatrix saturationColorMatrix = new ColorMatrix(); saturationColorMatrix.setSaturation(saturation); //亮度调节 ColorMatrix lumMatrix = new ColorMatrix(); lumMatrix.setScale(lum, lum, lum, 1); ColorMatrix ImageMatrix = new ColorMatrix(); ImageMatrix.postConcat(hueMatrix); ImageMatrix.postConcat(saturationColorMatrix); ImageMatrix.postConcat(lumMatrix); paint.setColorFilter(new ColorMatrixColorFilter(ImageMatrix)); canvas.drawBitmap(bm, 0, 0, paint); return bitmap; }调用的时候用seekbar获取调节的参数:
方法如下:
//调节seekbar的值 private final class FliterClick3 implements OnSeekBarChangeListener{ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { switch (seekBar.getId()) { case 0://色相 Pix[0] = (progress - MID_VALUE) * 1.0F / MID_VALUE * 180; ProcessingImage task1 = new ProcessingImage(); task1.execute(0,7); break; case 1://饱和度 Pix[1] = progress * 1.0F / MID_VALUE; ProcessingImage task2 = new ProcessingImage(); task2.execute(0,7); break; case 2://亮度 Pix[2] = progress * 1.0F / MID_VALUE; ProcessingImage task3 = new ProcessingImage(); task3.execute(0,7); break; } } @Override public void onStartTrackingTouch(SeekBar arg0) { } @Override public void onStopTrackingTouch(SeekBar arg0) { } }其中Pix是一个整型数组,用来存放seekBar的值。
值得注意的是在实例化seekBar的时候要设置其最大值。
调节的效果如下:
色相调节:
饱和度调节:
亮度调节:
这里我只调了几个数值,其实还可以联合调节的。