Phonegap Build iOS camera.getPicture() quality par

2020-04-26 06:31发布

问题:

I have written a Phonegap application and compiled it using the Build service. My application calls the camera in this block of code:

function capturePhoto() {

        // Take picture using device camera and retrieve image 
            navigator.camera.getPicture(onPhotoDataSuccess, onFail, { 
              quality: 20,
              destinationType: destinationType.FILE_URI });

    }

The resulting image is uploaded to a server via ajax. When I review the uploaded images, some arrive with their quality reduced appropriately but some arrive with a filesize of ~4 MB. I have realized that users who are running my app on iPhone 4 and 5 (I have no testers with 6 yet) are consistently uploading the large images.

I have reduced quality to 10 and 5. Image size of Android-submitted images are appropriately reduced- but still IOS images are large. Can anyone tell me if there is a known issue here?

The API Docs say that you cannot reduce quality of images selected from gallery or camera roll- but these are not. They are loaded from camera.

回答1:

This is the code the plugin uses for processing the image after taking the picture:

- (NSData*)processImage:(UIImage*)image info:(NSDictionary*)info options:(CDVPictureOptions*)options
{
    NSData* data = nil;

    switch (options.encodingType) {
        case EncodingTypePNG:
            data = UIImagePNGRepresentation(image);
            break;
        case EncodingTypeJPEG:
        {
            if ((options.allowsEditing == NO) && (options.targetSize.width <= 0) && (options.targetSize.height <= 0) && (options.correctOrientation == NO)){
                // use image unedited as requested , don't resize
                data = UIImageJPEGRepresentation(image, 1.0);
            } else {
                if (options.usesGeolocation) {
                    NSDictionary* controllerMetadata = [info objectForKey:@"UIImagePickerControllerMediaMetadata"];
                    if (controllerMetadata) {
                        self.data = data;
                        self.metadata = [[NSMutableDictionary alloc] init];

                        NSMutableDictionary* EXIFDictionary = [[controllerMetadata objectForKey:(NSString*)kCGImagePropertyExifDictionary]mutableCopy];
                        if (EXIFDictionary) {
                            [self.metadata setObject:EXIFDictionary forKey:(NSString*)kCGImagePropertyExifDictionary];
                        }

                        if (IsAtLeastiOSVersion(@"8.0")) {
                            [[self locationManager] performSelector:NSSelectorFromString(@"requestWhenInUseAuthorization") withObject:nil afterDelay:0];
                        }
                        [[self locationManager] startUpdatingLocation];
                    }
                } else {
                    data = UIImageJPEGRepresentation(image, [options.quality floatValue] / 100.0f);
                }
            }
        }
            break;
        default:
            break;
    };

    return data;
}

So, if options.encodingType is PNG the image quality isn't change. The default value is JPG, so the problem isn't there.

But then check the

if ((options.allowsEditing == NO) && (options.targetSize.width <= 0) && (options.targetSize.height <= 0) && (options.correctOrientation == NO))

allowsEditing is NO by default

targetSize.width and targetSize.height are 0 by default

correctOrientation is NO by default

So all the conditions of the if are meet, and no change in quality is done.

It sounds like a bug as it isn't mentioned anywhere that you have to specify any other value to make the quality param work.

So, what can you do now?

You can file an issue on the cordova jira page https://issues.apache.org/jira/browse/CB And/or you can pass any param to change the default value and make the code to skip the if and go to the else with the quality code. Changing any of this should work:

  allowEdit : true,
  targetWidth: 100,
  targetHeight: 100,
  correctOrientation: true

EDIT: there is already a bug open https://issues.apache.org/jira/browse/CB-6190

EDIT 2: I fixed the issue, you can get the changes with cordova plugin add https://github.com/apache/cordova-plugin-camera or wait until it's released to NPM



回答2:

There is no issue. iOS has larger pictures. They call it a feature.

http://lifeinlofi.com/more/iphone-photo-sizes-2007-2013/

Jesse