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

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

Discussion in 'Android Development' started by nagamothu, May 3, 2019.

  1. nagamothu

    nagamothu Newbie
    Thread Starter

    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 (Text):
    1.  
    2. if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK  )
    3. {
    4.     Bundle extras = data.getExtras();
    5.     Bitmap imageBitmap = (Bitmap) extras.get( "data" );
    6.     long height = imageBitmap.getHeight();              // the height is 204
    7.     long width = imageBitmap.getWidth();                //the width is 153
    8.     long filesizeinBytes1 = imageBitmap.getByteCount();
    9.     long fileSizeInKB1 = filesizeinBytes1 / 1024;      // 60KB
    10.     Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
    11.     System.out.print( fileSizeInKB1 );
    12.  
    13.     File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
    14.     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    15.     imageBitmap.compress( Bitmap.CompressFormat.JPEG,100,bytes );
    16.     FileOutputStream fo;
    17.     try {
    18.         fo = new FileOutputStream( destination );
    19.         fo.write( bytes.toByteArray());
    20.         fo.close();
    21.     } catch (IOException e) {
    22.         e.printStackTrace();
    23.     }
    24.     Intent intent = new Intent( this, Main2Activity.class );
    25.     startActivity( intent );
    26.  
    And now am retriving image from sd card to imageview .

    Activity 2(Main2Activity)
    Code (Text):
    1.  
    2. File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
    3. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    4. Bitmap bmp= BitmapFactory.decodeFile( destination.getAbsolutePath() );
    5. bmp.compress( Bitmap.CompressFormat.JPEG,50,bytes );
    6. image.setImageBitmap( bmp);
    7.  
    8. long height = bmp.getHeight();                                  // the height is 204
    9. long width = bmp.getWidth();                                    // the width is 153
    10. long filesizeinBytes1 = bmp.getByteCount();
    11. long fileSizeInKB1 = filesizeinBytes1 / 1024;            // 121KB
    12. textview1.setText( " Height:" + height + "Width" + width + "size in KB:  " + fileSizeInKB1 );
    13. Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
    14. System.out.print( fileSizeInKB1 );
    15.  
    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 ?

     


  2. Best Answer:
    Post #4 by Brian Berry, May 3, 2019 (1 points)
  3. nagamothu

    nagamothu Newbie
    Thread Starter

    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 (Text):
    1.  
    2. if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK  )
    3. {
    4.     Bundle extras = data.getExtras();
    5.     Bitmap imageBitmap = (Bitmap) extras.get( "data" );
    6.     long height = imageBitmap.getHeight();              // the height is 204
    7.     long width = imageBitmap.getWidth();                //the width is 153
    8.     long filesizeinBytes1 = imageBitmap.getByteCount();
    9.     long fileSizeInKB1 = filesizeinBytes1 / 1024;      // 60KB
    10.     Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
    11.     System.out.print( fileSizeInKB1 );
    12.  
    13.     File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
    14.     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    15.     imageBitmap.compress( Bitmap.CompressFormat.JPEG,100,bytes );
    16.     FileOutputStream fo;
    17.     try {
    18.         fo = new FileOutputStream( destination );
    19.         fo.write( bytes.toByteArray());
    20.         fo.close();
    21.     } catch (IOException e) {
    22.         e.printStackTrace();
    23.     }
    24.     Intent intent = new Intent( this, Main2Activity.class );
    25.     startActivity( intent );
    26.  
    And now am retriving image from sd card to imageview .

    Activity 2(Main2Activity)
    Code (Text):
    1.  
    2. File destination = new File( Environment.getExternalStorageDirectory(), "temp.jpg" );
    3. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    4. Bitmap bmp= BitmapFactory.decodeFile( destination.getAbsolutePath() );
    5. bmp.compress( Bitmap.CompressFormat.JPEG,50,bytes );
    6. image.setImageBitmap( bmp);
    7.  
    8. long height = bmp.getHeight();                                  // the height is 204
    9. long width = bmp.getWidth();                                    // the width is 153
    10. long filesizeinBytes1 = bmp.getByteCount();
    11. long fileSizeInKB1 = filesizeinBytes1 / 1024;            // 121KB
    12. textview1.setText( " Height:" + height + "Width" + width + "size in KB:  " + fileSizeInKB1 );
    13. Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
    14. System.out.print( fileSizeInKB1 );
    15.  
    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 ?
     
  4. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator

    duplicates merged and moved to the development area.
     
  5. Brian Berry

    Brian Berry Well-Known Member

    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"
     
    nagamothu likes this.
  6. nagamothu

    nagamothu Newbie
    Thread Starter


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

    nagamothu Newbie
    Thread Starter

    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 (Text):
    1.  
    2. if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
    3.    Bundle extras = data.getExtras();
    4.     Bitmap imageBitmap = (Bitmap) extras.get( "data" );
    5.     long height = imageBitmap.getHeight();                              //204
    6.     long width = imageBitmap.getWidth();                                //153
    7.     long filesizeinBytes1 = imageBitmap.getByteCount();
    8.     long fileSizeInKB1 = filesizeinBytes1 / 1024;                      //60KB
    9.     Log.d( TAG, "Textview message :"+ height + " , " + width + " , " + fileSizeInKB1 );
    10.     System.out.print( fileSizeInKB1 );
    11.  
    12.     if (fileSizeInKB1 >= 500) {
    13.         Bitmap convertedimage = getResizedBitmap( imageBitmap, 100 );
    14.         savefile( convertedimage );
    15.     } else {
    16.         savefile( imageBitmap );
    17.     }
    18.     Intent intent = new Intent( ScancodeActivity.this, Display.class );
    19.     startActivity( intent );
    20. }
    21.  
    22. private void savefile(Bitmap image) {
    23.     File destination = new File( Environment.getExternalStorageDirectory(), "capture_01.bmp" );
    24.     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    25.     image.compress( Bitmap.CompressFormat.JPEG, 100, bytes );
    26.     long filesizeinBytes = image.getByteCount();
    27.     long fileSizeInKB = filesizeinBytes / 1024;                           //60KB
    28.  
    29.     FileOutputStream fo;
    30.     try {
    31.         fo = new FileOutputStream( destination );
    32.         fo.write( bytes.toByteArray() );
    33.         fo.close();
    34.     } catch (IOException e) {
    35.         e.printStackTrace();
    36.     }
    37.  
    38. }
    39.  
    In Activity 2 (Display Activity)
    Code (Text):
    1.  
    2.  File destination = new File( Environment.getExternalStorageDirectory(), "capture_01.bmp" );
    3. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    4.   Bitmap bmp= BitmapFactory.decodeFile( destination.getAbsolutePath() );
    5.   //bmp.compress( Bitmap.CompressFormat.JPEG,100,bytes );
    6.   image.setImageBitmap( bmp);
    7.  
    8.      long height = bmp.getHeight();                                       //204
    9.      long width = bmp.getWidth();                                        //153
    10.      long filesizeinBytes1 = bmp.getByteCount();
    11.      long fileSizeInKB1 = filesizeinBytes1 / 1024;               //121KB
    12.      textview1.setText( " Height:" + height + "Width" + width + "size in KB:  " + fileSizeInKB1 );
    13.      Log.d( TAG, "Textview message :" + height + " , " + width + " , " + fileSizeInKB1 );
    14.  
    But in SD card it is 24KB.
    Why is that please tell me.
     
Loading...

Share This Page

Loading...