1

I am trying to save an image as cropped from android and then show it in my app. I am using the code below, but when I try to view the image in my app the image quality is not good as in the attached image. Am doing anything wrong? Any help would be great.

printscreeiphone6

My code is:

dipToPixel = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1 && resultCode == getActivity().RESULT_OK && data != null) {

    picUri = data.getData();

    performCrop();

}


if (requestCode == 111 && resultCode == getActivity().RESULT_OK && data != null) {

        Bundle extras = data.getExtras();

        Bitmap bitmapImage = extras.getParcelable("data");

        tweetImage.setImageBitmap(bitmapImage);

        tweetImage.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                tweetImage.getViewTreeObserver().removeOnPreDrawListener(this);

                widthPixel = tweetImage.getMeasuredWidth();
                heightPixel = tweetImage.getMeasuredHeight();

                return true;

            }
        });

        System.out.println("photo added");

        addPhotoVar = 1;
        addPhotoBtn.setText("remove");


}

callbackManager.onActivityResult(requestCode, resultCode, data);


}


private void performCrop() {

try {

    //call the standard crop action intent (the user device may not support it)
    Intent cropIntent = new Intent("com.android.camera.action.CROP");
    //indicate image type and Uri
    cropIntent.setDataAndType(picUri, "image/*");
    //set crop properties
    cropIntent.putExtra("crop", "true");
    //indicate aspect of desired crop
    cropIntent.putExtra("aspectX", 1);
    cropIntent.putExtra("aspectY", 1);
    //indicate output X and Y

    cropIntent.putExtra("outputX", Math.round(screenWidth / dipToPixel)-10);
    cropIntent.putExtra("outputY", Math.round(screenWidth / dipToPixel)-10);
    //retrieve data on return
    cropIntent.putExtra("return-data", true);
    //start the activity - we handle returning in onActivityResult
    startActivityForResult(cropIntent, 111);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
    // display an error message
    String errorMessage = "your device doesn't support the crop action!";
    Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT);
    toast.show();
}
}

Below is the code that I use the image and save to database:

                tweetImage.buildDrawingCache();
                bm = tweetImage.getDrawingCache();

                if (widthPixel < heightPixel) {

                    basePixel = widthPixel;

                }

                else {

                    basePixel = heightPixel;

                }


                if (basePixel > 768) {

                    widthRatio = (float) 768/basePixel;
                    heightRatio = (float) 768/basePixel;


                }

                else {

                    widthRatio = 1;
                    heightRatio = 1;

                }


                Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(widthPixel*widthRatio), (int)(heightPixel*heightRatio), true);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byteArray1 = stream.toByteArray();

                image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg");
saner
  • 821
  • 2
  • 10
  • 32

3 Answers3

1

Change :

bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream);

to :

bmResized.compress(Bitmap.CompressFormat.PNG, 100, stream);

Since JPEG format uses a lossy compression, you should use PNG to save the bitmap, if you don't want quality loss.

Also, you should avoid using com.android.camera.action.CROP intent as it doesn't exist on all the devices as explained here.

There are some alternatives listed on the above link, you may use one of them.

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
  • Consider though that the image will be larger when transitioning from JPEG to PNG. – JoxTraex Jul 14 '16 at 07:22
  • 1
    yeah that will be a trade off, if you don't wanna lose quality. – Ashish Ranjan Jul 14 '16 at 07:24
  • may there also be a problem that causes the image to get smaller while using the line: cropIntent.putExtra("outputX", Math.round(screenWidth / dipToPixel)-10); – saner Jul 14 '16 at 07:30
  • no that's not a problem, also you should not use `com.android.camera.action.CROP` as this intent doesn't exist on all the devices. – Ashish Ranjan Jul 14 '16 at 07:36
  • try to log the values of `widthRatio`, `heightRatio`, `basePixel` and check if they are as expected? – Ashish Ranjan Jul 14 '16 at 07:48
  • No, I guess it is not about the jpg png issue. Somehow the output image becomes 315x315 pixels after cropping function. – saner Jul 14 '16 at 08:02
  • try logging the values and check – Ashish Ranjan Jul 14 '16 at 08:10
  • 315x315 values comes right after the cropping I checked the log values of widthPixel and heightPixel. And when I use cropIntent.putExtra("outputX", 768); it is not working. I click on save after cropping and nothing happens. – saner Jul 14 '16 at 08:14
  • `Math.round(screenWidth / dipToPixel)-10` value must be returning `315` , that's why you're getting 315x315 image, did you try both `outputX` & `outputY` as 768? i.e, `cropIntent.putExtra("outputX", 768);cropIntent.putExtra("outputY", 768);` – Ashish Ranjan Jul 14 '16 at 10:30
1

Use this library, this library manage cropped image quality as well it keeps both image Crop Library

Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33
0

Please refer this link :

https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html

Here are some libraries to consider for image crop:

https://github.com/lvillani/android-cropimage
https://github.com/biokys/cropimage
https://github.com/MMP-forTour/cropimage (forked from the above one)
https://github.com/dtitov/pickncrop