From dddce3036827263e6587e94b1e78b85de9de789b Mon Sep 17 00:00:00 2001 From: macdonst Date: Tue, 26 Jun 2012 23:50:52 -0400 Subject: [PATCH] Rotate image if taken in portrait mode --- .../org/apache/cordova/CameraLauncher.java | 29 +++++++++++++++++++ .../src/org/apache/cordova/ExifHelper.java | 16 ++++++++++ 2 files changed, 45 insertions(+) diff --git a/framework/src/org/apache/cordova/CameraLauncher.java b/framework/src/org/apache/cordova/CameraLauncher.java index cfe58a61..5d1d2b6e 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); @@ -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. diff --git a/framework/src/org/apache/cordova/ExifHelper.java b/framework/src/org/apache/cordova/ExifHelper.java index 8b4d179d..c4f7d91e 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; + } + } }