Skip to content

Commit

Permalink
Merge pull request #17 from spoonconsulting/save-height-width-of-image
Browse files Browse the repository at this point in the history
save height and width for on imagePicker
  • Loading branch information
parveshneedhoo authored Nov 10, 2023
2 parents 86f002b + 01ed793 commit 2ec7de0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOD.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [2.0.5](https://github.com/spoonconsulting/cordova-plugin-telerik-imagepicker/compare/2.0.4...2.0.5) (2023-11-10)

* **android:** Return the Height and Width of selected images along with their paths.
* **ios:** Return the Height and Width of selected images along with their paths.

## [2.0.4](https://github.com/spoonconsulting/cordova-plugin-telerik-imagepicker/compare/2.0.3...2.0.4) (2023-07-12)

* **ios:** Use interfaceOrientation for ios 13+.
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ Example - Get Full Size Images (all default options):
window.imagePicker.getPictures(
function(results) {
for (var i = 0; i < results.length; i++) {
console.log('Image URI: ' + results[i]);
console.log('Image URI: ' + results[i].path);
console.log('Image Height: ' + results[i].height);
console.log('Image Width: ' + results[i].width);
}
}, function (error) {
console.log('Error: ' + error);
Expand All @@ -40,7 +42,9 @@ Example - Get at most 10 images scaled to width of 800:
window.imagePicker.getPictures(
function(results) {
for (var i = 0; i < results.length; i++) {
console.log('Image URI: ' + results[i]);
console.log('Image URI: ' + results[i].path);
console.log('Image Height: ' + results[i].height);
console.log('Image Width: ' + results[i].width);
}
}, function (error) {
console.log('Error: ' + error);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spoonconsulting/cordova-plugin-telerik-imagepicker",
"version": "2.0.4",
"version": "2.0.5",
"cordova_name": "ImagePicker",
"description": "This plugin allows selection of multiple images from the camera roll / gallery in a phonegap app",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="@spoonconsulting/cordova-plugin-telerik-imagepicker"
version="2.0.4">
version="2.0.5">

<name>ImagePicker</name>

Expand Down
15 changes: 14 additions & 1 deletion src/android/ImagePicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
Expand Down Expand Up @@ -76,6 +77,7 @@ public boolean execute(String action, final JSONArray args, final CallbackContex

public void onActivityResult(int requestCode, int resultCode, Intent data) {
ExecutorService executor = Executors.newSingleThreadExecutor();
ArrayList<JSONObject> imageInfos = new ArrayList<>();
executor.execute(() -> {
try {
cordova.getActivity().runOnUiThread(() -> {
Expand Down Expand Up @@ -111,7 +113,18 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}
}
JSONArray res = new JSONArray(fileURIs);
for (int i=0; i < fileURIs.size(); i++) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Uri ImageUri = Uri.parse(fileURIs.get(i));
BitmapFactory.decodeFile(new File(ImageUri.getPath()).getAbsolutePath(), options);
JSONObject json = new JSONObject();
json.put("path", fileURIs.get(i));
json.put("width", options.outWidth);
json.put("height", options.outHeight);
imageInfos.add(json);
}
JSONArray res = new JSONArray(imageInfos);
callbackContext.success(res);
} else if (resultCode == Activity.RESULT_CANCELED && data != null) {
String error = data.getStringExtra("ERRORMESSAGE");
Expand Down
32 changes: 24 additions & 8 deletions src/ios/SOSPicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ - (void)assetsPickerController:(GMImagePickerController *)picker didFinishPickin

NSLog(@"GMImagePicker: User finished picking assets. Number of selected items is: %lu", (unsigned long)fetchArray.count);

NSMutableArray * result_all = [[NSMutableArray alloc] init];
NSMutableArray * resultList = [[NSMutableArray alloc] init];
CGSize targetSize = CGSizeMake(self.width, self.height);
NSFileManager* fileMgr = [[NSFileManager alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
Expand All @@ -201,17 +201,24 @@ - (void)assetsPickerController:(GMImagePickerController *)picker didFinishPickin
do {
filePath = [NSString stringWithFormat:@"%@/%@.%@", libPath, [[NSUUID UUID] UUIDString], @"jpg"];
} while ([fileMgr fileExistsAtPath:filePath]);

NSData* data = nil;
if (self.width == 0 && self.height == 0) {
// no scaling required
if (self.outputType == BASE64_STRING){
UIImage* image = [UIImage imageNamed:item.image_fullsize];
[result_all addObject:[UIImageJPEGRepresentation(image, self.quality/100.0f) base64EncodedStringWithOptions:0]];
NSDictionary *imageInfo = @{@"path":[UIImageJPEGRepresentation(image, self.quality/100.0f) base64EncodedStringWithOptions:0],
@"width": [NSNumber numberWithFloat:image.size.width],
@"height": [NSNumber numberWithFloat:image.size.height]};
[resultList addObject: imageInfo];
} else {
if (self.quality == 100) {
// no scaling, no downsampling, this is the fastest option
[result_all addObject:item.image_fullsize];
UIImage* image = [UIImage imageNamed:item.image_fullsize];
NSDictionary *imageInfo = @{@"path":item.image_fullsize,
@"width": [NSNumber numberWithFloat:image.size.width],
@"height": [NSNumber numberWithFloat:image.size.height]};
[resultList addObject: imageInfo];

} else {
// resample first
Expand All @@ -221,7 +228,10 @@ - (void)assetsPickerController:(GMImagePickerController *)picker didFinishPickin
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[err localizedDescription]];
break;
} else {
[result_all addObject:[[NSURL fileURLWithPath:filePath] absoluteString]];
NSDictionary *imageInfo = @{@"path":[[NSURL fileURLWithPath:filePath] absoluteString],
@"width": [NSNumber numberWithFloat:image.size.width],
@"height": [NSNumber numberWithFloat:image.size.height]};
[resultList addObject: imageInfo];
}
}
}
Expand All @@ -236,16 +246,22 @@ - (void)assetsPickerController:(GMImagePickerController *)picker didFinishPickin
break;
} else {
if(self.outputType == BASE64_STRING){
[result_all addObject:[data base64EncodedStringWithOptions:0]];
NSDictionary *imageInfo = @{@"path":[data base64EncodedStringWithOptions:0],
@"width": [NSNumber numberWithFloat:scaledImage.size.width],
@"height": [NSNumber numberWithFloat:scaledImage.size.height]};
[resultList addObject: imageInfo];
} else {
[result_all addObject:[[NSURL fileURLWithPath:filePath] absoluteString]];
NSDictionary *imageInfo = @{@"path":[[NSURL fileURLWithPath:filePath] absoluteString],
@"width": [NSNumber numberWithFloat:scaledImage.size.width],
@"height": [NSNumber numberWithFloat:scaledImage.size.height]};
[resultList addObject: imageInfo];
}
}
}
}

if (result == nil) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:result_all];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:resultList];
}

[self.viewController dismissViewControllerAnimated:YES completion:nil];
Expand Down

0 comments on commit 2ec7de0

Please sign in to comment.