歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android改變圖像的飽和度、亮度和對比度

使用到了ColorMatrix。

Java代碼:

  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. }  
Copyright © Linux教程網 All Rights Reserved