Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
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
2 changes: 1 addition & 1 deletion packages/cloud_firestore/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {
}
}

allprojects {
rootProject.allprojects {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prob don't need this.

repositories {
google()
jcenter()
Expand Down
1 change: 1 addition & 0 deletions packages/image_picker/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ buildscript {
repositories {
google()
jcenter()
mavenLocal()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary change

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need this

}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,10 @@ private void handleImageResult(String path) {
if (pendingResult != null) {
Double maxWidth = methodCall.argument("maxWidth");
Double maxHeight = methodCall.argument("maxHeight");
Double resizeQuality = methodCall.argument("resizeQuality");

String finalImagePath = imageResizer.resizeImageIfNeeded(path, maxWidth, maxHeight);
String finalImagePath =
imageResizer.resizeImageIfNeeded(path, maxWidth, maxHeight, resizeQuality);
finishWithSuccess(finalImagePath);
} else {
throw new IllegalStateException("Received image from picker that was not requested");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ class ImageResizer {
*
* <p>If no resizing is needed, returns the path for the original image.
*/
String resizeImageIfNeeded(String imagePath, Double maxWidth, Double maxHeight) {
String resizeImageIfNeeded(
String imagePath, Double maxWidth, Double maxHeight, Double resizeQuality) {
boolean shouldScale = maxWidth != null || maxHeight != null;

if (!shouldScale) {
return imagePath;
}

try {
File scaledImage = resizedImage(imagePath, maxWidth, maxHeight);
File scaledImage = resizedImage(imagePath, maxWidth, maxHeight, resizeQuality);
exifDataCopier.copyExif(imagePath, scaledImage.getPath());

return scaledImage.getPath();
Expand All @@ -43,7 +44,8 @@ String resizeImageIfNeeded(String imagePath, Double maxWidth, Double maxHeight)
}
}

private File resizedImage(String path, Double maxWidth, Double maxHeight) throws IOException {
private File resizedImage(String path, Double maxWidth, Double maxHeight, Double resizeQuality)
throws IOException {
Bitmap bmp = BitmapFactory.decodeFile(path);
double originalWidth = bmp.getWidth() * 1.0;
double originalHeight = bmp.getHeight() * 1.0;
Expand All @@ -58,6 +60,8 @@ private File resizedImage(String path, Double maxWidth, Double maxHeight) throws
boolean shouldDownscaleHeight = hasMaxHeight && maxHeight < originalHeight;
boolean shouldDownscale = shouldDownscaleWidth || shouldDownscaleHeight;

resizeQuality = resizeQuality != null ? resizeQuality : new Double(1.0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int quality = resizeQuality != null ? ((int) resizeQuality * 100.0) : 100;


if (shouldDownscale) {
double downscaledWidth = (height / originalHeight) * originalWidth;
double downscaledHeight = (width / originalWidth) * originalHeight;
Expand Down Expand Up @@ -85,7 +89,7 @@ private File resizedImage(String path, Double maxWidth, Double maxHeight) throws

Bitmap scaledBmp = Bitmap.createScaledBitmap(bmp, width.intValue(), height.intValue(), false);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
scaledBmp.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
scaledBmp.compress(Bitmap.CompressFormat.JPEG, (int) (resizeQuality * 100), outputStream);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 scaledBmp.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);


String[] pathParts = path.split("/");
String imageName = pathParts[pathParts.length - 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class ImagePickerDelegateTest {
private static final double WIDTH = 10.0;
private static final double HEIGHT = 10.0;
private static final double QUALITY = 0.5;

@Mock Activity mockActivity;
@Mock ImageResizer mockImageResizer;
Expand Down Expand Up @@ -60,12 +61,21 @@ public void setUp() {
when(mockFileUtils.getPathFromUri(any(Context.class), any(Uri.class)))
.thenReturn("pathFromUri");

when(mockImageResizer.resizeImageIfNeeded("pathFromUri", null, null))
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", null, null, null))
.thenReturn("originalPath");
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", WIDTH, HEIGHT))
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", WIDTH, HEIGHT, null))
.thenReturn("scaledPath");
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", WIDTH, null)).thenReturn("scaledPath");
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", null, HEIGHT))
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", WIDTH, null, null))
.thenReturn("scaledPath");
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", null, HEIGHT, null))
.thenReturn("scaledPath");
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", null, null, QUALITY))
.thenReturn("originalPath");
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", WIDTH, null, QUALITY))
.thenReturn("scaledPath");
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", null, HEIGHT, QUALITY))
.thenReturn("scaledPath");
when(mockImageResizer.resizeImageIfNeeded("pathFromUri", WIDTH, HEIGHT, QUALITY))
.thenReturn("scaledPath");

mockFileUriResolver = new MockFileUriResolver();
Expand Down
2 changes: 2 additions & 0 deletions packages/image_picker/example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ buildscript {
repositories {
google()
jcenter()
mavenLocal()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary change

}

dependencies {
Expand All @@ -13,6 +14,7 @@ allprojects {
repositories {
google()
jcenter()
mavenLocal()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary change

}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/image_picker/ios/Classes/ImagePickerPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,16 @@ - (void)imagePickerController:(UIImagePickerController *)picker

NSNumber *maxWidth = [_arguments objectForKey:@"maxWidth"];
NSNumber *maxHeight = [_arguments objectForKey:@"maxHeight"];
NSNumber *resizeQuality = [_arguments objectForKey:@"resizeQuality"];

if (maxWidth != (id)[NSNull null] || maxHeight != (id)[NSNull null]) {
image = [self scaledImage:image maxWidth:maxWidth maxHeight:maxHeight];
}

NSData *data = UIImageJPEGRepresentation(image, 1.0);
if (resizeQuality == (id)[NSNull null]) {
resizeQuality = [NSNumber numberWithFloat:1.0f];
}
NSData *data = UIImageJPEGRepresentation(image, [resizeQuality floatValue]);
NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString];
NSString *tmpFile = [NSString stringWithFormat:@"image_picker_%@.jpg", guid];
NSString *tmpDirectory = NSTemporaryDirectory();
Expand Down
2 changes: 2 additions & 0 deletions packages/image_picker/lib/image_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ImagePicker {
@required ImageSource source,
double maxWidth,
double maxHeight,
double resizeQuality,
}) async {
assert(source != null);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check for value between 0.0 and 1.0

if (resizeQuality != null && (resizeQuality < 0 || resizeQuality > 1.0)) {
  throw ArgumentError.value(resizeQuality, 'resizeQuality must be between 0.0 and 1.0');
}

Expand All @@ -50,6 +51,7 @@ class ImagePicker {
'source': source.index,
'maxWidth': maxWidth,
'maxHeight': maxHeight,
'resizeQuality': resizeQuality,
},
);

Expand Down
50 changes: 50 additions & 0 deletions packages/image_picker/test/image_picker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ void main() {
'source': 0,
'maxWidth': null,
'maxHeight': null,
'resizeQuality': null,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 1,
'maxWidth': null,
'maxHeight': null,
'resizeQuality': null,
}),
],
);
Expand All @@ -59,6 +61,26 @@ void main() {
maxWidth: 10.0,
maxHeight: 20.0,
);
await ImagePicker.pickImage(
source: ImageSource.camera,
resizeQuality: 0.5,
);
await ImagePicker.pickImage(
source: ImageSource.camera,
maxWidth: 10.0,
resizeQuality: 0.5,
);
await ImagePicker.pickImage(
source: ImageSource.camera,
maxHeight: 20.0,
resizeQuality: 0.5,
);
await ImagePicker.pickImage(
source: ImageSource.camera,
maxWidth: 10.0,
maxHeight: 20.0,
resizeQuality: 0.5,
);

expect(
log,
Expand All @@ -67,21 +89,49 @@ void main() {
'source': 0,
'maxWidth': null,
'maxHeight': null,
'resizeQuality': null,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': null,
'resizeQuality': null,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': null,
'maxHeight': 10.0,
'resizeQuality': null,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': 20.0,
'resizeQuality': null,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': null,
'maxHeight': null,
'resizeQuality': 0.5,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': null,
'resizeQuality': 0.5,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': null,
'maxHeight': 20.0,
'resizeQuality': 0.5,
}),
isMethodCall('pickImage', arguments: <String, dynamic>{
'source': 0,
'maxWidth': 10.0,
'maxHeight': 20.0,
'resizeQuality': 0.5,
}),
],
);
Expand Down