android中Bitmap图像处理 修改图片大小以及保存时的文件大小

来源:互联网 发布:应用市场软件 编辑:程序博客网 时间:2024/05/17 07:48
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Options options1 = new Options();  
  2. options1.inJustDecodeBounds = true;    
  3. BitmapFactory.decodeFile(filePath, options1);    
  4. options1.inSampleSize = RegisterTool.calculateInSampleSize(options1, 110160);  //110,160:转换后的宽和高,具体值会有些出入  
  5. options1.inJustDecodeBounds = false;    
  6. Bitmap bitmap = BitmapFactory.decodeFile(filePath, options1);       //filePath:文件路径  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public static int calculateInSampleSize(BitmapFactory.Options options,  
  2.             int reqWidth, int reqHeight) {  
  3.           
  4.         final int height = options.outHeight;  
  5.         final int width = options.outWidth;  
  6.         int inSampleSize = 1;  
  7.   
  8.         if (height > reqHeight || width > reqWidth) {  
  9.   
  10.             final int heightRatio = Math.round((float) height  
  11.                     / (float) reqHeight);  
  12.             final int widthRatio = Math.round((float) width / (float) reqWidth);  
  13.   
  14.             inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;  
  15.         }  
  16.   
  17.         return inSampleSize;  
  18.     }  
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //压缩图片并将Bitmap保存到本地  
  2. FileOutputStream out = new FileOutputStream(new File(filePath));  
  3. saveBitmap.compress(Bitmap.CompressFormat.JPEG, 60, out);   //60代表压缩40%  
0 0
原创粉丝点击