[置顶] Android改变图像的饱和度、亮度和对比度

使用到了ColorMatrix。

Java代码:

package com.figo.imgedit; import java.io.FileNotFoundException; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.graphics.Paint; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class ImgeditActivity extends Activity { /** Called when the activity is first created. */ private Bitmap srcBitmap, dstBitmap; private String pathName = "/sdcard/testimg.jpg"; private ImageView dstimage = null; private SeekBar SaturationseekBar = null; private SeekBar BrightnessseekBar = null; private SeekBar ContrastseekBar = null; private int imgHeight, imgWidth; public static final int PICTURE = 0; public static final int MAX_WIDTH = 240; public static final int MAX_HEIGHT = 240; private Uri imageUri; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dstimage = (ImageView) findViewById(R.id.dstImageView); SaturationseekBar = (SeekBar) findViewById(R.id.Saturationseekbar); BrightnessseekBar = (SeekBar) findViewById(R.id.Brightnessseekbar); ContrastseekBar = (SeekBar) findViewById(R.id.Contrastseekbar); srcBitmap = BitmapFactory.decodeFile(pathName); dstimage.setImageBitmap(srcBitmap); imgHeight = srcBitmap.getHeight(); imgWidth = srcBitmap.getWidth(); dstBitmap = Bitmap.createBitmap(imgWidth, imgHeight, Config.ARGB_8888); SaturationseekBar .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { // 当拖动条的滑块位置发生改变时触发该方法 public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) { // 创建一个相同尺寸的可变的位图区,用于绘制调色后的图片 Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight, Config.ARGB_8888); ColorMatrix cMatrix = new ColorMatrix(); // 设置饱和度 cMatrix.setSaturation((float) (progress / 100.0)); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cMatrix)); Canvas canvas = new Canvas(bmp); // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了 canvas.drawBitmap(srcBitmap, 0, 0, paint); dstimage.setImageBitmap(bmp); } public void onStartTrackingTouch(SeekBar bar) { } public void onStopTrackingTouch(SeekBar bar) { } }); BrightnessseekBar .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { // 当拖动条的滑块位置发生改变时触发该方法 public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) { Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight, Config.ARGB_8888); int brightness = progress - 127; ColorMatrix cMatrix = new ColorMatrix(); cMatrix.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness,// 改变亮度 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 }); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cMatrix)); Canvas canvas = new Canvas(bmp); // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了 canvas.drawBitmap(srcBitmap, 0, 0, paint); dstimage.setImageBitmap(bmp); } public void onStartTrackingTouch(SeekBar bar) { } public void onStopTrackingTouch(SeekBar bar) { } }); ContrastseekBar .setOnSeekBarChangeListener(new OnSeekBarChangeListener() { // 当拖动条的滑块位置发生改变时触发该方法 public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) { Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight, Config.ARGB_8888); // int brightness = progress - 127; float contrast = (float) ((progress + 64) / 128.0); ColorMatrix cMatrix = new ColorMatrix(); cMatrix.set(new float[] { contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0,// 改变对比度 0, 0, contrast, 0, 0, 0, 0, 0, 1, 0 }); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cMatrix)); Canvas canvas = new Canvas(bmp); // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了 canvas.drawBitmap(srcBitmap, 0, 0, paint); dstimage.setImageBitmap(bmp); } public void onStartTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } }); } /** * 需要加载的图片可能是大图,我们需要对其进行合适的缩小处理 * * @param imageUri */ private Bitmap getSrcImage(Uri imageUri) { try { BitmapFactory.Options ops = new BitmapFactory.Options(); ops.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeStream(this.getContentResolver() .openInputStream(imageUri), null, ops); int wRatio = (int) Math.ceil(ops.outWidth / (float) MAX_WIDTH); int hRatio = (int) Math.ceil(ops.outHeight / (float) MAX_HEIGHT); if (wRatio > 1 && hRatio > 1) { if (wRatio > hRatio) { ops.inSampleSize = wRatio; } else { ops.inSampleSize = hRatio; } } ops.inJustDecodeBounds = false; bmp = BitmapFactory.decodeStream(this.getContentResolver() .openInputStream(imageUri), null, ops); return bmp; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e(this.getClass().getName(), e.getMessage()); } return null; } }布局文件,比较简单:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ImageView android:id="@+id/dstImageView" android:scaleType="fitCenter" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxWidth="240px" android:maxHeight="240px" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Saturation" /> <!-- 定义一个拖动条,并改变它的滑块外观 --> <SeekBar android:id="@+id/Saturationseekbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:progress="100" android:max="200" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Brightness" /> <!-- 定义一个拖动条,并改变它的滑块外观 --> <SeekBar android:id="@+id/Brightnessseekbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:progress="127" android:max="255" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/Contrast" /> <!-- 定义一个拖动条,并改变它的滑块外观 --> <SeekBar android:id="@+id/Contrastseekbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:progress="63" android:max="127" /> </LinearLayout> </LinearLayout> </LinearLayout>
运行结果:



转载于:https://www.cnblogs.com/java315/archive/2011/11/28/2397404.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值