react-native-compressor 1.8.7 → 1.8.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -343,7 +343,7 @@ await clearCache(); // this will clear cache of thumbnails cache directory
|
|
|
343
343
|
|
|
344
344
|
- ###### `quality: number` (default: 0.8)
|
|
345
345
|
|
|
346
|
-
The quality modifier for the `JPEG` file format,
|
|
346
|
+
The quality modifier for the `JPEG` and `PNG` file format, if your input file is `JPEG` and output file is `PNG` then compressed size can be increase
|
|
347
347
|
|
|
348
348
|
- ###### `input: InputType` (default: uri)
|
|
349
349
|
|
package/android/build.gradle
CHANGED
|
@@ -115,7 +115,7 @@ dependencies {
|
|
|
115
115
|
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
|
|
116
116
|
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
|
|
117
117
|
implementation "com.googlecode.mp4parser:isoparser:1.0.6"
|
|
118
|
-
|
|
118
|
+
implementation 'io.github.lizhangqu:coreprogress:1.0.2'
|
|
119
119
|
implementation 'com.github.banketree:AndroidLame-kotlin:v0.0.1'
|
|
120
120
|
implementation 'javazoom:jlayer:1.0.1'
|
|
121
121
|
}
|
|
@@ -85,10 +85,21 @@ object ImageCompressor {
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
fun compress(image: Bitmap?, output: ImageCompressorOptions.OutputType, quality: Float): ByteArrayOutputStream {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
88
|
+
var stream = ByteArrayOutputStream()
|
|
89
|
+
if (output === ImageCompressorOptions.OutputType.jpg)
|
|
90
|
+
{
|
|
91
|
+
image!!.compress(CompressFormat.JPEG, Math.round(100 * quality), stream)
|
|
92
|
+
}
|
|
93
|
+
else
|
|
94
|
+
{
|
|
95
|
+
val pngStream = ByteArrayOutputStream()
|
|
96
|
+
image!!.compress(CompressFormat.JPEG, Math.round(100 * quality), stream)
|
|
97
|
+
val byteArray: ByteArray = stream.toByteArray()
|
|
98
|
+
stream=ByteArrayOutputStream()
|
|
99
|
+
val bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
|
|
100
|
+
bitmap.compress(CompressFormat.PNG, 100, stream)
|
|
101
|
+
}
|
|
102
|
+
return stream
|
|
92
103
|
}
|
|
93
104
|
|
|
94
105
|
fun manualCompressImage(imagePath: String?, options: ImageCompressorOptions, reactContext: ReactApplicationContext?): String? {
|
|
@@ -111,6 +111,9 @@ class ImageCompressor {
|
|
|
111
111
|
data = image.jpegData(compressionQuality: CGFloat(quality))!
|
|
112
112
|
case .png:
|
|
113
113
|
data = image.pngData()!
|
|
114
|
+
data = image.jpegData(compressionQuality: CGFloat(quality))!
|
|
115
|
+
let compressedImage = UIImage(data: data)
|
|
116
|
+
data = compressedImage!.pngData()!
|
|
114
117
|
}
|
|
115
118
|
|
|
116
119
|
if isBase64 {
|
|
@@ -232,11 +235,11 @@ class ImageCompressor {
|
|
|
232
235
|
|
|
233
236
|
var actualHeight = image.size.height
|
|
234
237
|
var actualWidth = image.size.width
|
|
235
|
-
let maxHeight: CGFloat =
|
|
236
|
-
let maxWidth: CGFloat =
|
|
238
|
+
let maxHeight: CGFloat = CGFloat(options.maxHeight)
|
|
239
|
+
let maxWidth: CGFloat = CGFloat(options.maxWidth)
|
|
237
240
|
var imgRatio = actualWidth / actualHeight
|
|
238
241
|
let maxRatio = maxWidth / maxHeight
|
|
239
|
-
let compressionQuality: CGFloat =
|
|
242
|
+
let compressionQuality: CGFloat = CGFloat(options.quality)
|
|
240
243
|
|
|
241
244
|
if actualHeight > maxHeight || actualWidth > maxWidth {
|
|
242
245
|
if imgRatio < maxRatio {
|
|
@@ -257,15 +260,24 @@ class ImageCompressor {
|
|
|
257
260
|
UIGraphicsBeginImageContext(rect.size)
|
|
258
261
|
image.draw(in: rect)
|
|
259
262
|
if let img = UIGraphicsGetImageFromCurrentImageContext() {
|
|
260
|
-
|
|
263
|
+
var imageData: Data
|
|
264
|
+
switch OutputType(rawValue: options.output.rawValue)! {
|
|
265
|
+
case .jpg:
|
|
266
|
+
imageData = img.jpegData(compressionQuality: compressionQuality)!
|
|
267
|
+
case .png:
|
|
268
|
+
imageData = img.jpegData(compressionQuality: compressionQuality)!
|
|
269
|
+
let compressedImage = UIImage(data: imageData)
|
|
270
|
+
imageData = compressedImage!.pngData()!
|
|
271
|
+
}
|
|
272
|
+
|
|
261
273
|
UIGraphicsEndImageContext()
|
|
262
274
|
let isBase64 = options.returnableOutputType == .rbase64
|
|
263
275
|
if isBase64 {
|
|
264
|
-
return imageData
|
|
276
|
+
return imageData.base64EncodedString(options: [])
|
|
265
277
|
} else {
|
|
266
278
|
let filePath = Utils.generateCacheFilePath(outputExtension)
|
|
267
279
|
do {
|
|
268
|
-
try imageData
|
|
280
|
+
try imageData.write(to: URL(fileURLWithPath: filePath), options: .atomic)
|
|
269
281
|
let returnablePath = ImageCompressor.makeValidUri(filePath)
|
|
270
282
|
return returnablePath
|
|
271
283
|
} catch {
|