2017-04-27 181 views
1

我使用此功能在上傳之前,以減少圖像的大小,但通過以下方法我的文件大小增加如何減少圖像文件大小

下面的代碼我的文件大小---使用前> 157684

使用此代碼我的文件大小後-----> 177435

有一個人幫助我,我請如何上傳前減少文件大小到服務器

代碼:

public File saveBitmapToFile(File file){ 
    try { 

     // BitmapFactory options to downsize the image 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     o.inSampleSize = 6; 
     // factor of downsizing the image 

     FileInputStream inputStream = new FileInputStream(file); 
     //Bitmap selectedBitmap = null; 
     BitmapFactory.decodeStream(inputStream, null, o); 
     inputStream.close(); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE=75; 

     // Find the correct scale value. It should be the power of 2. 
     int scale = 1; 
     while(o.outWidth/scale/2 >= REQUIRED_SIZE && 
         o.outHeight/scale/2 >= REQUIRED_SIZE) { 
      scale *= 2; 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     inputStream = new FileInputStream(file); 

     Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2); 
     inputStream.close(); 

     // here i override the original image file 
     file.createNewFile(); 
     FileOutputStream outputStream = new FileOutputStream(file); 

     selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream); 

     return file; 
    } catch (Exception e) { 
     return null; 
    } 
} 
+0

Bitmap.CompressFormat.JPEG,100,outputStream爲Bitmap.CompressFormat.JPEG,REQUIRED_SIZE,outputStream –

+0

100質量好0爲牀品質。所以選擇按質量比壓縮,以減少您的文件大小 –

+1

http://stackoverflow.com/questions/8417034/how-to-make-bitmap-compress-without-change-the-bitmap-size –

回答

1

改變這一行:

selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream); 

int mCompressedSize = 50; // 0 is lowest and 100 original 
selectedBitmap.compress(Bitmap.CompressFormat.PNG, mCompressedSize, outputStream); 

希望這會有所幫助。

+1

REQUIRED_SIZE?這100是壓縮質量。 – greenapps

+0

不是它減少大小 – Krish

+0

現在檢查我編輯的答案。 –

0

嘗試

int compressionRatio = 2; //1 == originalImage, 2 = 50% compression, 4=25% compress 
File file = new File (imageUrl); 
try { 
    Bitmap bitmap = BitmapFactory.decodeFile (file.getPath()); 
    bitmap.compress (Bitmap.CompressFormat.JPEG, compressionRatio, new FileOutputStream (file)); 
} 
catch (Throwable t) { 
    Log.e("ERROR", "Error compressing file." + t.toString()); 
    t.printStackTrace(); 
} 
1

這是我用它來降低我的圖片大小不壓縮:

public static Bitmap getResizedBitmap(Bitmap bitmap, int newWidth, int newHeight) { 
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888); 

    float ratioX = newWidth/(float) bitmap.getWidth(); 
    float ratioY = newHeight/(float) bitmap.getHeight(); 
    float middleX = newWidth/2.0f; 
    float middleY = newHeight/2.0f; 

    Matrix scaleMatrix = new Matrix(); 
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY); 

    Canvas canvas = new Canvas(scaledBitmap); 
    canvas.setMatrix(scaleMatrix); 
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth()/2, middleY - bitmap.getHeight()/2, new Paint(Paint.FILTER_BITMAP_FLAG)); 

    return scaledBitmap; 
} 

你只需要輸入正確的新的高度和寬度,以滿足您的需求

1

我們想要製作一張圖片的縮略圖,所以我們需要先將ByteArrayOutputStream接受並傳遞給Bitmap.compress()方法。

ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
youBitmapImage.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

更多的功能,從docs

1

如果你的輸出文件較大:

  • 它可能意味着scale是錯誤的。並將該文件保存100%的質量,因此它可以對輸入文件的增長

  • 壓縮重於泰山即使你它的規模,採用無壓縮的輸出仍然會產生較大的文件