Apache Cordova
  1. Apache Cordova
  2. CB-54

Feature request: ability to downsize pictures with camera.getPicture (no crop)

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: 1.7.0
    • Component/s: iOS
    • Labels:
      None

      Description

      reported at: https://github.com/phonegap/phonegap-iphone/issues/287
      by: https://github.com/patdenice

      Hi,

      We're working on a mobile application using phonegap. The application is designed to upload photos from the smartphone/tablet to a Piwigo gallery. Piwigo is a popular open source web application to create photo galleries. The application "Piwigo for iOS/Android" is nearly complete, we just need the ability to downsize photos before upload. Unfortunately, targetWidth and targetHeight parameters on camera.getPicture method are designed to crop the photo, which is what we're seeking. Would it be possible to add maxWidth/maxHeight parameters and resize algorithm ?

      Thanks.

      P@t
      Piwigo Team.

        Issue Links

          Activity

          Hide
          Shazron Abdullah added a comment -

          by: https://github.com/patdenice

          I finally wrote a phonegap plugin to extend camera API:

          In my app, I call camera.getResizedPicture method instead of camera.getPicture with same parameters. With targetWidth and targetHeight parameters, my image is just resized and not cropped.

          cameraExtended.js

          Camera.prototype.getResizedPicture = function(successCallback, errorCallback, options) {

          // successCallback required
          if (typeof successCallback != "function")

          { console.log("Camera Error: successCallback is not a function"); return; }

          // errorCallback optional
          if (errorCallback && (typeof errorCallback != "function"))

          { console.log("Camera Error: errorCallback is not a function"); return; }

          PhoneGap.exec(successCallback, errorCallback, "com.phonegap.cameraExtended","getPicture",[options]);
          };

          cameraExtended.h

          #import "PhoneGap/Camera.h"

          @interface PGCameraExtended : PGCamera<UIImagePickerControllerDelegate,
          UINavigationControllerDelegate,
          UIPopoverControllerDelegate>
          {
          }

          • (UIImage*)imageByScalingAndCroppingForSize:(UIImage*)anImage toSize:(CGSize)targetSize;

          @end

          cameraExtended.m

          #import "cameraExtended.h"

          @implementation PGCameraExtended

          • (UIImage*)imageByScalingAndCroppingForSize:(UIImage*)anImage toSize:(CGSize)targetSize
            {
            UIImage *sourceImage = anImage;
            UIImage *newImage = nil;
            CGSize imageSize = sourceImage.size;
            CGFloat width = imageSize.width;
            CGFloat height = imageSize.height;
            CGFloat targetWidth = targetSize.width;
            CGFloat targetHeight = targetSize.height;
            CGFloat scaledWidth = targetWidth;
            CGFloat scaledHeight = targetHeight;

          CGFloat ratio_width = width / targetWidth;
          CGFloat ratio_height = height / targetHeight;

          // maximal size exceeded ?
          if (ratio_width > 1 || ratio_height > 1)
          {
          if (ratio_width < ratio_height)

          { scaledWidth = ceil(width / ratio_height); }

          else

          { scaledHeight = ceil(height / ratio_width); }

          targetSize = CGSizeMake(scaledWidth, scaledHeight);
          self.pickerController.targetSize = targetSize;
          }
          else

          { return sourceImage; }

          UIGraphicsBeginImageContext(targetSize);

          CGRect thumbnailRect = CGRectZero;
          thumbnailRect.size.width = scaledWidth;
          thumbnailRect.size.height = scaledHeight;

          [sourceImage drawInRect:thumbnailRect];

          newImage = UIGraphicsGetImageFromCurrentImageContext();
          if(newImage == nil)
          NSLog(@"could not scale image");

          //pop the context to get back to the default
          UIGraphicsEndImageContext();
          return newImage;
          }

          @end

          Show
          Shazron Abdullah added a comment - by: https://github.com/patdenice I finally wrote a phonegap plugin to extend camera API: In my app, I call camera.getResizedPicture method instead of camera.getPicture with same parameters. With targetWidth and targetHeight parameters, my image is just resized and not cropped. cameraExtended.js Camera.prototype.getResizedPicture = function(successCallback, errorCallback, options) { // successCallback required if (typeof successCallback != "function") { console.log("Camera Error: successCallback is not a function"); return; } // errorCallback optional if (errorCallback && (typeof errorCallback != "function")) { console.log("Camera Error: errorCallback is not a function"); return; } PhoneGap.exec(successCallback, errorCallback, "com.phonegap.cameraExtended","getPicture", [options] ); }; cameraExtended.h #import "PhoneGap/Camera.h" @interface PGCameraExtended : PGCamera<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverControllerDelegate> { } (UIImage*)imageByScalingAndCroppingForSize:(UIImage*)anImage toSize:(CGSize)targetSize; @end cameraExtended.m #import "cameraExtended.h" @implementation PGCameraExtended (UIImage*)imageByScalingAndCroppingForSize:(UIImage*)anImage toSize:(CGSize)targetSize { UIImage *sourceImage = anImage; UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetWidth = targetSize.width; CGFloat targetHeight = targetSize.height; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; CGFloat ratio_width = width / targetWidth; CGFloat ratio_height = height / targetHeight; // maximal size exceeded ? if (ratio_width > 1 || ratio_height > 1) { if (ratio_width < ratio_height) { scaledWidth = ceil(width / ratio_height); } else { scaledHeight = ceil(height / ratio_width); } targetSize = CGSizeMake(scaledWidth, scaledHeight); self.pickerController.targetSize = targetSize; } else { return sourceImage; } UIGraphicsBeginImageContext(targetSize); CGRect thumbnailRect = CGRectZero; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect] ; newImage = UIGraphicsGetImageFromCurrentImageContext(); if(newImage == nil) NSLog(@"could not scale image"); //pop the context to get back to the default UIGraphicsEndImageContext(); return newImage; } @end
          Hide
          Shazron Abdullah added a comment -

          by: https://github.com/jmunkki

          I think it would be better to add new parameters to the settings map from JavaScript as suggested by patdenice. We have the same issue with the current version of PhoneGap. We are primarily interested in the Android version.

          by: https://github.com/jmunkki

          I read through the iOS and Android source code for the camera module. On Android, you can specify just one of the target size parameters and if you are getting the data in base64 encoded format, you will get a scaled down image with no cropping. In other words, if your camera module is 1024x768 (4:3) and you specify a targetWidth or 240, you will get a 240x180 result in landscape and a 240x320 result in portrait orientation. It's probably not exactly what you were hoping for, but it may be good enough (it's probably good enough for us).

          On iOS, if either the target width or height is missing, the other one is also ignored, so this will not work.

          Show
          Shazron Abdullah added a comment - by: https://github.com/jmunkki I think it would be better to add new parameters to the settings map from JavaScript as suggested by patdenice. We have the same issue with the current version of PhoneGap. We are primarily interested in the Android version. by: https://github.com/jmunkki I read through the iOS and Android source code for the camera module. On Android, you can specify just one of the target size parameters and if you are getting the data in base64 encoded format, you will get a scaled down image with no cropping. In other words, if your camera module is 1024x768 (4:3) and you specify a targetWidth or 240, you will get a 240x180 result in landscape and a 240x320 result in portrait orientation. It's probably not exactly what you were hoping for, but it may be good enough (it's probably good enough for us). On iOS, if either the target width or height is missing, the other one is also ignored, so this will not work.
          Hide
          Shazron Abdullah added a comment -

          Escalating as a bug, not new feature. Related: https://github.com/apache/incubator-cordova-ios/pull/5

          "no crop" should be the default like the other platforms

          Show
          Shazron Abdullah added a comment - Escalating as a bug, not new feature. Related: https://github.com/apache/incubator-cordova-ios/pull/5 "no crop" should be the default like the other platforms
          Show
          Shazron Abdullah added a comment - see https://github.com/apache/incubator-cordova-ios/pull/12
          Hide
          Shazron Abdullah added a comment -

          Fixed in CB-183

          Show
          Shazron Abdullah added a comment - Fixed in CB-183
          Hide
          Shazron Abdullah added a comment -

          An update: I had to remove the JavaScript option cropToSize - the team doesn't want to add more iOS specific functions. What I did however was to make cropToSize = NO the default in the Objective-C code, which is the default in the other platform implementations.

          Show
          Shazron Abdullah added a comment - An update: I had to remove the JavaScript option cropToSize - the team doesn't want to add more iOS specific functions. What I did however was to make cropToSize = NO the default in the Objective-C code, which is the default in the other platform implementations.

            People

            • Assignee:
              Shazron Abdullah
              Reporter:
              Shazron Abdullah
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development