Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize Bitmap in Android

I resize a bitmap using the following code:

FileOutputStream out = new FileOutputStream("/sdcard/mods.png");
Bitmap bmp = Bitmap.createScaledBitmap(pict, (int)(pict.getWidth() / totScale),  
    (int)(pict.getHeight() / totScale), false);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();

The code for getting the bitmap from the camera that I am using is the following:

mCamera.takePicture(null, null, null, new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            pict = BitmapFactory.decodeByteArray(data, 0, data.length);
        }
});

The first picture is what I can see on the phone (in Astro file manager), and also when I draw the bitmap in my application on a canvas. This happens on every device I've tested on (HTC Legend and Galaxy Tab) The second picture is what it looks like on my computer. What is causing the blocks on the device?


Solution:

Here is what fixed my problem: Instead of

pict = BitmapFactory.decodeByteArray(data, 0, data.length);

I replaced that with

BitmapFactory.Options opts = new BitmapFactory.Options();               
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
pict = BitmapFactory.decodeByteArray(data, 0, data.length, opts);

PhoneComputer

like image 728
Amandeep Grewal Avatar asked May 13 '11 16:05

Amandeep Grewal


People also ask

How do I resize a photo on Android?

Tap the image you want to adjust. You can adjust the size of an image or rotate it: Resize: Touch and drag the squares along the edges. Rotate: Touch and drag the circle attached to the image.


2 Answers

1) Try changing the filter parameter from "false" to "true" in your call to createScaledBitmap(). I bet that will fix your problem.

You should read this article: http://www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/

like image 53
George Freeman Avatar answered Nov 14 '22 22:11

George Freeman


First and second pictures are the same.

P.S.

bmp.compress(Bitmap.CompressFormat.PNG, 90, out);

You cant compress PNG format, only JPEG.

like image 36
Divers Avatar answered Nov 14 '22 22:11

Divers