2013-08-01 154 views
0

我想將從相機拍攝的照片發送到服務器。但它的大小在1M左右,我相信它是兩倍多的。因此我想減小照片的大小。下面我正在使用的方法。
saveFile - 只需將位圖保存到文件並返回到它的路徑。
calculateInSampleSize返回inSampleSize將照片縮放調整爲所選。
decodeSampledBitmapFromResource從文件解碼birmap並用新參數重建它。
Evrything不錯,但似乎不工作。 small3.png的尺寸是1240 * 768 * 24,大小仍然是〜1M。如何減少圖片的大小?

我把它與

photo = saveFile(decodeSampledBitmapFromResource(picturePath, 
        800, 800),3); 

方法

public String saveFile(Bitmap bm,int id) { 
     String file_path = Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/Mk"; 
     File dir = new File(file_path); 
     if (!dir.exists()) 
      dir.mkdirs(); 
     File file = new File(dir, "smaller" + id + ".png"); 
     FileOutputStream fOut; 
     try { 
      fOut = new FileOutputStream(file); 
      bm.compress(Bitmap.CompressFormat.PNG, 85, fOut); 
      fOut.flush(); 
      fOut.close(); 


      } 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     return file.getPath(); 

    } 

    public static int calculateInSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 

      // Calculate ratios of height and width to requested height and 
      // width 
      final int heightRatio = Math.round((float) height 
        /(float) reqHeight); 
      final int widthRatio = Math.round((float) width/(float) reqWidth); 

      // Choose the smallest ratio as inSampleSize value, this will 
      // guarantee 
      // a final image with both dimensions larger than or equal to the 
      // requested height and width. 
      inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
     } 

     return inSampleSize; 
    } 
public static Bitmap decodeSampledBitmapFromResource(String path, 
      int reqWidth, int reqHeight) { 
     Log.d("path", path); 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeFile(path, options); 
    } 

回答

1

爲什麼你正在寫的圖像兩次做它在所有一次性

我該怎麼辦

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inSampleSize = 4; 

Bitmap myBitmap = BitmapFactory.decodeFile({ImagePath}, options); 
FileOutputStream fileOutputStream = new FileOutputStream({ImagePath}); 
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
myBitmapClose.compress(CompressFormat.JPEG, imageQuality, bos); 
if (bos != null) { 
    bos.flush(); 
    bos.close(); 
} 
if (fileOutputStream != null) { 
    fileOutputStream.flush(); 
    fileOutputStream.close(); 
} 

它會d efinitely壓縮心靈行

myBitmapClose.compress(CompressFormat.JPEG, imageQuality, bos); 

其中一套imageCompression型CompressFormat.JPEG

+0

只是想保留原始照片:) – Yarh

+0

好的,你可以處理insample的大小和質量 – 2013-08-01 08:16:22

0

那是我的代碼...我想,以減少圖像的大小發生路徑字符串。

String pics=(arraylist.get(position).getImage()); 
try{ 
    BitmapFactory.Options op = new BitmapFactory.Options(); 
    op.inJustDecodeBounds = true; 
    //op.inSampleSize = 20; op.outWidth = 25; 
    op.outHeight = 35; 
    bmp = BitmapFactory.decodeFile(pics); 
    image.setImageBitmap(bmp); 
}catch(Exception e) { 
}