android图片滤镜功能实现

mac2024-10-11  50

1. ColorMatrix实现

系统使用一个颜色矩阵——ColorMatrix,来处理图像的色彩效果。在色彩处理中,通常使用以下三个角度来描述一个图像。

色调——物体传播的颜色饱和度——颜色的纯度,从0(灰)到100%(饱和)来进行描述亮度——颜色的相对明暗程度

代码实现:

float mHue = 0.0f;//色调 float mSaturation = 1f;//饱和度 float mLum = 1f;//亮度 float MID_VALUE; Bitmap oriBitmap,newBitmap; MID_VALUE = barHue.getMax() * 1.0F / 2; oriBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.as); //Android系统不允许直接修改原图 newBitmap = Bitmap.createBitmap(oriBitmap.getWidth(), oriBitmap.getHeight(), Bitmap.Config.ARGB_8888); barHue.setOnSeekBarChangeListener(this); barSaturation.setOnSeekBarChangeListener(this); barLum.setOnSeekBarChangeListener(this); @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; } iv_photo.setImageBitmap(handleImageEffect(oriBitmap,newBitmap, mHue, mSaturation, mLum)); tvValue.setText(mHue + "..." + mSaturation + "..." + mLum); } public Bitmap handleImageEffect(Bitmap oriBmp, Bitmap bmp,float hue, float saturation, float lum) { Canvas canvas = new Canvas(bmp); Paint paint = new Paint(); ColorMatrix hueMatrix = new ColorMatrix(); hueMatrix.setRotate(0, hue); 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); ColorMatrix imageMatrix = new ColorMatrix(); imageMatrix.postConcat(hueMatrix); imageMatrix.postConcat(saturationMatrix); imageMatrix.postConcat(lumMatrix); paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix)); canvas.drawBitmap(oriBmp, 0, 0, paint); return bmp; } <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical"> <ImageView android:id="@+id/iv_photo" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:src="@drawable/as"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="20dp" android:text="色调" android:textColor="@android:color/black" /> <SeekBar android:id="@+id/seekbarHue" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="200" android:progress="100"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:text="饱和度" android:textColor="@android:color/black" /> <SeekBar android:id="@+id/seekbarSaturation" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="200" android:progress="100"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:text="亮度" android:textColor="@android:color/black" /> <SeekBar android:id="@+id/seekbarLum" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="200" android:progress="100"/> <TextView android:id="@+id/value" android:layout_width="wrap_content" android:text="" android:layout_height="wrap_content"/> </LinearLayout>

2. GPUImage实现

GPUImage 是一个开源的图像渲染的库,使用它可以轻松实现很多滤镜效果,也可以很轻松的定义和实现自己特有的滤镜效果。

地址:https://github.com/cats-oss/android-gpuimage

最新回复(0)