Return-Path: X-Original-To: apmail-incubator-callback-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-callback-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1AB3DDB5F for ; Thu, 5 Jul 2012 19:33:16 +0000 (UTC) Received: (qmail 4532 invoked by uid 500); 5 Jul 2012 19:33:16 -0000 Delivered-To: apmail-incubator-callback-commits-archive@incubator.apache.org Received: (qmail 4479 invoked by uid 500); 5 Jul 2012 19:33:16 -0000 Mailing-List: contact callback-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: callback-dev@incubator.apache.org Delivered-To: mailing list callback-commits@incubator.apache.org Received: (qmail 4471 invoked by uid 99); 5 Jul 2012 19:33:16 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 05 Jul 2012 19:33:16 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id C28ACAFD9; Thu, 5 Jul 2012 19:33:15 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: macdonst@apache.org To: callback-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: android commit: CB-1008: Camera with targetHeight, targetWidth loses image aspect ratio Message-Id: <20120705193315.C28ACAFD9@tyr.zones.apache.org> Date: Thu, 5 Jul 2012 19:33:15 +0000 (UTC) Updated Branches: refs/heads/master c7d6a2eec -> 14870726e CB-1008: Camera with targetHeight, targetWidth loses image aspect ratio 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/14870726 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/14870726 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/14870726 Branch: refs/heads/master Commit: 14870726e070a66bce8ff4d02f7bd8c1cb0e1eaa Parents: c7d6a2e Author: macdonst Authored: Thu Jul 5 15:32:55 2012 -0400 Committer: macdonst Committed: Thu Jul 5 15:32:55 2012 -0400 ---------------------------------------------------------------------- .../src/org/apache/cordova/CameraLauncher.java | 79 +++++++++++++-- 1 files changed, 69 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/14870726/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 1612e0f..3251bb3 100755 --- a/framework/src/org/apache/cordova/CameraLauncher.java +++ b/framework/src/org/apache/cordova/CameraLauncher.java @@ -525,20 +525,79 @@ public class CameraLauncher extends Plugin implements MediaScannerConnectionClie return BitmapFactory.decodeFile(imagePath); } - Bitmap unscaledBitmap = decodeFile(imagePath, - this.targetWidth, this.targetHeight); - return Bitmap.createScaledBitmap(unscaledBitmap, this.targetWidth, this.targetHeight, true); - } - - public static Bitmap decodeFile(String pathName, int dstWidth, int dstHeight) { + // figure out the original width and height of the image BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; - BitmapFactory.decodeFile(pathName, options); + BitmapFactory.decodeFile(imagePath, options); + + // determine the correct aspect ratio + int[] widthHeight = calculateAspectRatio(options.outWidth, options.outHeight); + + // Load in the smallest bitmap possible that is closest to the size we want options.inJustDecodeBounds = false; - options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth, dstHeight); - return BitmapFactory.decodeFile(pathName, options); - } + options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, this.targetWidth, this.targetHeight); + Bitmap unscaledBitmap = BitmapFactory.decodeFile(imagePath, options); + + return Bitmap.createScaledBitmap(unscaledBitmap, widthHeight[0], widthHeight[1], true); + } + /** + * Maintain the aspect ratio so the resulting image does not look smooshed + * + * @param origWidth + * @param origHeight + * @return + */ + public int[] calculateAspectRatio(int origWidth, int origHeight) { + int newWidth = this.targetWidth; + int newHeight = this.targetHeight; + + // If no new width or height were specified return the original bitmap + if (newWidth <= 0 && newHeight <= 0) { + newWidth = origWidth; + newHeight = origHeight; + } + // Only the width was specified + else if (newWidth > 0 && newHeight <= 0) { + newHeight = (newWidth * origHeight) / origWidth; + } + // only the height was specified + else if (newWidth <= 0 && newHeight > 0) { + newWidth = (newHeight * origWidth) / origHeight; + } + // If the user specified both a positive width and height + // (potentially different aspect ratio) then the width or height is + // scaled so that the image fits while maintaining aspect ratio. + // Alternatively, the specified width and height could have been + // kept and Bitmap.SCALE_TO_FIT specified when scaling, but this + // would result in whitespace in the new image. + else { + double newRatio = newWidth / (double) newHeight; + double origRatio = origWidth / (double) origHeight; + + if (origRatio > newRatio) { + newHeight = (newWidth * origHeight) / origWidth; + } else if (origRatio < newRatio) { + newWidth = (newHeight * origWidth) / origHeight; + } + } + + int[] retval = new int[2]; + retval[0] = newWidth; + retval[1] = newHeight; + return retval; + } + + /** + * Figure out what ratio we can load our image into memory at while still being bigger than + * our desired width and height + * + * @param srcWidth + * @param srcHeight + * @param dstWidth + * @param dstHeight + * @return + */ public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth, int dstHeight) { final float srcAspect = (float)srcWidth / (float)srcHeight; final float dstAspect = (float)dstWidth / (float)dstHeight;