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

Why Image is becoming blur when i try to display in image view

nagamothu

Newbie
Hi can anyone please clear my doubt. am trying to display an image which is captured from camera in imageview which is in 2nd activity. but image quality is becoming very poor. an anyone tell me why is that and how to resolve it. This is my code please see

Activity 1 (MainActivity)
Code:
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK  )
{
    Bundle extras = data.getExtras();
    Bitmap imageBitmap = (Bitmap) extras.get( "data" );
    long height = imageBitmap.getHeight();              // the height is 204
    long width = imageBitmap.getWidth();                //the width is 153
    long filesizeinBytes1 = imageBitmap.getByteCount();
    long fileSizeInKB1 = filesizeinBytes1 / 1024;      // 60KB
    Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
    System.out.print( fileSizeInKB1 );

    File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    imageBitmap.compress( Bitmap.CompressFormat.JPEG,100,bytes );
    FileOutputStream fo;
    try {
        fo = new FileOutputStream( destination );
        fo.write( bytes.toByteArray());
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Intent intent = new Intent( this, Main2Activity.class );
    startActivity( intent );

And now am retriving image from sd card to imageview .

Activity 2(Main2Activity)
Code:
File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Bitmap bmp= BitmapFactory.decodeFile( destination.getAbsolutePath() );
bmp.compress( Bitmap.CompressFormat.JPEG,50,bytes );
image.setImageBitmap( bmp);

long height = bmp.getHeight();                                  // the height is 204
long width = bmp.getWidth();                                    // the width is 153
long filesizeinBytes1 = bmp.getByteCount();
long fileSizeInKB1 = filesizeinBytes1 / 1024;            // 121KB
textview1.setText( " Height:" + height + "Width" + width + "size in KB:  " + fileSizeInKB1 );
Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
System.out.print( fileSizeInKB1 );

Am compressing the image but the image is becoming from 60KB to 121 KB and when i check in sd card it is 22.4 KB Why is that please explain.And even if it is becoming from 60KB to 121KB it is blurry in imageview Why is that ?
 
Hi can anyone please clear my doubt. am trying to display an image which is captured from camera in imageview which is in 2nd activity. but image quality is becoming very poor. an anyone tell me why is that and how to resolve it. This is my code please see

Activity 1 (MainActivity)
Code:
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK  )
{
    Bundle extras = data.getExtras();
    Bitmap imageBitmap = (Bitmap) extras.get( "data" );
    long height = imageBitmap.getHeight();              // the height is 204
    long width = imageBitmap.getWidth();                //the width is 153
    long filesizeinBytes1 = imageBitmap.getByteCount();
    long fileSizeInKB1 = filesizeinBytes1 / 1024;      // 60KB
    Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
    System.out.print( fileSizeInKB1 );

    File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    imageBitmap.compress( Bitmap.CompressFormat.JPEG,100,bytes );
    FileOutputStream fo;
    try {
        fo = new FileOutputStream( destination );
        fo.write( bytes.toByteArray());
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Intent intent = new Intent( this, Main2Activity.class );
    startActivity( intent );

And now am retriving image from sd card to imageview .

Activity 2(Main2Activity)
Code:
File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Bitmap bmp= BitmapFactory.decodeFile( destination.getAbsolutePath() );
bmp.compress( Bitmap.CompressFormat.JPEG,50,bytes );
image.setImageBitmap( bmp);

long height = bmp.getHeight();                                  // the height is 204
long width = bmp.getWidth();                                    // the width is 153
long filesizeinBytes1 = bmp.getByteCount();
long fileSizeInKB1 = filesizeinBytes1 / 1024;            // 121KB
textview1.setText( " Height:" + height + "Width" + width + "size in KB:  " + fileSizeInKB1 );
Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
System.out.print( fileSizeInKB1 );

Am compressing the image but the image is becoming from 60KB to 121 KB and when i check in sd card it is 22.4 KB Why is that please explain.And even if it is becoming from 60KB to 121KB it is blurry in imageview Why is that ?
 
You have to preserve the aspect ratio ...try
app:layout_constraintDimensionRatio="h,16:9" within the xml for the imageView object. You will have to look up or play around with the h 16:9 the width of the image view object must be set to 0 to use
app:layout_constraintDimensionRatio="h,16:9"
 
You have to preserve the aspect ratio ...try
app:layout_constraintDimensionRatio="h,16:9" within the xml for the imageView object. You will have to look up or play around with the h 16:9 the width of the image view object must be set to 0 to use
app:layout_constraintDimensionRatio="h,16:9"


Thank you. got clear image in imageView. can you please tell me about compressing image also
 

Hi the size of the image is 60KB when it captured and when it is saved to file it is 60KB, But in the Activity 2 when i retrive image from sdcard it is becoimng 121 KB why is that, can You tell me, Am attaching my code also.
Code:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
   Bundle extras = data.getExtras();
    Bitmap imageBitmap = (Bitmap) extras.get( "data" );
    long height = imageBitmap.getHeight();                              //204
    long width = imageBitmap.getWidth();                                //153
    long filesizeinBytes1 = imageBitmap.getByteCount();
    long fileSizeInKB1 = filesizeinBytes1 / 1024;                      //60KB
    Log.d( TAG, "Textview message :"+ height + " , " + width + " , " + fileSizeInKB1 );
    System.out.print( fileSizeInKB1 );

    if (fileSizeInKB1 >= 500) {
        Bitmap convertedimage = getResizedBitmap( imageBitmap, 100 );
        savefile( convertedimage );
    } else {
        savefile( imageBitmap );
    }
    Intent intent = new Intent( ScancodeActivity.this, Display.class );
    startActivity( intent );
}

private void savefile(Bitmap image) {
    File destination = new File( Environment.getExternalStorageDirectory(), "capture_01.bmp" );
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    image.compress( Bitmap.CompressFormat.JPEG, 100, bytes );
    long filesizeinBytes = image.getByteCount();
    long fileSizeInKB = filesizeinBytes / 1024;                           //60KB

    FileOutputStream fo;
    try {
        fo = new FileOutputStream( destination );
        fo.write( bytes.toByteArray() );
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

In Activity 2 (Display Activity)
Code:
 File destination = new File( Environment.getExternalStorageDirectory(), "capture_01.bmp" );
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  Bitmap bmp= BitmapFactory.decodeFile( destination.getAbsolutePath() );
  //bmp.compress( Bitmap.CompressFormat.JPEG,100,bytes );
  image.setImageBitmap( bmp);

     long height = bmp.getHeight();                                       //204
     long width = bmp.getWidth();                                        //153
     long filesizeinBytes1 = bmp.getByteCount();
     long fileSizeInKB1 = filesizeinBytes1 / 1024;               //121KB
     textview1.setText( " Height:" + height + "Width" + width + "size in KB:  " + fileSizeInKB1 );
     Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );

But in SD card it is 24KB.
Why is that please tell me.
 
Back
Top Bottom