6

Does anyone know how to scale the bitmap image without losing the image quality ? Currently I facing this problem, where the size of selected image maybe too big caused the app return to another activity.

So now I tried using this method to scale the selected image without losing its quality. I get the code from here.

 protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver()
                            .query(selectedImage, filePathColumn, null, null,
                                    null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    Bitmap a = (BitmapFactory.decodeFile(picturePath));
                    photo = scaleBitmap(a, 200, 200);
                    imageView.setImageBitmap(photo);
                }
                break;
}

  public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
        Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

        float scaleX = newWidth / (float) bitmap.getWidth();
        float scaleY = newHeight / (float) bitmap.getHeight();
        float pivotX = 0;
        float pivotY = 0;

        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG));

        return scaledBitmap;
    }

The answer seems helpful for most of the people, but why it doesn't work for me ? Have I missed anything?

original image

selected image display on imageView

Community
  • 1
  • 1
  • if you wan to scale it to the size 200 x 200, most likely you will get distorted image, since original image is not square one – pskink Dec 16 '15 at 17:47
  • @pskink where can I know the original size ? –  Dec 16 '15 at 17:47
  • 1
    by reading `Bitmap` docs? – pskink Dec 16 '15 at 17:48
  • @pskink what value should I put instead of 200,200 ..still stuck ! –  Dec 17 '15 at 08:32
  • did you read `Bitmap` docs? do you now know what is the size of your `Bitmap`? if so, just make it smaller – pskink Dec 17 '15 at 08:34
  • @pskink but the image does not fixed. –  Dec 17 '15 at 08:36
  • i have no green idea what you mean – pskink Dec 17 '15 at 08:37
  • @pskink if make it smaller, the scaled image will not losing its quality ? –  Dec 17 '15 at 08:39
  • of course it will loose its quality, if you want to preserve original quality, dont scale it – pskink Dec 17 '15 at 08:40
  • @pskink if I don't scale it, I will get this [problem](http://stackoverflow.com/questions/34315387/will-the-image-too-big-caused-app-return-to-previous-activity) –  Dec 17 '15 at 08:43
  • I've been looking the answer for two days and yet still can't find the solution –  Dec 17 '15 at 08:49
  • then scale down your image if you have OutOfMemory problems – pskink Dec 17 '15 at 08:50
  • @pskink How do I make the image scaled efficiency? –  Dec 17 '15 at 08:51
  • 1
    search the web, 20 seconds search gives: [Loading Large Bitmaps Efficiently](http://developer.android.com/training/displaying-bitmaps/load-bitmap.html) what were you doing for two days? – pskink Dec 17 '15 at 08:52
  • I'm sure you have read [this](http://stackoverflow.com/questions/34315387/will-the-image-too-big-caused-app-return-to-previous-activity) before comment right ? –  Dec 17 '15 at 08:56
  • @pskink seems like I only can have one of these two methods :'( –  Dec 17 '15 at 13:12
  • @pskink I want to resize the selected image without loosing its quality. If I use `scaleBitmap` (stated as my post ), image looked blur. If I use [this](http://stackoverflow.com/questions/34315387/will-the-image-too-big-caused-app-return-to-previous-activity) method, the size of image too large... –  Dec 17 '15 at 13:24
  • While I'm fairly certain there are many ways to implement this automatically based on your image size and canvas size, you can of course just play around with the settings manually. instead of the code line: photo = scaleBitmap(a, 200, 200) try photo = scaleBitmap(a, 100, 200) for instance. Fiddle around with the numbers until it looks like you want it to. – Uvar Dec 21 '15 at 15:11
  • @Uvar but the image still look blur –  Dec 21 '15 at 16:51
  • I'm afraid it's never possible to preserve full quality, since your visual information just does not fit onto the small canvas. It should be doable to render it slightly blurred, yet readable though – Uvar Dec 21 '15 at 17:22
  • @uvar is this the only method to scale bitmap image without loosing its quality? Thanks –  Dec 21 '15 at 17:53

5 Answers5

1

Please try this

public Bitmap scaleBitmap(Bitmap bitmap,int newWidth,int newHeight) {    
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;

    }
Mobile Apps Expert
  • 1,028
  • 1
  • 8
  • 11
1

Try this for scaling bitmap

public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    float scaleX = ((float)newWidth) /  bitmap.getWidth();
    float scaleY = ((float)newHeight) / bitmap.getHeight(); 
    Matrix scaleMatrix = new Matrix();
    scaleMatrix.postScale(scaleX, scaleY);
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight() , scaleMatrix, true);
    return scaledBitmap;
}
SachinS
  • 2,223
  • 1
  • 15
  • 25
1
Use this it will definitely help you to reduce the size without loosing quality.
public Bitmap compressImage(String imageUri) {

        String filePath = imageUri;
        // String filePath = getRealPathFromURI(imageUri);
        Bitmap scaledBitmap = null;

        BitmapFactory.Options options = new BitmapFactory.Options();

        // by setting this field as true, the actual bitmap pixels are not
        // loaded in the memory. Just the bounds are loaded. If
        // you try the use the bitmap here, you will get null.
        options.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(filePath, options);

        int actualHeight = options.outHeight;
        int actualWidth = options.outWidth;

        // max Height and width values of the compressed image is taken as
        // 816x612

        /*
         * float maxHeight = 816.0f; float maxWidth = 612.0f;
         */
        float maxHeight = 1080.0f;
        float maxWidth = 800.0f;

        float imgRatio = actualWidth / (float) actualHeight;
        float maxRatio = maxWidth / (float) maxHeight;

        // width and height values are set maintaining the aspect ratio of the
        // image

        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = maxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) maxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;

            }
        }

        // setting inSampleSize value allows to load a scaled down version of
        // the original image

        options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);

        // inJustDecodeBounds set to false to load the actual bitmap
        options.inJustDecodeBounds = false;

        // this options allow android to claim the bitmap memory if it runs low
        // on memory
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inTempStorage = new byte[16 * 1024];

        try {
            // load the bitmap from its path
            bmp = BitmapFactory.decodeFile(filePath, options);
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();

        }
        try {
            scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();
        }

        float ratioX = actualWidth / (float) options.outWidth;
        float ratioY = actualHeight / (float) options.outHeight;
        float middleX = actualWidth / 2.0f;
        float middleY = actualHeight / 2.0f;

        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

        // check the rotation of the image and display it properly
        ExifInterface exif;
        try {
            exif = new ExifInterface(filePath);

            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
            displayLogs("EXIF", "Exif: " + orientation);
            Matrix matrix = new Matrix();
            if (orientation == 6) {
                matrix.postRotate(90);
                displayLogs("EXIF", "Exif: " + orientation);
            } else if (orientation == 3) {
                matrix.postRotate(180);
                displayLogs("EXIF", "Exif: " + orientation);
            } else if (orientation == 8) {
                matrix.postRotate(270);
                displayLogs("EXIF", "Exif: " + orientation);
            }

            scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileOutputStream out = null;
        String filename = getFilename(filePath);
        // File image = new File(getFilename(), System.currentTimeMillis() +
        // ".jpg");
        try {
            out = new FileOutputStream(filename);

            // write the compressed bitmap at the destination specified by
            // filename.
            boolean didWrite = scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);

            out.close();

            System.out.println(didWrite);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        return scaledBitmap;

    }

    private String getRealPathFromURI(String contentURI) {
        Uri contentUri = Uri.parse(contentURI);
        Cursor cursor = getContentResolver().query(contentUri, null, null, null, null);
        if (cursor == null) {
            return contentUri.getPath();
        } else {
            cursor.moveToFirst();
            int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            return cursor.getString(index);
        }
    }

    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        final float totalPixels = width * height;
        final float totalReqPixelsCap = reqWidth * reqHeight * 2;
        while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
            inSampleSize++;
        }

        return inSampleSize;
    }

    public String getFilename(String originalname) {

        ImageCache imageCache = new ImageCache(this);
        File file = new File("Your folder path");

        // return file;
        String uriSting = (file.getAbsolutePath() + "/" + "F_" + System.currentTimeMillis() + ".png");
        return uriSting;

    }
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
0

Your problem is that you are not keeping the aspect ratio of the original image. in your code if you need the width to be 200, then the height should be calculated like this:

newHeight = ((float) originalHeight/(float) originalWidth) * (float) newWidth;

So in your case:

float newHeight = ((float) a.getHeight()/(float) a.getWidth()) * (float) 200;

And then call your scaling function with width:200 and height calculated from original value.

After that if the quality is still low, then experiment with increasing the width and stop when you get the needed quality.

Sari Alalem
  • 870
  • 7
  • 18
-1

While you have too much large image at that time you must Required to manage Bitmap cache policy.Bitmap cache management is too much Complex.So in Android we have one superb library Picasso.this library is provide inbuilt cache management. this will be manage your bitmap in background and it's also provide image cache feature.once your image is load and again you load this same Image this will be get in cache.it's inbuilt functionality no need to change in code.

I hope you are clear with my solution. Best of Luck

Chirag Solanki
  • 1,107
  • 2
  • 13
  • 23