martes, 21 de abril de 2015

Android: Downsample image

private static Bitmap getImageFromInternalStorage(Context context, String string, int widthDp,
            int heightdp) {

        if (string == null) {
            return BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.image_placeholder);
        }

        File file = context.getFileStreamPath(string);

        BitmapFactory.Options originalImageOptions = new BitmapFactory.Options();
        originalImageOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), originalImageOptions);

        int requiredWidth = convertToPx(widthDp) * 2;  // 1
        int requiredHeight = convertToPx(heightdp) * 2; // 1
        int sampleSize = Math.max(originalImageOptions.outWidth / requiredWidth, originalImageOptions.outHeight / requiredHeight);

        BitmapFactory.Options outputImageOptions = new BitmapFactory.Options();
        outputImageOptions.inSampleSize = Math.max(sampleSize, 1);
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), outputImageOptions);

        if (bitmap == null) {
            bitmap = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.image_placeholder);
        }

        return bitmap;
    }
1) If I made if so the image would fit exactly being the source image big and the output image small, the result would be horrible (to pixelated) so I made it bigger to not pixelate as much

No hay comentarios:

Publicar un comentario