• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How to compress an image without loosing its quality

nagamothu

Newbie
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:
]   //open camera
[*]    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
[*]       Bundle extras = data.getExtras();
[*]        Bitmap imageBitmap = (Bitmap) extras.get( "data" );
[*]        //compress image
[*]        Bitmap convertedimage = getResizedBitmap( imageBitmap, 100 );
[*]

[*]        Intent intent = new Intent( MainActivity.this, Display.class );
[*]        extras.putParcelable( "Bitmap", convertedimage );
[*]        intent.putExtras( extras );
[*]        startActivity( intent );
[*]    }
[*]}
[*]

[*]private Bitmap getResizedBitmap(Bitmap image, int maxSize) {
[*]    int width = image.getWidth();
[*]    int height = image.getHeight();
[*]    float bitmapRatio = (float) width / (float) height;
[*]    if (bitmapRatio > 1) {
[*]        width = maxSize;
[*]        height = (int) (width / bitmapRatio);
[*]    } else {
[*]        height = maxSize;
[*]        width = (int) (height * bitmapRatio);
[*]    }
[*]    return Bitmap.createScaledBitmap( image, width, height, true );
[*]

[*]}
[*]
IN Display Activity.java
Code:
[*]Bundle extras = getIntent().getExtras();
[*]if (extras != null) {
[*]    Bitmap bmp = extras.getParcelable( "Bitmap" );
[*]    image.setImageBitmap( bmp );
[*]    //display compressed image size in textview
[*]    long height = bmp.getHeight();
[*]    long width = bmp.getWidth();
[*]    long filesizeinBytes1 = bmp.getByteCount();
[*]    long fileSizeInKB1 = filesizeinBytes1 / 1024;
[*]    textview1.setText( " Height:" + height + "Width" + width + "size in KB:  " + fileSizeInKB1 );
[*]    Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
[*]

[*]    File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
[*]    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
[*]    FileOutputStream fo;
[*]    try {
[*]        fo = new FileOutputStream( destination );
[*]        fo.write( bytes.toByteArray() );
[*]        fo.close();
[*]    } catch (IOException e) {
[*]        e.printStackTrace();
[*]    }
[*]
 
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?
 
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?


Hi i have changed my code but with this code 60KB image is becoming 121KB . please see why is that
Code:
Bundle extras = getIntent().getExtras();
if (extras != null) {
    Bitmap bmp = (Bitmap) extras.getParcelable( "Bitmap" );
 
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress( Bitmap.CompressFormat.JPEG, 40, stream );
    byte[] BYTE = stream.toByteArray();
    Bitmap convertedimage = BitmapFactory.decodeByteArray( BYTE,0,BYTE.length );
    image.setImageBitmap( convertedimage );

    //display compressed image size in textview
    long height = convertedimage.getHeight();
    long width = convertedimage.getWidth();
    long filesizeinBytes1 = convertedimage.getByteCount();
    long fileSizeInKB1 = filesizeinBytes1 / 1024;
    textview1.setText( " Height:" + height + "Width" + width + "size in KB:  " + fileSizeInKB1 );
    Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
    System.out.print( fileSizeInKB1 );

    File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    FileOutputStream fo;
    try {
        fo = new FileOutputStream( destination );
        fo.write( bytes.toByteArray() );
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
 
Back
Top Bottom