[英]How to compress an image?
我正在上載圖像的android studio項目。 有些圖片尺寸很大,我想先壓縮一下再上傳到應用程序中。 有人可以建議我做點什么嗎?
您可以使用此代碼壓縮圖像
Bitmap imageBitmap;
int newHeight = 20 // define your height here
int newWidth = 30 //define new height
Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap,newWidth,
newHeight,
true);
https://developer.android.com/reference/android/graphics/Bitmap.html有關詳細信息, 請參見文檔。
使用此庫壓縮任何文件: https : //github.com/zetbaitsu/Compressor
試試這個功能
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize, boolean filter) {
float ratio = Math.min(
maxImageSize / realImage.getWidth(),
maxImageSize / realImage.getHeight());
int width = Math.round(ratio * realImage.getWidth());
int height = Math.round(ratio * realImage.getHeight());
Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
height, filter);
return newBitmap;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.