Skip to content

Commit

Permalink
Fixes scaling on android and added aspect ratio (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruerob committed Jul 11, 2022
1 parent ae01696 commit 32149c2
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ p, q, r, s, t
#### Scale

```dart
ScaleOption(width,height);
ScaleOption(width, height, keepRatio: keepRatio);
```

After specifying the width and height, it is not clipped, but stretched to the specified width and height (Does not maintain the aspect ratio of the image).
After specifying the width and height, it is not clipped, but stretched to the specified width and height (Does maintain the aspect ratio of the image if wanted).

#### AddText

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,29 @@ class ImageHandler(private var bitmap: Bitmap) {
}

private fun handleScale(option: ScaleOption): Bitmap {
val w = min(bitmap.width, option.width)
val h = min(bitmap.height, option.height)

val bitmapRatio = bitmap.width.toFloat() / bitmap.height.toFloat()
val optionRatio = option.width.toFloat() / option.height.toFloat()
var w = option.width
var h = option.height
if (option.keepRatio) {
if (bitmapRatio < optionRatio) {
w = (option.height * bitmapRatio).toInt()
} else if (bitmapRatio > optionRatio) {
h = (option.width / bitmapRatio).toInt()
}
}
val newBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)

val canvas = Canvas(newBitmap)

val p = Paint()

val m = Matrix()

val width: Int = bitmap.width
val height: Int = bitmap.height
if (width != w || height != h) {
val sx: Float = w / width.toFloat()
val sy: Float = h / height.toFloat()
m.setScale(sx, sy)
}

canvas.drawBitmap(bitmap, m, p)

return newBitmap
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package top.kikt.flutter_image_editor.option

data class ScaleOption(val width: Int, val height: Int) : Option
data class ScaleOption(val width: Int, val height: Int, val keepRatio: Boolean) : Option
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ object ConvertUtils {

val w = optionMap["width"] as Int
val h = optionMap["height"] as Int
return ScaleOption(w, h)
val keepRatio = optionMap["keepRatio"] as Boolean
return ScaleOption(w, h, keepRatio)
}

private fun getColorOption(optionMap: Any?): ColorOption {
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/FIConvertUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface FIScaleOption : NSObject <FIOption>
@property(assign, nonatomic) int width;
@property(assign, nonatomic) int height;
@property(assign, nonatomic) BOOL keepRatio;
@end

@interface FIAddText : NSObject <FIOption>
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/FIConvertUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ + (id)createFromDict:(NSDictionary *)dict {
FIScaleOption *option = [FIScaleOption new];
option.width = [dict[@"width"] intValue];
option.height = [dict[@"height"] intValue];
option.keepRatio = [dict[@"keepRatio"] boolValue];
return option;
}

Expand Down
17 changes: 15 additions & 2 deletions ios/Classes/FIUIImageHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,27 @@ - (void)scale:(FIScaleOption *)option {
return;
}

UIGraphicsBeginImageContext(CGSizeMake(option.width, option.height));
float imageRatio = outImage.size.width / outImage.size.height;
float optionRatio = option.width / option.height;
int w = option.width;
int h = option.height;

if (option.keepRatio) {
if (imageRatio < optionRatio) {
w = option.height * imageRatio;
} else if (imageRatio > optionRatio) {
h = option.width / imageRatio;
}
}

UIGraphicsBeginImageContext(CGSizeMake(w, h));

CGContextRef ctx = UIGraphicsGetCurrentContext();
if (!ctx) {
return;
}

[outImage drawInRect:CGRectMake(0, 0, option.width, option.height)];
[outImage drawInRect:CGRectMake(0, 0, w, h)];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

Expand Down
20 changes: 13 additions & 7 deletions lib/src/option/scale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ part of 'edit_options.dart';
class ScaleOption implements Option {
final int width;
final int height;
final bool keepRatio;

ScaleOption(this.width, this.height)
: assert(width > 0),
assert(height > 0);
ScaleOption(
this.width,
this.height, {
this.keepRatio: false,
}) : assert(width > 0 && height > 0);

@override
bool get canIgnore => false;
Expand All @@ -15,8 +18,11 @@ class ScaleOption implements Option {
String get key => 'scale';

@override
Map<String, Object> get transferValue => {
'width': width,
'height': height,
};
Map<String, Object> get transferValue {
return <String, Object>{
'width': width,
'height': height,
'keepRatio': keepRatio,
};
}
}

0 comments on commit 32149c2

Please sign in to comment.