图片的处理包括以下:

1, 缩放

2, 倾斜

3, 旋转

4, 缩放+

5, 平移

6, 镜像

代码如下:

 

 
  
  1. package com.mike.activity; 
  2.  
  3. import java.io.FileNotFoundException; 
  4.  
  5. import android.app.Activity; 
  6. import android.content.Intent; 
  7. import android.graphics.Bitmap; 
  8. import android.graphics.BitmapFactory; 
  9. import android.graphics.BitmapFactory.Options; 
  10. import android.graphics.Canvas; 
  11. import android.graphics.Color; 
  12. import android.graphics.Matrix; 
  13. import android.graphics.Paint; 
  14. import android.net.Uri; 
  15. import android.os.Bundle; 
  16. import android.util.Log; 
  17. import android.view.Display; 
  18. import android.view.View; 
  19. import android.view.View.OnClickListener; 
  20. import android.widget.Button; 
  21. import android.widget.ImageView; 
  22.  
  23. public class ImageDemoActivity extends Activity implements OnClickListener { 
  24.     /** Called when the activity is first created. */ 
  25.  
  26.     public static String TAG = "IMAGE"
  27.     public static int REQUEST_CODE = 0
  28.     private ImageView mImageShow; 
  29.     private ImageView mImageAltered; 
  30.  
  31.     @Override 
  32.     public void onCreate(Bundle savedInstanceState) { 
  33.         super.onCreate(savedInstanceState); 
  34.         setContentView(R.layout.main); 
  35.  
  36.         Button p_w_picpathSelectBtn = (Button) findViewById(R.id.p_w_picpathSelectBtn); 
  37.         mImageShow = (ImageView) findViewById(R.id.p_w_picpathShow); 
  38.         mImageAltered = (ImageView) findViewById(R.id.p_w_picpathAltered); 
  39.         p_w_picpathSelectBtn.setOnClickListener(this); 
  40.     } 
  41.  
  42.     public void onClick(View v) { 
  43.         // TODO Auto-generated method stub 
  44.         Intent intent = new Intent(Intent.ACTION_PICK, 
  45.                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 启动照片Gallery 
  46.         startActivityForResult(intent, 0); 
  47.     } 
  48.  
  49.     @Override 
  50.     protected void onActivityResult(int requestCode, int resultCode, 
  51.             Intent intent) { 
  52.         // TODO Auto-generated method stub 
  53.         super.onActivityResult(requestCode, resultCode, intent); 
  54.  
  55.         if (resultCode == RESULT_OK) {// 操作成功 
  56.             Uri imgFileUri = intent.getData();// 获得所选照片的信息 
  57.             Log.d(TAG, "imgFileUri is :" + imgFileUri); 
  58.             // 由于返回的图像可能太大而无法完全加载到内存中。系统有限制,需要处理。 
  59.             Display currentDisplay = getWindowManager().getDefaultDisplay(); 
  60.             int defaultHeight = currentDisplay.getHeight(); 
  61.             int defaultWidth = currentDisplay.getWidth(); 
  62.  
  63.             BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options(); 
  64.             bitmapFactoryOptions.inJustDecodeBounds = false;// /只是为获取原始图片的尺寸,而不返回Bitmap对象 
  65.             // 注上:If set to true, the decoder will return null (no bitmap), but 
  66.             // the out... fields will still be set, 
  67.             // allowing the caller to query the bitmap without having to 
  68.             // allocate the memory for its pixels 
  69.             try { 
  70.                 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver() 
  71.                         .openInputStream(imgFileUri), null
  72.                         bitmapFactoryOptions); 
  73.                 int outHeight = bitmapFactoryOptions.outHeight; 
  74.                 int outWidth = bitmapFactoryOptions.outWidth; 
  75.                 int heightRatio = (int) Math.ceil((float) outHeight 
  76.                         / defaultHeight); 
  77.                 int widthRatio = (int) Math.ceil((float) outWidth 
  78.                         / defaultWidth); 
  79.  
  80.                 if (heightRatio > 1 || widthRatio > 1) { 
  81.                     if (heightRatio > widthRatio) { 
  82.                         bitmapFactoryOptions.inSampleSize = heightRatio; 
  83.                     } else { 
  84.                         bitmapFactoryOptions.inSampleSize = widthRatio; 
  85.                     } 
  86.                 } 
  87.  
  88.                 bitmapFactoryOptions.inJustDecodeBounds = false
  89.                 bitmap = BitmapFactory.decodeStream(getContentResolver() 
  90.                         .openInputStream(imgFileUri), null
  91.                         bitmapFactoryOptions); 
  92.  
  93.                 mImageShow.setImageBitmap(bitmap); 
  94.  
  95.                 // /* 
  96.                 // * 在位图上绘制位图 
  97.                 // */ 
  98.                 // 
  99.                 // Bitmap bitmapAltered = Bitmap.createBitmap(bitmap.getWidth(), 
  100.                 // bitmap.getHeight(), bitmap.getConfig()); 
  101.                 // 
  102.                 // Canvas canvas = new 
  103.                 // Canvas(bitmapAltered);//bitmap提供了画布,只在此提供了大小尺寸,偏移后并未有背景显示出来 
  104.                 // 
  105.                 // 
  106.                 // Paint paint = new Paint(); 
  107.                 // 
  108.                 // canvas.drawBitmap(bitmap, 0, 0, paint);//绘制的图片和之前的一模一样 
  109.                 // 
  110.                 // mImageAltered.setImageBitmap(bitmapAltered); 
  111.  
  112.                 /* 
  113.                  * 使用矩阵 
  114.                  */ 
  115.  
  116.                 Bitmap bitmapAltered = Bitmap.createBitmap(bitmap.getWidth(), 
  117.                         bitmap.getHeight(), bitmap.getConfig());// 缺点是限定画布大小,处理后的图片可能被截断,可引入矩阵构造方法,动态设定画布大小 
  118.                 Canvas canvas = new Canvas(bitmapAltered);// bitmap提供了画布,只在此提供了大小尺寸,偏移后并未有背景显示出来, 
  119.                 Paint paint = new Paint(); 
  120.  
  121.                 Matrix matrix = new Matrix(); 
  122.  
  123.                 // 1:缩放 
  124.                 // matrix.setValues(new float[] {//涉及线性代数的知识,可以翻翻相乘的基础知识 
  125.                 // 1,0,0,//算出x的值 
  126.                 // 0,1,0,//算出y的值 
  127.                 // 0,0,1//二维图用不到 
  128.                 // }); 
  129.  
  130.                 // 2:倾斜 
  131.                 // matrix.setValues(new float[] {//涉及线性代数的知识,可以翻翻相乘的基础知识 
  132.                 // 1,.5f,0,//算出x的值,x,y值相关 
  133.                 // 0,1,0,//算出y的值 
  134.                 // 0,0,1//二维图用不到 
  135.                 // }); 
  136.  
  137.                 // 3:旋转 
  138.                 // matrix.setRotate(45);//设置旋转角度,(0,0)点,顺时针旋转45度 
  139.  
  140.                 // 4,缩放plus:使用Matrix类方法//需要修改画布宽度>1.5倍* 原始宽度 
  141.                 // matrix.setScale(1.5f, 1); 
  142.  
  143.                 // 5,平移 
  144.                 // matrix.setTranslate(10, 0); 
  145.  
  146.                 // 6,镜像 
  147.                 // eg1:x轴镜像 
  148.                 // matrix.setScale(-1, 1);//向左绘制,原始图片沿y轴对称 
  149.                 // matrix.postTranslate(bitmap.getWidth(), 0);//注意是post即后移动 
  150.                 // eg2:y轴镜像 
  151.                 matrix.setScale(1, -1);// 向上绘制,原始图片沿x轴对称,图像在第四象限 
  152.                 matrix.postTranslate(0, bitmap.getHeight());// 注意是post即后移动 
  153.                  
  154.                  
  155.                 //~optimize~因为指定画布大小bitmapAltered,所以处理后的图片可能被截断,可以通过矩阵动态改变bitmapAltered大小 
  156.                 //eg: matrix.setRotate(degrees, px, py) 
  157.                 //bitmapAltered = Bitmap.createBitmap(source, x, y, width, height, m, filter);//显然矩阵影响其大小 
  158.  
  159.                 // 
  160.  
  161.                 // 注:for more details :Wikipedia Transformation Matrix 
  162.                 // http://en.wikipedia.org/wiki/Transformation_matrix 
  163.  
  164.                 canvas.drawBitmap(bitmap, matrix, paint);// 绘制的图片和之前的一模一样 
  165.  
  166.                 mImageAltered.setImageBitmap(bitmapAltered); 
  167.  
  168.             } catch (FileNotFoundException e) { 
  169.                 // TODO Auto-generated catch block 
  170.                 e.printStackTrace(); 
  171.             } 
  172.  
  173.         } 
  174.     }