[英]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.