@@ -133,11 +133,20 @@ object ImageCompressor {
133133 }
134134
135135 fun manualCompressImage (imagePath : String? , options : ImageCompressorOptions , reactContext : ReactApplicationContext ? ): String? {
136- val image = if (options.input == = ImageCompressorOptions .InputType .base64) decodeImage(imagePath) else loadImage(imagePath)
137- val resizedImage = resize(image, options.maxWidth, options.maxHeight)
138- val imageDataByteArrayOutputStream = compress(resizedImage, options.output, options.quality,options.disablePngTransparency)
139- val isBase64 = options.returnableOutputType == = ImageCompressorOptions .ReturnableOutputType .base64
140- return encodeImage(imageDataByteArrayOutputStream, isBase64, options.output.toString(),imagePath, reactContext)
136+ val image = if (options.input == = ImageCompressorOptions .InputType .base64) decodeImage(imagePath) else loadImage(imagePath)
137+ val resizedImage = resize(image, options.maxWidth, options.maxHeight)
138+ val isBase64 = options.returnableOutputType == = ImageCompressorOptions .ReturnableOutputType .base64
139+ val uri = Uri .parse(imagePath)
140+ val imagePathNew = uri.path
141+ var scaledBitmap: Bitmap ? = correctImageOrientation(resizedImage, imagePathNew)
142+ val imageDataByteArrayOutputStream = compress(scaledBitmap, options.output, options.quality, options.disablePngTransparency)
143+ val compressedImagePath = encodeImage(imageDataByteArrayOutputStream, isBase64, options.output.toString(), imagePath, reactContext)
144+ if (isCompressedSizeLessThanActualFile(imagePath!! , compressedImagePath)) {
145+ return compressedImagePath
146+ } else {
147+ MediaCache .deleteFile(compressedImagePath!! )
148+ return slashifyFilePath(imagePath)
149+ }
141150 }
142151
143152 fun isCompressedSizeLessThanActualFile (sourceFileUrl : String ,compressedFileUrl : String? ): Boolean {
@@ -249,20 +258,45 @@ object ImageCompressor {
249258 }
250259
251260 fun correctImageOrientation (bitmap : Bitmap ? , imagePath : String? ): Bitmap ? {
261+ if (bitmap == null || imagePath == null ) return bitmap
262+
252263 return try {
253- val exif = ExifInterface (imagePath!! )
264+ val exif = ExifInterface (imagePath)
254265 val orientation = exif.getAttributeInt(ExifInterface .TAG_ORIENTATION , ExifInterface .ORIENTATION_NORMAL )
255266 val matrix = Matrix ()
267+
256268 when (orientation) {
257- ExifInterface .ORIENTATION_ROTATE_90 -> matrix.postRotate(90f )
258- ExifInterface .ORIENTATION_ROTATE_180 -> matrix.postRotate(180f )
259- ExifInterface .ORIENTATION_ROTATE_270 -> matrix.postRotate(270f )
260- else -> return bitmap // No rotation needed
269+ ExifInterface .ORIENTATION_NORMAL -> return bitmap
270+ ExifInterface .ORIENTATION_FLIP_HORIZONTAL -> {
271+ matrix.setScale(- 1f , 1f )
272+ }
273+ ExifInterface .ORIENTATION_ROTATE_180 -> {
274+ matrix.setRotate(180f )
275+ }
276+ ExifInterface .ORIENTATION_FLIP_VERTICAL -> {
277+ matrix.setScale(1f , - 1f )
278+ }
279+ ExifInterface .ORIENTATION_TRANSPOSE -> {
280+ matrix.setRotate(90f )
281+ matrix.postScale(- 1f , 1f )
282+ }
283+ ExifInterface .ORIENTATION_ROTATE_90 -> {
284+ matrix.setRotate(90f )
285+ }
286+ ExifInterface .ORIENTATION_TRANSVERSE -> {
287+ matrix.setRotate(- 90f )
288+ matrix.postScale(- 1f , 1f )
289+ }
290+ ExifInterface .ORIENTATION_ROTATE_270 -> {
291+ matrix.setRotate(- 90f )
292+ }
293+ else -> return bitmap
261294 }
262- Bitmap .createBitmap(bitmap!! , 0 , 0 , bitmap.width, bitmap.height, matrix, true )
295+
296+ Bitmap .createBitmap(bitmap, 0 , 0 , bitmap.width, bitmap.height, matrix, true )
263297 } catch (e: IOException ) {
264298 e.printStackTrace()
265- bitmap // Return original bitmap if an error occurs
299+ bitmap
266300 }
267301 }
268302}
0 commit comments