我发现如果想要压缩图片,每个人都会先读取Bitmap。但是我不想阅读,只需编辑图片文件,然后图片文件可以更小。
java有类,但它不能用于android。有没有源代码可以做到这一点?
答案 0 :(得分:0)
您可以使用Picasso Library进行图像处理。就像这样:
Picasso.with(getActivity())
.load(R.drawable.add_icon)
.resize(130, 130).placeholder(R.drawable.no_images).into(imgPratientImage;)
答案 1 :(得分:0)
试试这段代码。
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) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
使用以下代码从字节数组中获取图像
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, WIDTH, HEIGHT);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length, options);
int dimension = getSquareCropDimensionForBitmap(bitmap);
bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);
其中imageByteArray
实际上是您可以从服务器获取的字节数组。你可以使用其他方法,我只写这个例子。变量WIDTH
和HEIGHT
是图片的所需宽度和高度。