分享

android图像处理系列之二--图片旋转、缩放、反转

 ada_lib 2015-04-10

转自:http://blog.csdn.net/maylian7700/article/details/7071837

 

注意是反转,不是翻转。贴图:

原图:



处理后:



下面看代码:

  1. package com.jacp.image.util;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Matrix;  
  5.   
  6. /** 
  7.  * 图片处理 
  8.  *  
  9.  * @author maylian7700@126.com 
  10.  *  
  11.  */  
  12. public class ImageHandler {  
  13.   
  14.     /** 
  15.      * 图片旋转 
  16.      *  
  17.      * @param bmp 
  18.      *            要旋转的图片 
  19.      * @param degree 
  20.      *            图片旋转的角度,负值为逆时针旋转,正值为顺时针旋转 
  21.      * @return 
  22.      */  
  23.     public static Bitmap rotateBitmap(Bitmap bmp, float degree) {  
  24.         Matrix matrix = new Matrix();  
  25.         matrix.postRotate(degree);  
  26.         return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);  
  27.     }  
  28.   
  29.     /** 
  30.      * 图片缩放 
  31.      *  
  32.      * @param bm 
  33.      * @param scale 
  34.      *            值小于则为缩小,否则为放大 
  35.      * @return 
  36.      */  
  37.     public static Bitmap resizeBitmap(Bitmap bm, float scale) {  
  38.         Matrix matrix = new Matrix();  
  39.         matrix.postScale(scale, scale);  
  40.         return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);  
  41.     }  
  42.   
  43.     /** 
  44.      * 图片缩放 
  45.      *  
  46.      * @param bm 
  47.      * @param w 
  48.      *            缩小或放大成的宽 
  49.      * @param h 
  50.      *            缩小或放大成的高 
  51.      * @return 
  52.      */  
  53.     public static Bitmap resizeBitmap(Bitmap bm, int w, int h) {  
  54.         Bitmap BitmapOrg = bm;  
  55.   
  56.         int width = BitmapOrg.getWidth();  
  57.         int height = BitmapOrg.getHeight();  
  58.   
  59.         float scaleWidth = ((float) w) / width;  
  60.         float scaleHeight = ((float) h) / height;  
  61.   
  62.         Matrix matrix = new Matrix();  
  63.         matrix.postScale(scaleWidth, scaleHeight);  
  64.         return Bitmap.createBitmap(BitmapOrg, 0, 0, width, height, matrix, true);  
  65.     }  
  66.   
  67.     /** 
  68.      * 图片反转 
  69.      *  
  70.      * @param bm 
  71.      * @param flag 
  72.      *            0为水平反转,1为垂直反转 
  73.      * @return 
  74.      */  
  75.     public static Bitmap reverseBitmap(Bitmap bmp, int flag) {  
  76.         float[] floats = null;  
  77.         switch (flag) {  
  78.         case 0: // 水平反转  
  79.             floats = new float[] { -1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f };  
  80.             break;  
  81.         case 1: // 垂直反转  
  82.             floats = new float[] { 1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f };  
  83.             break;  
  84.         }  
  85.   
  86.         if (floats != null) {  
  87.             Matrix matrix = new Matrix();  
  88.             matrix.setValues(floats);  
  89.             return Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);  
  90.         }  
  91.   
  92.         return null;  
  93.     }  
  94.   
  95. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多