bitmap的一些常用操作:读图片文件、图片缩放和旋转、将图片二进制转化为bitmap,以及图片加密,解密

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.Log;
/**
 * bitmap的一些常用操作:读图片文件、图片缩放和旋转、将图片二进制转化为bitmap,以及图片加密,解密
 */
public class BitmapUtil {


 private static String TAG = BitmapUtil.class.getName();
    /**
     * 图片二进制转为Bitmap
     * @param byte[] bytes  图片二进制
     * @return Bitmap  位图
     */
    public static Bitmap getPicFromBytes(byte[] bytes,
            BitmapFactory.Options opts) {


        if (bytes != null)
            if (opts != null)
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
                        opts);
            else
                return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        return null;
    }   
    
    /**
    * 加密
    * @param byteContent 待加密二进制
    * @param password    密码
    * @return   byte[]   加密后的二进制
    */
    public static byte[]  encrypt(byte [] byteContent, String password) { 
        byte[] result=null;
        try {
              KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
              kgen.init(128, new SecureRandom(password.getBytes())); 
              SecretKey secretKey = kgen.generateKey(); 
              byte[] enCodeFormat = secretKey.getEncoded();       
              SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");         
              Cipher cipher = Cipher.getInstance("AES");// 创建密码器 
              //byte[] byteContent = content.getBytes("utf-8");               
              cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 
              result = cipher.doFinal(byteContent);         
              return result; // 加密 
       } catch (Exception e) {
           e.printStackTrace();
       }
        return result;
    }
   /**
    * 解密
    * @param content   待解密二进制
    * @param password  密码
    * @return  byte [] 解密后的二进制
    */
    public static byte[] decrypt(byte[] content, String password){
        byte[] result =null;
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
            kgen.init(128, new SecureRandom(password.getBytes()));  
            SecretKey secretKey = kgen.generateKey(); 
            byte[] enCodeFormat = secretKey.getEncoded(); 
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); 
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器 
            cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 
            result= cipher.doFinal(content); 
            return result; // 加密 Svn中文网 nm 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /** 
     * Drawable 转 bitmap 
     * @param drawable 
     * @return 
     */  
    public static Bitmap drawable2Bitmap(Drawable drawable){  
        if(drawable instanceof BitmapDrawable){  
                 return ((BitmapDrawable)drawable).getBitmap() ;  
           }else if(drawable instanceof NinePatchDrawable){  
               Bitmap bitmap = Bitmap  
                       .createBitmap(  
                              drawable.getIntrinsicWidth(),  
                               drawable.getIntrinsicHeight(),  
                                 drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
                                       : Bitmap.Config.RGB_565);  
                Canvas canvas = new Canvas(bitmap);  
               drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),  
                        drawable.getIntrinsicHeight());  
               drawable.draw(canvas);  
                 return bitmap;  
             }else{  
                 return null ;  
             }  
     }  
    /**
     * 以最省内存的方式读取本地资源的图片
     * @param context
     * @param resId
     * @return
     */   
     public static Bitmap readBitMap(File file){  
         BitmapFactory.Options opt = new BitmapFactory.Options();  
         opt.inPreferredConfig = Bitmap.Config.RGB_565;   
         opt.inPurgeable = true;  
         opt.inInputShareable = true;  
         //缩放的比例,缩放是很难按准备的比例进行缩放的,其值表明缩放的倍数,SDK中建议其值是2的指数值,值越大会导致图片不清晰
         opt.inSampleSize = 2;//图片宽高都为原来的二分之一,即图片为原来的四分之一


           //获取资源图片  
         FileInputStream is = null;
          try {
is = new FileInputStream(file);
// is = context.openFileInput(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}  
           return BitmapFactory.decodeStream(is,null,opt);  
    }
     /**获取带黑边的图片有效区域
     * @param bitmapOrg
     * 图片
     * @return
     * 上下左右的截取位置坐标,像素点
     */
    public static int[] getValidArea ( Bitmap bitmapOrg )
  {
  int h = bitmapOrg.getHeight();
  int w = bitmapOrg.getWidth();
  int[] pixels = new int[w * h];
  bitmapOrg.getPixels(pixels, 0, bitmapOrg.getWidth(), 0, 0,
  bitmapOrg.getWidth(), bitmapOrg.getHeight());


  int x1 = w;
  int y1 = h;
  int x2 = 0;
  int y2 = 0;
  for (int i = 0; i < h; i += 5)
  {
  for (int j = 0; j < w; j += 5)
  {
  int index = i * w + j;
  int r = (pixels[index] >> 16) & 0xff;
  int g = (pixels[index] >> 8) & 0xff;
  int b = pixels[index] & 0xff;
  if (r > 45 || g > 45 || b > 45)
  {
  if (j < x1)
  {
  x1 = j;
  }
  if (j > x2)
  {
  x2 = j;
  }


  if (i < y1)
  {
  y1 = i;
  }
  if (i > y2)
  {
  y2 = i;
  }
  }
  }
  }
  x2 += 1;
  y2 += 1;
  Log.d(TAG, "x1 = " + x1 + " y1 = " + y1 + " x2 = " + x2 + " y2 = "
  + y2);


  return new int[]
  { x1, y1, x2, y2 };
  }
    /**
     * 等比例缩放图片
     * 
     * @param orgBitmap
     * @param ratio
     *            缩放比
     * @return
     */
    public static Bitmap zoomBitmap(Bitmap orgBitmap, float ratio) {
    if (ratio==1.0) {
return orgBitmap;
}
        // 获取这个图片的宽和高
        int width = orgBitmap.getWidth();
        int height = orgBitmap.getHeight();


        // 创建操作图片用的matrix对象
        Matrix matrix = new Matrix();
        // 缩放图片动作
        matrix.postScale(ratio, ratio);
        Log.d(TAG , "width="+width+",height="+height + " ratio="+ratio);
        Bitmap bitmap = Bitmap.createBitmap(orgBitmap, 0, 0, width, height, matrix, true);
        Log.d(TAG , "bitmap.width="+bitmap.getWidth()+",bitmap.height="+bitmap.getWidth());
        if(orgBitmap != null  && !orgBitmap.isRecycled()){//先判断图片是否已释放了    
            orgBitmap.recycle();    
         }
        // 创建新的图片
        return bitmap;
    }
    /**
     * 按指定角度旋转图片
     * 
     * @param orgBitmap
     * @param angle
     *            旋转角度
     * @return
     */
    public static Bitmap rotateBitmap(Bitmap orgBitmap, double angle) {
        // 获取这个图片的宽和高
        int width = orgBitmap.getWidth();
        System.out.println("bitmap width:" + width);
        int height = orgBitmap.getHeight();
        System.out.println("bitmap height:" + height);


        Matrix matrix = new Matrix();
        // 旋转图片 动作
        matrix.setRotate((float) angle);


        // 创建新的图片
        Bitmap bitmap = Bitmap
        .createBitmap(orgBitmap, 0, 0, width, height, matrix, true);
        //释放资源,防止内存溢出
        if(orgBitmap != null && !orgBitmap.isRecycled()){//先判断图片是否已释放了    
            orgBitmap.recycle();    
         }
        return bitmap;
    }
    /**
     * 按指定大小缩放图片
     * 
     * @param orgBitmap
     * @param ratio
     *            缩放比
     * @return
     */
    public static Bitmap zoomPhotoBitmap(Bitmap orgBitmap, int width,int height) {


    // 获取这个图片的宽和高
        int height_bitmap = orgBitmap.getHeight();




//         计算缩放率,新尺寸除原始尺寸
//         float ratioWidth = ((float) width) / width;
         float ratioHeight = ((float)height)/ height_bitmap;
         float ratioWidth = ratioHeight;
         int Width = (int) (ratioWidth*orgBitmap.getWidth());
        // 创建操作图片用的matrix对象
        Matrix matrix = new Matrix();
        // 缩放图片动作
        matrix.postScale(ratioWidth, ratioHeight);
        Bitmap bitmap = Bitmap.createBitmap(orgBitmap, 0, 0, Width, height, matrix, true);
      //释放资源,防止内存溢出
        if(orgBitmap != null  && !orgBitmap.isRecycled()){//先判断图片是否已释放了    
            orgBitmap.recycle();    
         }
        // 创建新的图片
        return bitmap;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无物勿吾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值