How to dim the size of a Blob image?

1

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.

    
asked by anonymous 02.03.2016 / 15:12

1 answer

4

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:

  • "Large" images are always stored in a file on the SD card.
  • When I just need the "big" image, I save the path to your file.
  • When I need to access frequently (px ListView) a reduced image, I save it in the DB.
  • When I need the "big" image and the reduced image, I save the path to the "big" image and the reduced image in the BD.
  • The "big" image is always obtained / read asynchronously.
02.03.2016 / 16:02