图片旋转、缩放、反转

  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, 00, 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, 00, 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, 00, 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, 00, bmp.getWidth(), bmp.getHeight(), matrix, true);  
  90.         }  
  91.   
  92.         return null;  
  93.     }  
  94.   
  95. }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值