1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to compress an image without loosing its quality

Discussion in 'Android Development' started by nagamothu, Apr 26, 2019.

  1. nagamothu

    nagamothu Newbie
    Thread Starter

    Hi guys am trying to compress an image without loosing its quality. now with my code image is compressing from 60KB to 3KB . but the quality is really bad how to fix this. this is my code please see and help me.
    i want to capture an image compress it , send it to next activity and display it in image view.
    MainActivity.java
    Code
    Code (Text):
    1.  
    2. ]   //open camera
    3. [*]    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
    4. [*]       Bundle extras = data.getExtras();
    5. [*]        Bitmap imageBitmap = (Bitmap) extras.get( "data" );
    6. [*]        //compress image
    7. [*]        Bitmap convertedimage = getResizedBitmap( imageBitmap, 100 );
    8. [*]
    9.  
    10. [*]        Intent intent = new Intent( MainActivity.this, Display.class );
    11. [*]        extras.putParcelable( "Bitmap", convertedimage );
    12. [*]        intent.putExtras( extras );
    13. [*]        startActivity( intent );
    14. [*]    }
    15. [*]}
    16. [*]
    17.  
    18. [*]private Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    19. [*]    int width = image.getWidth();
    20. [*]    int height = image.getHeight();
    21. [*]    float bitmapRatio = (float) width / (float) height;
    22. [*]    if (bitmapRatio > 1) {
    23. [*]        width = maxSize;
    24. [*]        height = (int) (width / bitmapRatio);
    25. [*]    } else {
    26. [*]        height = maxSize;
    27. [*]        width = (int) (height * bitmapRatio);
    28. [*]    }
    29. [*]    return Bitmap.createScaledBitmap( image, width, height, true );
    30. [*]
    31.  
    32. [*]}
    33. [*]
    34.  
    IN Display Activity.java
    Code (Text):
    1.  
    2.  
    3. [*]Bundle extras = getIntent().getExtras();
    4. [*]if (extras != null) {
    5. [*]    Bitmap bmp = extras.getParcelable( "Bitmap" );
    6. [*]    image.setImageBitmap( bmp );
    7. [*]    //display compressed image size in textview
    8. [*]    long height = bmp.getHeight();
    9. [*]    long width = bmp.getWidth();
    10. [*]    long filesizeinBytes1 = bmp.getByteCount();
    11. [*]    long fileSizeInKB1 = filesizeinBytes1 / 1024;
    12. [*]    textview1.setText( " Height:" + height + "Width" + width + "size in KB:  " + fileSizeInKB1 );
    13. [*]    Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
    14. [*]
    15.  
    16. [*]    File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
    17. [*]    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    18. [*]    FileOutputStream fo;
    19. [*]    try {
    20. [*]        fo = new FileOutputStream( destination );
    21. [*]        fo.write( bytes.toByteArray() );
    22. [*]        fo.close();
    23. [*]    } catch (IOException e) {
    24. [*]        e.printStackTrace();
    25. [*]    }
    26. [*]
    27.  
     


  2. Hadron

    Hadron Smoke me a kipper...
    VIP Member

    Without some examples it's really hard to say whether there is a problem or this is just an inevitable result of what you are doing.

    There's a lot of stuff I can't tell from this code, such as what the original size of the image you are scaling to a maximum width/height of 100 units (pixels?) is. But it seems pretty clear that you are not compressing an image but rescaling it (sorry to be pedantic, but they mean different things), which will of course lose information. If that is actually 100 pixels you are scaling to then that's about the size of one of the icons in my phone's dock rather than a thumbnail in my gallery: that might work for a fairly simple image with a few large, clear features, but would be very small for anything of any complexity.

    Anyway, it seems that the scaling is being done by the function "createScaledBitmap", so if the size is what you intend and you think there's something wrong with the quality then the question is what is that function doing?
     
  3. nagamothu

    nagamothu Newbie
    Thread Starter


    Hi i have changed my code but with this code 60KB image is becoming 121KB . please see why is that
    Code (Text):
    1.  
    2.  
    3. Bundle extras = getIntent().getExtras();
    4. if (extras != null) {
    5.     Bitmap bmp = (Bitmap) extras.getParcelable( "Bitmap" );
    6.  
    7.     ByteArrayOutputStream stream = new ByteArrayOutputStream();
    8.     bmp.compress( Bitmap.CompressFormat.JPEG, 40, stream );
    9.     byte[] BYTE = stream.toByteArray();
    10.     Bitmap convertedimage = BitmapFactory.decodeByteArray( BYTE,0,BYTE.length );
    11.     image.setImageBitmap( convertedimage );
    12.  
    13.     //display compressed image size in textview
    14.     long height = convertedimage.getHeight();
    15.     long width = convertedimage.getWidth();
    16.     long filesizeinBytes1 = convertedimage.getByteCount();
    17.     long fileSizeInKB1 = filesizeinBytes1 / 1024;
    18.     textview1.setText( " Height:" + height + "Width" + width + "size in KB:  " + fileSizeInKB1 );
    19.     Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
    20.     System.out.print( fileSizeInKB1 );
    21.  
    22.     File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
    23.     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    24.     FileOutputStream fo;
    25.     try {
    26.         fo = new FileOutputStream( destination );
    27.         fo.write( bytes.toByteArray() );
    28.         fo.close();
    29.     } catch (IOException e) {
    30.         e.printStackTrace();
    31.     }
    32.  
     
Loading...

Share This Page

Loading...