Rotate image if taken in portrait mode
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/dddce303 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/dddce303 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/dddce303 Branch: refs/heads/master Commit: dddce3036827263e6587e94b1e78b85de9de789b Parents: e0e4ba2 Author: macdonst <[email protected]> Authored: Tue Jun 26 23:50:52 2012 -0400 Committer: macdonst <[email protected]> Committed: Thu Jun 28 12:00:19 2012 -0400 ---------------------------------------------------------------------- .../src/org/apache/cordova/CameraLauncher.java | 29 +++++++++++++++ framework/src/org/apache/cordova/ExifHelper.java | 16 ++++++++ 2 files changed, 45 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/dddce303/framework/src/org/apache/cordova/CameraLauncher.java ---------------------------------------------------------------------- diff --git a/framework/src/org/apache/cordova/CameraLauncher.java b/framework/src/org/apache/cordova/CameraLauncher.java index cfe58a6..5d1d2b6 100755 --- a/framework/src/org/apache/cordova/CameraLauncher.java +++ b/framework/src/org/apache/cordova/CameraLauncher.java @@ -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); @@ -433,6 +443,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. * http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/dddce303/framework/src/org/apache/cordova/ExifHelper.java ---------------------------------------------------------------------- diff --git a/framework/src/org/apache/cordova/ExifHelper.java b/framework/src/org/apache/cordova/ExifHelper.java index 8b4d179..c4f7d91 100644 --- a/framework/src/org/apache/cordova/ExifHelper.java +++ b/framework/src/org/apache/cordova/ExifHelper.java @@ -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; + } + } }
