11package com.example.flutterimagecompress.core
22
33import android.graphics.BitmapFactory
4+ import android.graphics.Bitmap
45import com.example.flutterimagecompress.FlutterImageCompressPlugin
56import com.example.flutterimagecompress.exif.Exif
67import 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 ) {
0 commit comments