I have a blob image in the bank, it is in high resolution, I needed to lower its resolution. Does anyone know how to do this? Thank you in advance.
I have a blob image in the bank, it is in high resolution, I needed to lower its resolution. Does anyone know how to do this? Thank you in advance.
You can decrease the dimensions of an image by using the BitmapFactory.Options last seen at BitmapFactory.decodeByteArray () .
Type a helper method to calculate the value of BitmapFactory. Options.inSampleSize :
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Altura e largura da imagem original
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;
//Calcula o maior valor "inSampleSize" que é uma potência de 2 e mantém tanto
// a altura e largura maior do que a altura e largura solicitada
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
Type a method that returns a Bitmap with the desired size:
public static Bitmap decodeSampledBitmapFromByteArray(byte[] byteArray,
int reqWidth, int reqHeight) {
//Primeiro descodifica com inJustDecodeBounds = true para obter as dimensões originais
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(byteArray, 0, byteArray.lenght, options);
// Calcula o valor inSampleSize a usar para obter as dimensões pretendidas.
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// descodifica a imagem com as dimensões pretendidas.
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(byteArray, 0, byteArray.lenght, options);
}
Use this:
Bitmap imagem = decodeSampledBitmapFromByteArray(byteArray, reqWidth, reqHeight);
Adapted from documentation >.
Principles I follow when using BD and images: