Android BitmapFactory 图片压缩

public static Bitmap getCuttedBitmap(String path, Bitmap bitmap) {

      //根据图片路径选择图片的旋转角度

     int degree = ImageUtil.getCameraImgRotateDegree(path);

    //原图片的宽度和高度
     int sWidth = bitmap.getWidth();
     int sHeight = bitmap.getHeight();
    
     int nWidth = 0;
     int nHeight = 0;
    
     int x = 0;
     int y = 0;
     //如果原图片的宽度大于高度,做等比例配置。
     if (sWidth > sHeight) {
       nWidth = sHeight;
       nHeight = sHeight;

      //确定图片水平其实位置
       x = (sWidth - sHeight) / 2;
       y = 0;
     } else {
       nWidth = sWidth;
       nHeight = sWidth;
       x = 0;
       y = (sHeight - sWidth) / 2;
     }

    //创建矩阵,并设置旋转角度

     Matrix matrix = new Matrix();
     matrix.postRotate(degree);
     //创建新的bitmap
     Bitmap newBitmap = Bitmap.createBitmap(bitmap, x, y, nWidth, nHeight, matrix, true);

 //对于宽度小于56像素的图片直接返回    

 if (sWidth <= 56 || sHeight <= 56) {
       return newBitmap;
     }

     try {

   //现将图片保存到手机
       newBitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/ufriend/head_icon_temp.png")));
     }
     catch (FileNotFoundException e) {
       e.printStackTrace();
      
       return null;
     }
    
     int destWidth = 56;
     int destHeight = 56;
     //得到压缩比率
     int ratio = nWidth / destWidth + 1;
    
     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inSampleSize = ratio;
     options.inJustDecodeBounds = false;
     options.outWidth = destWidth;
     options.outHeight = destHeight;
     //将图片压缩后输出
     Bitmap destBitmap = BitmapFactory.decodeFile("/sdcard/ufriend/head_icon_temp.png", options);
    
     try {
       destBitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(new File(Constants.AVATAR_LOCAL_PATH)));
       return destBitmap;
     }
     catch (FileNotFoundException e) {
       e.printStackTrace();
      
       return null;
     } finally {
       newBitmap.recycle();
       newBitmap = null;
     }
   }

对于压缩后图片清晰度变低,是因为压缩的时候没有对图片大小进行判断,结果反复压缩,导致图片清晰度变低


在调用Bitmap.createBitmap(bitmap,x,y,width,height,filter)方法时,本身的x,y,width,height不能大于bitmap的宽高,否则会报异常。

如果options的inJustDecodeBounds 设置为false ,那么它不会把图像本身的流读出来,但是可以获得图像的宽高等信息。


ExifInterface exifInterface = new ExifInterface(file.getAbsolutePath());
 if(exifInterface!=null){
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
   String latitude = exifInterface.getAttribute(ExifInterface.TAG_GPS_ALTITUDE);
Log.i("flash", latitude);
   switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
Log.i("orientation", "90");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Log.i("orientation", "180");
break;
case ExifInterface.ORIENTATION_ROTATE_270:
Log.i("orientation", "270");
break;
default:
break;

ExifInterface接口可以获得图片的旋转角度,和gps定位的一些信息,但是并不是所有的图片都包含gps信息,这和图片的格式内容有关系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值