You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2012/06/21 02:06:54 UTC

[3/3] wp7 commit: CB-570 resize image if user defined width & height provided

CB-570 resize image if user defined width & height provided


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/commit/c7a075e3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/tree/c7a075e3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/diff/c7a075e3

Branch: refs/heads/master
Commit: c7a075e3b1b07f6931f030219272c6fc9844528d
Parents: 7d21d6c
Author: hermwong <he...@gmail.com>
Authored: Wed Jun 20 16:50:09 2012 -0700
Committer: hermwong <he...@gmail.com>
Committed: Wed Jun 20 16:50:09 2012 -0700

----------------------------------------------------------------------
 framework/Cordova/Commands/Camera.cs |   51 +++++++++++++++++++++++++++--
 1 files changed, 48 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/c7a075e3/framework/Cordova/Commands/Camera.cs
----------------------------------------------------------------------
diff --git a/framework/Cordova/Commands/Camera.cs b/framework/Cordova/Commands/Camera.cs
index 0a1ae9e..ec0cc1c 100644
--- a/framework/Cordova/Commands/Camera.cs
+++ b/framework/Cordova/Commands/Camera.cs
@@ -333,13 +333,58 @@ namespace WP7CordovaClassLib.Cordova.Commands
         {
             int streamLength = (int)stream.Length;
             byte[] fileData = new byte[streamLength + 1];
-            stream.Read(fileData, 0, streamLength);
-            stream.Close();
+            stream.Read(fileData, 0, streamLength);        
 
-            return Convert.ToBase64String(fileData);
+            //use photo's actual width & height if user doesn't provide width & height
+            if (cameraOptions.TargetWidth < 0 && cameraOptions.TargetHeight < 0)
+            {
+                stream.Close();
+                return Convert.ToBase64String(fileData);                
+            }
+            else
+            {
+                // resize photo
+                byte[] resizedFile = resizePhoto(stream, fileData);
+                stream.Close();
+                return Convert.ToBase64String(resizedFile);
+            }
         }
 
+        /// <summary>
+        /// Resize image
+        /// </summary>
+        /// <param name="stream">Image stream</param>
+        /// <param name="fileData">File data</param>
+        /// <returns>resized image</returns>
+        private byte[] resizePhoto(Stream stream, byte[] fileData)
+        {
+            int streamLength = (int)stream.Length;
+            int intResult = 0;
+
+            byte[] resizedFile;
+
+            stream.Read(fileData, 0, streamLength);
 
+            BitmapImage objBitmap = new BitmapImage();
+            MemoryStream objBitmapStream = new MemoryStream(fileData);
+            MemoryStream objBitmapStreamResized = new MemoryStream();
+            WriteableBitmap objWB;
+            objBitmap.SetSource(stream);
+            objWB = new WriteableBitmap(objBitmap);
+
+            // resize the photo with user defined TargetWidth & TargetHeight
+            Extensions.SaveJpeg(objWB, objBitmapStreamResized, cameraOptions.TargetWidth, cameraOptions.TargetHeight, 0, cameraOptions.Quality);
+
+            //Convert the resized stream to a byte array. 
+            streamLength = (int)objBitmapStreamResized.Length;
+            resizedFile = new Byte[streamLength]; //-1 
+            objBitmapStreamResized.Position = 0;
+            //for some reason we have to set Position to zero, but we don't have to earlier when we get the bytes from the chosen photo... 
+            intResult = objBitmapStreamResized.Read(resizedFile, 0, streamLength);
+
+            return resizedFile;
+        }
+        
         /// <summary>
         /// Saves captured image in isolated storage
         /// </summary>