Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output type option - File URI or Base64 Encoded String #65

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,60 @@ The plugin creates the object `window.imagePicker` with the method `getPictures(
Example - Get Full Size Images (all default options):
```javascript
window.imagePicker.getPictures(
function(results) {
for (var i = 0; i < results.length; i++) {
console.log('Image URI: ' + results[i]);
}
}, function (error) {
console.log('Error: ' + error);
}
function(results) {
for (var i = 0; i < results.length; i++) {
console.log('Image URI: ' + results[i]);
}
}, function (error) {
console.log('Error: ' + error);
}
);
```

Example - Get at most 10 images scaled to width of 800:
```javascript
window.imagePicker.getPictures(
function(results) {
for (var i = 0; i < results.length; i++) {
console.log('Image URI: ' + results[i]);
}
}, function (error) {
console.log('Error: ' + error);
}, {
maximumImagesCount: 10,
width: 800
}
function(results) {
for (var i = 0; i < results.length; i++) {
console.log('Image URI: ' + results[i]);
}
}, function (error) {
console.log('Error: ' + error);
}, {
maximumImagesCount: 10,
width: 800
}
);
```

### Options

options = {
// max images to be selected, defaults to 15. If this is set to 1, upon
// selection of a single image, the plugin will return it.
maximumImagesCount: int,

// max width and height to allow the images to be. Will keep aspect
// ratio no matter what. So if both are 800, the returned image
// will be at most 800 pixels wide and 800 pixels tall. If the width is
// 800 and height 0 the image will be 800 pixels wide if the source
// is at least that wide.
width: int,
height: int,

// quality of resized image, defaults to 100
quality: int (0-100)
// selection of a single image, the plugin will return it.
maximumImagesCount: int,

// max width and height to allow the images to be. Will keep aspect
// ratio no matter what. So if both are 800, the returned image
// will be at most 800 pixels wide and 800 pixels tall. If the width is
// 800 and height 0 the image will be 800 pixels wide if the source
// is at least that wide.
width: int,
height: int,

// quality of resized image, defaults to 100
quality: int (0-100),

// output type, defaults to FILE_URIs.
// available options are
// window.imagePicker.OutputType.FILE_URI (0) or
// window.imagePicker.OutputType.BASE64_STRING (1)
outputType: int
};

### Note for Android Use

The plugin returns images that are stored in a temporary directory. These images will often not be deleted automatically though. The files should be moved or deleted after you get their filepaths in javascript.
When outputType is FILE_URI the plugin returns images that are stored in a temporary directory. These images will often not be deleted automatically though. The files should be moved or deleted after you get their filepaths in javascript. If Base64 Strings are being returned, there is nothing to clean up.

## Libraries used

Expand Down
40 changes: 38 additions & 2 deletions src/android/Library/src/MultiImageChooserActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
package com.synconset;

import java.net.URI;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -62,6 +63,7 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Display;
Expand All @@ -86,6 +88,7 @@ public class MultiImageChooserActivity extends Activity implements OnItemClickLi
public static final String WIDTH_KEY = "WIDTH";
public static final String HEIGHT_KEY = "HEIGHT";
public static final String QUALITY_KEY = "QUALITY";
public static final String OUTPUT_TYPE_KEY = "OUTPUT_TYPE";

private ImageAdapter ia;

Expand All @@ -106,6 +109,7 @@ public class MultiImageChooserActivity extends Activity implements OnItemClickLi
private int desiredWidth;
private int desiredHeight;
private int quality;
private OutputType outputType;

private GridView gridView;

Expand All @@ -130,6 +134,7 @@ public void onCreate(Bundle savedInstanceState) {
desiredHeight = getIntent().getIntExtra(HEIGHT_KEY, 0);
quality = getIntent().getIntExtra(QUALITY_KEY, 0);
maxImageCount = maxImages;
outputType = OutputType.fromValue(getIntent().getIntExtra(OUTPUT_TYPE_KEY, 0));

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
Expand Down Expand Up @@ -537,8 +542,12 @@ protected ArrayList<String> doInBackground(Set<Entry<String, Integer>>... fileSe
}
}

file = this.storeImage(bmp, file.getName());
al.add(Uri.fromFile(file).toString());
if(outputType == OutputType.FILE_URI) {
file = this.storeImage(bmp, file.getName());
al.add(Uri.fromFile(file).toString());
} else if (outputType == OutputType.BASE64_STRING){
al.add(getBase64OfImage(bmp));
}
}
return al;
} catch(IOException e) {
Expand Down Expand Up @@ -640,6 +649,13 @@ private Bitmap getResizedBitmap(Bitmap bm, float factor) {
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}

private String getBase64OfImage(Bitmap bm) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
}

private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
Expand Down Expand Up @@ -693,4 +709,24 @@ private float calculateScale(int width, int height) {

return scale;
}

enum OutputType {

FILE_URI(0), BASE64_STRING(1);

int value;

OutputType(int value) {
this.value = value;
}

public static OutputType fromValue(int value) {
for (OutputType type : OutputType.values()) {
if (type.value == value) {
return type;
}
}
throw new IllegalArgumentException("Invalid enum value specified");
}
}
}
5 changes: 5 additions & 0 deletions src/android/com/synconset/ImagePicker/ImagePicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
int desiredWidth = 0;
int desiredHeight = 0;
int quality = 100;
int outputType = 0;
if (this.params.has("maximumImagesCount")) {
max = this.params.getInt("maximumImagesCount");
}
Expand All @@ -43,10 +44,14 @@ public boolean execute(String action, final JSONArray args, final CallbackContex
if (this.params.has("quality")) {
quality = this.params.getInt("quality");
}
if (this.params.has("outputType")) {
outputType = this.params.getInt("outputType");
}
intent.putExtra("MAX_IMAGES", max);
intent.putExtra("WIDTH", desiredWidth);
intent.putExtra("HEIGHT", desiredHeight);
intent.putExtra("QUALITY", quality);
intent.putExtra("OUTPUT_TYPE", outputType);
if (this.cordova != null) {
this.cordova.startActivityForResult((CordovaPlugin) this, intent, 0);
}
Expand Down
1 change: 1 addition & 0 deletions src/ios/SOSPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
@property (nonatomic, assign) NSInteger width;
@property (nonatomic, assign) NSInteger height;
@property (nonatomic, assign) NSInteger quality;
@property (nonatomic, assign) NSInteger outputType;

@end
12 changes: 11 additions & 1 deletion src/ios/SOSPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

#define CDV_PHOTO_PREFIX @"cdv_photo_"

typedef enum : NSUInteger {
FILE_URI = 0,
BASE64_STRING = 1
} SOSPickerOutputType;

@implementation SOSPicker

@synthesize callbackId;
Expand All @@ -24,6 +29,7 @@ - (void) getPictures:(CDVInvokedUrlCommand *)command {
self.width = [[options objectForKey:@"width"] integerValue];
self.height = [[options objectForKey:@"height"] integerValue];
self.quality = [[options objectForKey:@"quality"] integerValue];
self.outputType = [[options objectForKey:@"outputType"] integerValue];

// Create the an album controller and image picker
ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] init];
Expand Down Expand Up @@ -95,7 +101,11 @@ - (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPic
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[err localizedDescription]];
break;
} else {
[resultStrings addObject:[[NSURL fileURLWithPath:filePath] absoluteString]];
if(self.outputType == BASE64_STRING){
[resultStrings addObject:[data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
} else {
[resultStrings addObject:[[NSURL fileURLWithPath:filePath] absoluteString]];
}
}
}

Expand Down
24 changes: 22 additions & 2 deletions www/imagepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ var ImagePicker = function() {

};

ImagePicker.prototype.OutputType = {
FILE_URI: 0,
BASE64_STRING: 1
};

ImagePicker.prototype.validateOutputType = function(options){
var outputType = options.outputType;
if(outputType){
if(outputType !== this.OutputType.FILE_URI && outputType !== this.OutputType.BASE64_STRING){
console.log('Invalid output type option entered. Defaulting to FILE_URI. Please use window.imagePicker.OutputType.FILE_URI or window.imagePicker.OutputType.BASE64_STRING');
options.outputType = this.OutputType.FILE_URI;
}
}
};

/*
* success - success callback
* fail - error callback
Expand All @@ -20,19 +35,24 @@ var ImagePicker = function() {
* image will be returned)
* .height - height to resize image to
* .quality - quality of resized image, defaults to 100
* .outputType - type of output returned. defaults to file URIs.
* Please see ImagePicker.OutputType for available values.
*/
ImagePicker.prototype.getPictures = function(success, fail, options) {
if (!options) {
options = {};
}

this.validateOutputType(options);

var params = {
maximumImagesCount: options.maximumImagesCount ? options.maximumImagesCount : 15,
width: options.width ? options.width : 0,
height: options.height ? options.height : 0,
quality: options.quality ? options.quality : 100
quality: options.quality ? options.quality : 100,
outputType: options.outputType ? options.outputType : this.OutputType.FILE_URI
};

return cordova.exec(success, fail, "ImagePicker", "getPictures", [params]);
};

Expand Down