Android改变图像的饱和度、亮度和对比度

来源:互联网 发布:linux中打开一个文件 编辑:程序博客网 时间:2024/05/13 01:40

转自:http://blog.csdn.net/sxwyf248/article/details/7019731


运行结果:




使用到了ColorMatrix。

Java代码:

[java] view plaincopy
  1. package com.figo.imgedit;  
  2.   
  3. import java.io.FileNotFoundException;  
  4.   
  5. import android.app.Activity;  
  6. import android.graphics.Bitmap;  
  7. import android.graphics.Bitmap.Config;  
  8. import android.graphics.BitmapFactory;  
  9. import android.graphics.Canvas;  
  10. import android.graphics.ColorMatrix;  
  11. import android.graphics.ColorMatrixColorFilter;  
  12. import android.graphics.Paint;  
  13. import android.net.Uri;  
  14. import android.os.Bundle;  
  15. import android.util.Log;  
  16. import android.widget.ImageView;  
  17. import android.widget.SeekBar;  
  18. import android.widget.SeekBar.OnSeekBarChangeListener;  
  19.   
  20. public class ImgeditActivity extends Activity {  
  21.     /** Called when the activity is first created. */  
  22.     private Bitmap srcBitmap, dstBitmap;  
  23.     private String pathName = "/sdcard/testimg.jpg";  
  24.   
  25.     private ImageView dstimage = null;  
  26.     private SeekBar SaturationseekBar = null;  
  27.     private SeekBar BrightnessseekBar = null;  
  28.     private SeekBar ContrastseekBar = null;  
  29.     private int imgHeight, imgWidth;  
  30.   
  31.     public static final int PICTURE = 0;  
  32.     public static final int MAX_WIDTH = 240;  
  33.     public static final int MAX_HEIGHT = 240;  
  34.     private Uri imageUri;  
  35.   
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.main);  
  40.   
  41.         dstimage = (ImageView) findViewById(R.id.dstImageView);  
  42.         SaturationseekBar = (SeekBar) findViewById(R.id.Saturationseekbar);  
  43.         BrightnessseekBar = (SeekBar) findViewById(R.id.Brightnessseekbar);  
  44.         ContrastseekBar = (SeekBar) findViewById(R.id.Contrastseekbar);  
  45.   
  46.         srcBitmap = BitmapFactory.decodeFile(pathName);  
  47.         dstimage.setImageBitmap(srcBitmap);  
  48.         imgHeight = srcBitmap.getHeight();  
  49.         imgWidth = srcBitmap.getWidth();  
  50.   
  51.         dstBitmap = Bitmap.createBitmap(imgWidth, imgHeight, Config.ARGB_8888);  
  52.   
  53.         SaturationseekBar  
  54.                 .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  55.                     // 当拖动条的滑块位置发生改变时触发该方法  
  56.                     public void onProgressChanged(SeekBar arg0, int progress,  
  57.                             boolean fromUser) {  
  58.                         // 创建一个相同尺寸的可变的位图区,用于绘制调色后的图片  
  59.                         Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight,  
  60.                                 Config.ARGB_8888);  
  61.                         ColorMatrix cMatrix = new ColorMatrix();  
  62.                         // 设置饱和度  
  63.                         cMatrix.setSaturation((float) (progress / 100.0));  
  64.   
  65.                         Paint paint = new Paint();  
  66.                         paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));  
  67.   
  68.                         Canvas canvas = new Canvas(bmp);  
  69.                         // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了  
  70.                         canvas.drawBitmap(srcBitmap, 00, paint);  
  71.   
  72.                         dstimage.setImageBitmap(bmp);  
  73.   
  74.                     }  
  75.   
  76.                     public void onStartTrackingTouch(SeekBar bar) {  
  77.                     }  
  78.   
  79.                     public void onStopTrackingTouch(SeekBar bar) {  
  80.                     }  
  81.                 });  
  82.   
  83.         BrightnessseekBar  
  84.                 .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  85.                     // 当拖动条的滑块位置发生改变时触发该方法  
  86.                     public void onProgressChanged(SeekBar arg0, int progress,  
  87.                             boolean fromUser) {  
  88.                         Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight,  
  89.                                 Config.ARGB_8888);  
  90.                         int brightness = progress - 127;  
  91.                         ColorMatrix cMatrix = new ColorMatrix();  
  92.                         cMatrix.set(new float[] { 1000, brightness, 01,  
  93.                                 00, brightness,// 改变亮度  
  94.                                 0010, brightness, 00010 });  
  95.   
  96.                         Paint paint = new Paint();  
  97.                         paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));  
  98.   
  99.                         Canvas canvas = new Canvas(bmp);  
  100.                         // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了  
  101.                         canvas.drawBitmap(srcBitmap, 00, paint);  
  102.                         dstimage.setImageBitmap(bmp);  
  103.   
  104.                     }  
  105.   
  106.                     public void onStartTrackingTouch(SeekBar bar) {  
  107.                     }  
  108.   
  109.                     public void onStopTrackingTouch(SeekBar bar) {  
  110.                     }  
  111.                 });  
  112.   
  113.         ContrastseekBar  
  114.                 .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  115.                     // 当拖动条的滑块位置发生改变时触发该方法  
  116.                     public void onProgressChanged(SeekBar arg0, int progress,  
  117.                             boolean fromUser) {  
  118.                         Bitmap bmp = Bitmap.createBitmap(imgWidth, imgHeight,  
  119.                                 Config.ARGB_8888);  
  120.                         // int brightness = progress - 127;  
  121.                         float contrast = (float) ((progress + 64) / 128.0);  
  122.                         ColorMatrix cMatrix = new ColorMatrix();  
  123.                         cMatrix.set(new float[] { contrast, 00000,  
  124.                                 contrast, 000,// 改变对比度  
  125.                                 00, contrast, 0000010 });  
  126.   
  127.                         Paint paint = new Paint();  
  128.                         paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));  
  129.   
  130.                         Canvas canvas = new Canvas(bmp);  
  131.                         // 在Canvas上绘制一个已经存在的Bitmap。这样,dstBitmap就和srcBitmap一摸一样了  
  132.                         canvas.drawBitmap(srcBitmap, 00, paint);  
  133.   
  134.                         dstimage.setImageBitmap(bmp);  
  135.                     }  
  136.   
  137.                     public void onStartTrackingTouch(SeekBar arg0) {  
  138.                         // TODO Auto-generated method stub  
  139.   
  140.                     }  
  141.   
  142.                     public void onStopTrackingTouch(SeekBar seekBar) {  
  143.                         // TODO Auto-generated method stub  
  144.                     }  
  145.                 });  
  146.     }  
  147.   
  148.     /** 
  149.      * 需要加载的图片可能是大图,我们需要对其进行合适的缩小处理 
  150.      *  
  151.      * @param imageUri 
  152.      */  
  153.     private Bitmap getSrcImage(Uri imageUri) {  
  154.         try {  
  155.             BitmapFactory.Options ops = new BitmapFactory.Options();  
  156.             ops.inJustDecodeBounds = true;  
  157.             Bitmap bmp = BitmapFactory.decodeStream(this.getContentResolver()  
  158.                     .openInputStream(imageUri), null, ops);  
  159.             int wRatio = (int) Math.ceil(ops.outWidth / (float) MAX_WIDTH);  
  160.             int hRatio = (int) Math.ceil(ops.outHeight / (float) MAX_HEIGHT);  
  161.   
  162.             if (wRatio > 1 && hRatio > 1) {  
  163.                 if (wRatio > hRatio) {  
  164.                     ops.inSampleSize = wRatio;  
  165.                 } else {  
  166.                     ops.inSampleSize = hRatio;  
  167.                 }  
  168.             }  
  169.   
  170.             ops.inJustDecodeBounds = false;  
  171.             bmp = BitmapFactory.decodeStream(this.getContentResolver()  
  172.                     .openInputStream(imageUri), null, ops);  
  173.   
  174.             return bmp;  
  175.   
  176.         } catch (FileNotFoundException e) {  
  177.             // TODO Auto-generated catch block  
  178.             e.printStackTrace();  
  179.             Log.e(this.getClass().getName(), e.getMessage());  
  180.         }  
  181.   
  182.         return null;  
  183.     }  
  184. }  
布局文件,比较简单:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  4.     android:orientation="vertical">  
  5.   
  6.     <ImageView android:id="@+id/dstImageView" android:scaleType="fitCenter"  
  7.         android:layout_width="fill_parent" android:layout_height="wrap_content"  
  8.         android:maxWidth="240px" android:maxHeight="240px" />  
  9.   
  10.     <LinearLayout android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content" android:orientation="vertical">  
  12.         <LinearLayout android:layout_width="fill_parent"  
  13.             android:layout_height="wrap_content" android:orientation="horizontal"  
  14.             android:layout_alignParentBottom="true" android:gravity="center_vertical">  
  15.   
  16.             <TextView android:layout_width="wrap_content"  
  17.                 android:layout_height="wrap_content" android:text="@string/Saturation" />  
  18.   
  19.             <!-- 定义一个拖动条,并改变它的滑块外观 -->  
  20.             <SeekBar android:id="@+id/Saturationseekbar"  
  21.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  22.                 android:progress="100" android:max="200" />  
  23.         </LinearLayout>  
  24.   
  25.         <LinearLayout android:layout_width="fill_parent"  
  26.             android:layout_height="wrap_content" android:orientation="horizontal"  
  27.             android:gravity="center_vertical">  
  28.   
  29.             <TextView android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content" android:text="@string/Brightness" />  
  31.   
  32.             <!-- 定义一个拖动条,并改变它的滑块外观 -->  
  33.             <SeekBar android:id="@+id/Brightnessseekbar"  
  34.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  35.                 android:progress="127" android:max="255" />  
  36.         </LinearLayout>  
  37.   
  38.         <LinearLayout android:layout_width="fill_parent"  
  39.             android:layout_height="wrap_content" android:orientation="horizontal"  
  40.             android:gravity="center_vertical">  
  41.   
  42.             <TextView android:layout_width="wrap_content"  
  43.                 android:layout_height="wrap_content" android:text="@string/Contrast" />  
  44.   
  45.             <!-- 定义一个拖动条,并改变它的滑块外观 -->  
  46.             <SeekBar android:id="@+id/Contrastseekbar"  
  47.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  48.                 android:progress="63" android:max="127" />  
  49.         </LinearLayout>  
  50.     </LinearLayout>  
  51. </LinearLayout>  



0 0