Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-4027

Using targetWidth & targetHeight with camera.getPicture does not return scaled image on WP7 WP8 PhoneGap




      I have built an app that uses the camera.getPicture.

      I am using targetWidth & targetHeight to shrink the image to a smaller size before saving it to a server.
      The PhoneGap functionality is supposed to scale the image so as not to lose the aspect ratio of the original image.
      As mentioned in http://docs.phonegap.com/en/2.8.0/cordova_camera_camera.md.html#Camera

      The functionality is working fine in iOS and Android, but in Windows Phone it turns it into a square with the width and height as set by targetWidth & targetHeight.

      This is the call;
      navigator.camera.getPicture(app.onPhotoDataSuccess, app.onPhotoDataFail,

      { quality: 50, targetWidth:400, targetHeight:400, allowEdit: false, destinationType: app.destinationType.DATA_URL }


      Looking at the PhoneGap 2.8 code in Plugins/Camera.cs

      private string GetImageContent(Stream stream)
      int streamLength = (int)stream.Length;
      byte[] fileData = new byte[streamLength + 1];
      stream.Read(fileData, 0, streamLength);

      //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); }


      { // resize photo byte[] resizedFile = ResizePhoto(stream, fileData); stream.Close(); return Convert.ToBase64String(resizedFile); }


      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; }

      You can see the if code that calls to resize the image.

      When you look at the definition for Extensions.SaveJpeg

      public static void LoadJpeg(this WriteableBitmap bitmap, Stream sourceStream);
      // Summary:
      // Encodes a WriteableBitmap object into a JPEG stream, with parameters for
      // setting the target width and height of the JPEG file.
      // Parameters:
      // bitmap:
      // The WriteableBitmap object.
      // targetStream:
      // The image data stream.
      // targetWidth:
      // The target width of the file.
      // targetHeight:
      // The target height of the file.
      // orientation:
      // This parameter is not currently used by this method. Use a value of 0 as
      // a placeholder.
      // quality:
      // This parameter represents the quality of the JPEG photo with a range between
      // 0 and 100, with 100 being the best photo quality. We recommend that you do
      // not fall lower than a value of 70. because JPEG picture quality diminishes
      // significantly below that level.
      public static void SaveJpeg(this WriteableBitmap bitmap, Stream targetStream, int targetWidth, int targetHeight, int orientation, int quality);

      You can not see any reference to the fact that the aspect ratio will be kept anywhere in the code.
      So it looks as if the aspect ratio is not honoured in the Windows Phone implementation of the functionality.

      Can we please have this fixed?

      I originally posted here: https://groups.google.com/forum/#!topic/phonegap/XNOhAqQDn4U





            purplecabbage Jesse MacFadyen
            richard711 Richard Marshall
            0 Vote for this issue
            4 Start watching this issue