Rotate image if taken in portrait mode

This commit is contained in:
macdonst 2012-06-26 23:50:52 -04:00
parent e0e4ba2bd7
commit dddce30368
2 changed files with 45 additions and 0 deletions

View File

@ -280,6 +280,11 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
if (destType == DATA_URL) {
bitmap = getScaledBitmap(FileUtils.stripFileProtocol(imageUri.toString()));
rotate = exif.getOrientation();
if (rotate != 0) {
bitmap = getRotatedBitmap(rotate, bitmap);
}
this.processPicture(bitmap);
checkForDuplicateImage(DATA_URL);
}
@ -305,6 +310,11 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
} else {
bitmap = getScaledBitmap(FileUtils.stripFileProtocol(imageUri.toString()));
rotate = exif.getOrientation();
if (rotate != 0) {
bitmap = getRotatedBitmap(rotate, bitmap);
}
// Add compressed version of captured image to returned media store Uri
OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
@ -432,6 +442,25 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie
}
}
/**
* Figure out if the bitmap should be rotated. For instance if the picture was taken in
* portrait mode
*
* @param rotate
* @param bitmap
* @return rotated bitmap
*/
private Bitmap getRotatedBitmap(int rotate, Bitmap bitmap) {
Matrix matrix = new Matrix();
if (rotate == 180) {
matrix.setRotate(rotate);
} else {
matrix.setRotate(rotate, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
}
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return bitmap;
}
/**
* In the special case where the default width, height and quality are unchanged
* we just write the file out to disk saving the expensive Bitmap.compress function.

View File

@ -162,4 +162,20 @@ public class ExifHelper {
this.outFile.saveAttributes();
}
public int getOrientation() {
int o = Integer.parseInt(this.orientation);
if (o == ExifInterface.ORIENTATION_NORMAL) {
return 0;
} else if (o == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (o == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (o == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
} else {
return 0;
}
}
}