8
votes

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

2
Most sporting of you to take the time to post the exact solution (upvote).George Freeman

2 Answers

4
votes

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/

2
votes

First and second pictures are the same.

P.S.

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

You cant compress PNG format, only JPEG.