android-crop icon indicating copy to clipboard operation
android-crop copied to clipboard

Image Rotation

Open prowsandy opened this issue 8 years ago • 3 comments

Is there any way to make the image to be cropped should be rotate first?

Thanks

prowsandy avatar Apr 21 '17 07:04 prowsandy

Agree. If the image is in portrait mode, it will be rotated. I am trying to solve this problem as the same happens when I simply add uri into imageview

SourceCipher avatar May 17 '17 21:05 SourceCipher

@prowsandy @gwidaz Using android.media.ExifInterface you can fix the image rotation via button click. You could essentially modify the com.soundcloud.android.crop.CropImageActivity to include a "Fix Rotation" button. But this is how you would do it. This is what I use in one of my in-house apps.

public void fixImageRotation(){
	imageview.buildDrawingCache();
	Bitmap original = imageview.getDrawingCache();
	//fileUri is the uri of the selected image
	imageview.setImageBitmap(imageOreintationValidator(original, fileUri.getPath()));
}

private Bitmap imageOreintationValidator(Bitmap bitmap, String path) {

	ExifInterface ei;
	try {
		ei = new ExifInterface(path);
		int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
				ExifInterface.ORIENTATION_NORMAL);
		switch (orientation) {
			case ExifInterface.ORIENTATION_ROTATE_90:
				bitmap = rotateImage(bitmap, 90);
				break;
			case ExifInterface.ORIENTATION_ROTATE_180:
				bitmap = rotateImage(bitmap, 180);
				break;
			case ExifInterface.ORIENTATION_ROTATE_270:
				bitmap = rotateImage(bitmap, 270);
				break;
		}
	} catch (IOException e) {
		e.printStackTrace();
	}

	return bitmap;
}

private Bitmap rotateImage(Bitmap source, float angle) {

	Bitmap bitmap = null;
	Matrix matrix = new Matrix();
	matrix.postRotate(angle);
	try {
		bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
				matrix, true);
	} catch (OutOfMemoryError err) {
		err.printStackTrace();
	}
	return bitmap;
}

Works instantly when the button is clicked and flawlessly.

power7714 avatar Aug 03 '17 20:08 power7714

Solution written by @power7714 is working only when you want to rotate your image after editing.. Basically this solution not show how to rotate image to display it correctly in crop-edit mode.. Lib is deprecated if you are looking for nice lib to use I personally recommend this one: https://github.com/Yalantis/uCrop Best

Wiktorl4z avatar Jan 22 '19 12:01 Wiktorl4z