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
IN Display Activity.java
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 );
[*]
[*]}
[*]
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();
[*] }
[*]