Skip to content

Commit 29e800c

Browse files
authored
Merge pull request fluttercandies#101 from crazecoder/master
A fix for fluttercandies#77
2 parents 3630074 + 8aa6fc5 commit 29e800c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

android/src/main/kotlin/com/example/flutterimagecompress/core/CompressFileHandler.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.flutterimagecompress.core
22

33
import android.graphics.BitmapFactory
4+
import android.graphics.Bitmap
45
import com.example.flutterimagecompress.FlutterImageCompressPlugin
56
import com.example.flutterimagecompress.exif.Exif
67
import com.example.flutterimagecompress.exif.ExifKeeper
@@ -30,6 +31,7 @@ class CompressFileHandler(private val call: MethodCall, result: MethodChannel.Re
3031
val autoCorrectionAngle = args[5] as Boolean
3132
val format = args[6] as Int
3233
val keepExif = args[7] as Boolean
34+
val inSampleSize = args[8] as Int
3335

3436
val exifRotate =
3537
if (autoCorrectionAngle) {
@@ -45,8 +47,12 @@ class CompressFileHandler(private val call: MethodCall, result: MethodChannel.Re
4547
minWidth = minHeight
4648
minHeight = tmp
4749
}
48-
49-
val bitmap = BitmapFactory.decodeFile(file)
50+
val options = BitmapFactory.Options()
51+
options.inJustDecodeBounds = false
52+
options.inPreferredConfig = Bitmap.Config.RGB_565
53+
options.inSampleSize = inSampleSize
54+
options.inDither = true
55+
val bitmap = BitmapFactory.decodeFile(file, options)
5056
val array = bitmap.compress(minWidth, minHeight, quality, rotate + exifRotate, format)
5157

5258
if (keepExif) {
@@ -87,9 +93,15 @@ class CompressFileHandler(private val call: MethodCall, result: MethodChannel.Re
8793
}
8894
val format = args[7] as Int
8995
val keepExif = args[8] as Boolean
96+
val inSampleSize = args[9] as Int
9097

9198
try {
92-
val bitmap = BitmapFactory.decodeFile(file)
99+
val options = BitmapFactory.Options()
100+
options.inJustDecodeBounds = false
101+
options.inPreferredConfig = Bitmap.Config.RGB_565
102+
options.inSampleSize = inSampleSize
103+
options.inDither = true
104+
val bitmap = BitmapFactory.decodeFile(file, options)
93105
val outputStream = File(targetPath).outputStream()
94106
outputStream.use {
95107
if (exifRotate == 270 || exifRotate == 90) {

lib/flutter_image_compress.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class FlutterImageCompress {
7373
String path, {
7474
int minWidth = 1920,
7575
int minHeight = 1080,
76+
int inSampleSize = 1,
7677
int quality = 95,
7778
int rotate = 0,
7879
bool autoCorrectionAngle = true,
@@ -95,6 +96,7 @@ class FlutterImageCompress {
9596
autoCorrectionAngle,
9697
_convertTypeToInt(format),
9798
keepExif,
99+
inSampleSize,
98100
]);
99101
return convertDynamic(result);
100102
}
@@ -105,6 +107,7 @@ class FlutterImageCompress {
105107
String targetPath, {
106108
int minWidth = 1920,
107109
int minHeight = 1080,
110+
int inSampleSize = 1,
108111
int quality = 95,
109112
int rotate = 0,
110113
bool autoCorrectionAngle = true,
@@ -130,6 +133,7 @@ class FlutterImageCompress {
130133
autoCorrectionAngle,
131134
_convertTypeToInt(format),
132135
keepExif,
136+
inSampleSize,
133137
]);
134138

135139
if (result == null) {

0 commit comments

Comments
 (0)