Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap compress PNG -> JPEG and vice versa in Android

I have problem with size of picture when I'm doing conversion from PNG to JPEG, and then JPEG to PNG.

            public void onClick(View v) {
            String imageFileName = "/sdcard/Penguins2.png";
            File imageFile = new File(imageFileName);
            if (imageFile.exists()) {
                // Load the image from file
                myBitmap = BitmapFactory.decodeFile(imageFileName);
                // Display the image in the image viewer
                myImageView = (ImageView) findViewById(R.id.my_image_view);
                if (myImageView != null) {
                    myImageView.setImageBitmap(myBitmap);
                }
            }
        }

Conversion:

    private void processImage() {               
    try {
        String outputPath = "/sdcard/Penguins2.jpg";
        int quality = 100;
        FileOutputStream fileOutStr = new FileOutputStream(outputPath);
        BufferedOutputStream bufOutStr = new BufferedOutputStream(
                fileOutStr);
        myBitmap.compress(CompressFormat.JPEG, quality, bufOutStr);
        bufOutStr.flush();
        bufOutStr.close();
    } catch (FileNotFoundException exception) {
        Log.e("debug_log", exception.toString());
    } catch (IOException exception) {
        Log.e("debug_log", exception.toString());
    }
    myImageView.setImageBitmap(myBitmap);

After processing this operation I just change these lines:

String imageFileName = "/sdcard/Penguins2.png";

to

String imageFileName = "/sdcard/Penguins2.jpg";

and

String outputPath = "/sdcard/Penguins2.jpg";
(...)
myBitmap.compress(CompressFormat.JPEG, quality, bufOutStr);    

to

String outputPath = "/sdcard/Penguins2.png";
(...)
myBitmap.compress(CompressFormat.PNG, quality, bufOutStr);    

Size of image changed from 585847 to 531409 (in DDMS)

I want to do such thing because I'd like to use PNG which is lossless for some image processing. Then converse image to jpeg and send as MMS, I'm not sure but I think JPEG is only format which is support by all devices in MMS. Receiver would open image and converse it back to png without losing data.

like image 806
Marcin_cyna Avatar asked Dec 26 '22 10:12

Marcin_cyna


2 Answers

In addition to the @Sherif elKhatib answer, if you check the documentation: http://developer.android.com/reference/android/graphics/Bitmap.html#compress%28android.graphics.Bitmap.CompressFormat,%20int,%20java.io.OutputStream%29

You can see that the PNG images don't use the quality paremeter:

quality: Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting

like image 180
Milos Cuculovic Avatar answered Jan 08 '23 05:01

Milos Cuculovic


This is not doable! Once you convert to JPG you lost the "lossless state of PNG".

Anyway png is supported by everyone.

+In your case, you want the receiver to change it back to PNG to retrieve the lossless image. This means that the receiver also supports PNG. What is the point of changing it to JPG before sending and then changing it back to PNG when received. Just some extra computations?

like image 38
Sherif elKhatib Avatar answered Jan 08 '23 04:01

Sherif elKhatib