react-native-compressor 1.7.1 → 1.8.0
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 +40 -15
- package/android/build.gradle +8 -0
- package/android/src/main/java/com/reactnativecompressor/Audio/AudioCompressor.kt +527 -0
- package/android/src/main/java/com/reactnativecompressor/Audio/AudioMain.kt +31 -0
- package/android/src/main/java/com/reactnativecompressor/CompressorModule.kt +157 -0
- package/android/src/main/java/com/reactnativecompressor/CompressorPackage.kt +34 -0
- package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressor.kt +199 -0
- package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressorOptions.kt +57 -0
- package/android/src/main/java/com/reactnativecompressor/Image/ImageMain.kt +31 -0
- package/android/src/main/java/com/reactnativecompressor/Image/ImageSize.kt +3 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/Downloader.kt +119 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/EventEmitterHandler.kt +66 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/MediaCache.kt +61 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/RealPathUtil.kt +177 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/Uploader.kt +117 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/Utils.kt +162 -0
- package/android/src/main/java/com/reactnativecompressor/Video/AutoVideoCompression.kt +63 -0
- package/android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.kt +125 -0
- package/android/src/main/java/com/reactnativecompressor/Video/VideoMain.kt +86 -0
- package/android/src/newarch/CompressorSpec.kt +5 -0
- package/android/src/oldarch/CompressorSpec.kt +32 -0
- package/ios/Audio/AudioMain.swift +115 -0
- package/ios/Compressor-Bridging-Header.h +1 -2
- package/ios/Compressor.h +0 -8
- package/ios/Compressor.mm +28 -389
- package/ios/CompressorManager.swift +96 -0
- package/ios/Image/ImageCompressor.swift +354 -0
- package/ios/Image/ImageCompressorOptions.swift +93 -0
- package/ios/Image/ImageMain.swift +27 -0
- package/ios/Utils/Downloader.swift +157 -0
- package/ios/Utils/EventEmitterHandler.swift +54 -0
- package/ios/Utils/MediaCache.swift +48 -0
- package/ios/Utils/Uploader.swift +99 -0
- package/ios/Utils/Utils.swift +110 -0
- package/ios/Video/{VideoCompressor.swift → VideoMain.swift} +168 -147
- package/lib/commonjs/Image/index.js +1 -1
- package/lib/commonjs/Image/index.js.map +1 -1
- package/lib/commonjs/Main.js +1 -8
- package/lib/commonjs/Main.js.map +1 -1
- package/lib/commonjs/Spec/NativeCompressor.js.map +1 -1
- package/lib/commonjs/Video/index.js +4 -33
- package/lib/commonjs/Video/index.js.map +1 -1
- package/lib/commonjs/expo-plugin/compressor.js.map +1 -1
- package/lib/commonjs/index.js +11 -6
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/Downloader.js +38 -0
- package/lib/commonjs/utils/Downloader.js.map +1 -0
- package/lib/commonjs/utils/Uploader.js +40 -0
- package/lib/commonjs/utils/Uploader.js.map +1 -0
- package/lib/commonjs/utils/index.js +35 -0
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/module/Image/index.js +1 -1
- package/lib/module/Image/index.js.map +1 -1
- package/lib/module/Main.js +1 -7
- package/lib/module/Main.js.map +1 -1
- package/lib/module/Spec/NativeCompressor.js.map +1 -1
- package/lib/module/Video/index.js +5 -33
- package/lib/module/Video/index.js.map +1 -1
- package/lib/module/expo-plugin/compressor.js.map +1 -1
- package/lib/module/index.js +6 -6
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/Downloader.js +31 -0
- package/lib/module/utils/Downloader.js.map +1 -0
- package/lib/module/utils/Uploader.js +33 -0
- package/lib/module/utils/Uploader.js.map +1 -0
- package/lib/module/utils/index.js +2 -0
- package/lib/module/utils/index.js.map +1 -1
- package/lib/typescript/Image/index.d.ts +4 -0
- package/lib/typescript/Image/index.d.ts.map +1 -1
- package/lib/typescript/Main.d.ts +1 -2
- package/lib/typescript/Main.d.ts.map +1 -1
- package/lib/typescript/Spec/NativeCompressor.d.ts +8 -2
- package/lib/typescript/Spec/NativeCompressor.d.ts.map +1 -1
- package/lib/typescript/Video/index.d.ts +4 -28
- package/lib/typescript/Video/index.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +5 -4
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/Downloader.d.ts +2 -0
- package/lib/typescript/utils/Downloader.d.ts.map +1 -0
- package/lib/typescript/utils/Uploader.d.ts +28 -0
- package/lib/typescript/utils/Uploader.d.ts.map +1 -0
- package/lib/typescript/utils/index.d.ts +2 -1
- package/lib/typescript/utils/index.d.ts.map +1 -1
- package/package.json +6 -4
- package/src/Image/index.tsx +5 -1
- package/src/Main.tsx +1 -15
- package/src/Spec/NativeCompressor.ts +14 -2
- package/src/Video/index.tsx +11 -84
- package/src/expo-plugin/compressor.ts +1 -1
- package/src/index.tsx +6 -3
- package/src/utils/Downloader.tsx +38 -0
- package/src/utils/Uploader.tsx +78 -0
- package/src/utils/index.tsx +2 -0
- package/android/src/main/java/com/reactnativecompressor/Audio/AudioCompressor.java +0 -605
- package/android/src/main/java/com/reactnativecompressor/CompressorModule.java +0 -220
- package/android/src/main/java/com/reactnativecompressor/CompressorPackage.java +0 -58
- package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressor.java +0 -257
- package/android/src/main/java/com/reactnativecompressor/Image/utils/ImageCompressorOptions.java +0 -69
- package/android/src/main/java/com/reactnativecompressor/Image/utils/ImageSize.java +0 -13
- package/android/src/main/java/com/reactnativecompressor/Utils/FileUplaoder/FileUploadHelper.java +0 -39
- package/android/src/main/java/com/reactnativecompressor/Utils/FileUplaoder/FileUploader.java +0 -129
- package/android/src/main/java/com/reactnativecompressor/Utils/MediaCache.java +0 -65
- package/android/src/main/java/com/reactnativecompressor/Utils/RealPathUtil.java +0 -210
- package/android/src/main/java/com/reactnativecompressor/Utils/Utils.java +0 -272
- package/android/src/main/java/com/reactnativecompressor/Video/AutoVideoCompression/AutoVideoCompression.java +0 -87
- package/android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.java +0 -182
- package/android/src/main/java/com/reactnativecompressor/Video/VideoModule.java +0 -112
- package/android/src/newarch/CompressorSpec.java +0 -9
- package/android/src/newarch/VideoCompressorSpec.java +0 -9
- package/android/src/oldarch/CompressorSpec.java +0 -34
- package/android/src/oldarch/VideoCompressorSpec.java +0 -26
- package/ios/Image/ImageCompressor.h +0 -22
- package/ios/Image/ImageCompressor.m +0 -540
- package/ios/Image/ImageCompressorOptions.h +0 -28
- package/ios/Image/ImageCompressorOptions.m +0 -97
- package/ios/Utils/Downloader.h +0 -16
- package/ios/Utils/Downloader.m +0 -150
- package/ios/Utils/MediaCache.h +0 -11
- package/ios/Utils/MediaCache.m +0 -51
- package/ios/Video/VideoCompressor.h +0 -5
- package/ios/Video/VideoCompressor.m +0 -1
- package/lib/commonjs/Spec/NativeVideoCompressor.js +0 -10
- package/lib/commonjs/Spec/NativeVideoCompressor.js.map +0 -1
- package/lib/module/Spec/NativeVideoCompressor.js +0 -3
- package/lib/module/Spec/NativeVideoCompressor.js.map +0 -1
- package/lib/typescript/Spec/NativeVideoCompressor.d.ts +0 -13
- package/lib/typescript/Spec/NativeVideoCompressor.d.ts.map +0 -1
- package/src/Spec/NativeVideoCompressor.ts +0 -14
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
package com.reactnativecompressor;
|
|
2
|
-
|
|
3
|
-
import androidx.annotation.NonNull;
|
|
4
|
-
|
|
5
|
-
import com.facebook.react.bridge.Promise;
|
|
6
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
7
|
-
import com.facebook.react.bridge.ReactMethod;
|
|
8
|
-
|
|
9
|
-
import androidx.annotation.RequiresApi;
|
|
10
|
-
|
|
11
|
-
import com.facebook.react.bridge.Arguments;
|
|
12
|
-
import com.facebook.react.bridge.ReactContext;
|
|
13
|
-
import com.facebook.react.bridge.ReadableMap;
|
|
14
|
-
import com.facebook.react.bridge.WritableMap;
|
|
15
|
-
import com.reactnativecompressor.Image.ImageCompressor;
|
|
16
|
-
import com.reactnativecompressor.Image.utils.ImageCompressorOptions;
|
|
17
|
-
import com.reactnativecompressor.Utils.MediaCache;
|
|
18
|
-
import com.reactnativecompressor.Utils.Utils;
|
|
19
|
-
import com.reactnativecompressor.Video.VideoCompressorHelper;
|
|
20
|
-
import com.reactnativecompressor.CompressorSpec;
|
|
21
|
-
import static com.reactnativecompressor.Utils.Utils.generateCacheFilePath;
|
|
22
|
-
|
|
23
|
-
import android.content.ContentResolver;
|
|
24
|
-
import android.content.res.AssetFileDescriptor;
|
|
25
|
-
import android.database.Cursor;
|
|
26
|
-
import android.media.MediaMetadataRetriever;
|
|
27
|
-
import android.net.Uri;
|
|
28
|
-
import android.os.Build;
|
|
29
|
-
import android.provider.OpenableColumns;
|
|
30
|
-
import android.util.Log;
|
|
31
|
-
|
|
32
|
-
import com.reactnativecompressor.Audio.AudioCompressor;
|
|
33
|
-
|
|
34
|
-
import java.io.File;
|
|
35
|
-
import java.io.FileNotFoundException;
|
|
36
|
-
|
|
37
|
-
public class CompressorModule extends CompressorSpec {
|
|
38
|
-
public static final String NAME = "Compressor";
|
|
39
|
-
private final ReactApplicationContext reactContext;
|
|
40
|
-
|
|
41
|
-
public CompressorModule(ReactApplicationContext context) {
|
|
42
|
-
super(context);
|
|
43
|
-
this.reactContext = context;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
@Override
|
|
47
|
-
@NonNull
|
|
48
|
-
public String getName() {
|
|
49
|
-
return NAME;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
//Image
|
|
53
|
-
@ReactMethod
|
|
54
|
-
public void image_compress(
|
|
55
|
-
String imagePath,
|
|
56
|
-
ReadableMap optionMap,
|
|
57
|
-
Promise promise) {
|
|
58
|
-
try {
|
|
59
|
-
final ImageCompressorOptions options = ImageCompressorOptions.fromMap(optionMap);
|
|
60
|
-
imagePath=Utils.getRealPath(imagePath,reactContext,options.uuid);
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if(options.compressionMethod==ImageCompressorOptions.CompressionMethod.auto)
|
|
64
|
-
{
|
|
65
|
-
String returnableResult=ImageCompressor.autoCompressImage(imagePath,options,reactContext);
|
|
66
|
-
promise.resolve(returnableResult);
|
|
67
|
-
}
|
|
68
|
-
else
|
|
69
|
-
{
|
|
70
|
-
String returnableResult=ImageCompressor.manualCompressImage(imagePath,options,reactContext);
|
|
71
|
-
promise.resolve(returnableResult);
|
|
72
|
-
}
|
|
73
|
-
MediaCache.removeCompletedImagePath(imagePath);
|
|
74
|
-
} catch (Exception ex) {
|
|
75
|
-
promise.reject(ex);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
@ReactMethod
|
|
80
|
-
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
|
|
81
|
-
public void compress_audio(
|
|
82
|
-
String fileUrl,
|
|
83
|
-
ReadableMap optionMap,
|
|
84
|
-
Promise promise) {
|
|
85
|
-
try{
|
|
86
|
-
final VideoCompressorHelper options = VideoCompressorHelper.fromMap(optionMap);
|
|
87
|
-
Uri uri= Uri.parse(fileUrl);
|
|
88
|
-
String srcPath = uri.getPath();
|
|
89
|
-
String destinationPath = generateCacheFilePath("mp3", reactContext);
|
|
90
|
-
|
|
91
|
-
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
|
|
92
|
-
metaRetriever.setDataSource(srcPath);
|
|
93
|
-
|
|
94
|
-
float bitrate = options.bitrate;
|
|
95
|
-
Log.d("nomi onStart", destinationPath+"onProgress: "+bitrate);
|
|
96
|
-
new AudioCompressor().CompressAudio(srcPath, destinationPath, (int) bitrate*1000);
|
|
97
|
-
|
|
98
|
-
} catch (Exception ex) {
|
|
99
|
-
promise.reject(ex);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
//General
|
|
105
|
-
@ReactMethod
|
|
106
|
-
public void generateFilePath(String extension, Promise promise) {
|
|
107
|
-
try {
|
|
108
|
-
final String outputUri =generateCacheFilePath(extension,reactContext);
|
|
109
|
-
promise.resolve(outputUri);
|
|
110
|
-
} catch (Exception e) {
|
|
111
|
-
promise.reject(e);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
@ReactMethod
|
|
116
|
-
public void getRealPath(String path,String type, Promise promise) {
|
|
117
|
-
try {
|
|
118
|
-
final String realPath =Utils.getRealPath(path,reactContext);
|
|
119
|
-
promise.resolve("file://"+realPath);
|
|
120
|
-
} catch (Exception e) {
|
|
121
|
-
promise.reject(e);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
@ReactMethod
|
|
126
|
-
public void getVideoMetaData(String filePath, Promise promise) {
|
|
127
|
-
try {
|
|
128
|
-
filePath=Utils.getRealPath(filePath,reactContext);
|
|
129
|
-
Uri uri= Uri.parse(filePath);
|
|
130
|
-
String srcPath = uri.getPath();
|
|
131
|
-
MediaMetadataRetriever metaRetriever = new MediaMetadataRetriever();
|
|
132
|
-
metaRetriever.setDataSource(srcPath);
|
|
133
|
-
File file=new File(srcPath);
|
|
134
|
-
float sizeInKBs = file.length()/1024;
|
|
135
|
-
int actualHeight =Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
|
|
136
|
-
int actualWidth = Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
|
|
137
|
-
long duration = Long.parseLong(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
|
|
138
|
-
String creationTime = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
|
|
139
|
-
String extension = filePath.substring(filePath.lastIndexOf(".")+1);
|
|
140
|
-
|
|
141
|
-
WritableMap params = Arguments.createMap();
|
|
142
|
-
params.putString("size", String.valueOf(sizeInKBs));
|
|
143
|
-
params.putString("width", String.valueOf(actualWidth));
|
|
144
|
-
params.putString("height", String.valueOf(actualHeight));
|
|
145
|
-
params.putString("duration", String.valueOf(duration/1000));
|
|
146
|
-
params.putString("extension", extension);
|
|
147
|
-
params.putString("creationTime", String.valueOf(creationTime));
|
|
148
|
-
|
|
149
|
-
promise.resolve(params);
|
|
150
|
-
} catch (Exception e) {
|
|
151
|
-
promise.reject(e);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
@ReactMethod
|
|
155
|
-
public void getFileSize(String filePath, Promise promise)
|
|
156
|
-
{
|
|
157
|
-
if(filePath.startsWith("http://")||filePath.startsWith("https://"))
|
|
158
|
-
{
|
|
159
|
-
promise.resolve(Utils.getFileSizeFromURL(filePath));
|
|
160
|
-
}
|
|
161
|
-
else
|
|
162
|
-
{
|
|
163
|
-
filePath=Utils.getRealPath(filePath,reactContext);
|
|
164
|
-
Uri uri= Uri.parse(filePath);
|
|
165
|
-
ContentResolver contentResolver = reactContext.getContentResolver();
|
|
166
|
-
long fileSize = getLength(uri, contentResolver);
|
|
167
|
-
if (fileSize >= 0) {
|
|
168
|
-
promise.resolve(String.valueOf(fileSize));
|
|
169
|
-
} else {
|
|
170
|
-
promise.resolve("");
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
@ReactMethod
|
|
176
|
-
public void addListener(String eventName) {
|
|
177
|
-
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
@ReactMethod
|
|
181
|
-
public void removeListeners(double count) {
|
|
182
|
-
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
public static long getLength(Uri uri, ContentResolver contentResolver) {
|
|
186
|
-
AssetFileDescriptor assetFileDescriptor = null;
|
|
187
|
-
try {
|
|
188
|
-
assetFileDescriptor = contentResolver.openAssetFileDescriptor(uri, "r");
|
|
189
|
-
} catch (FileNotFoundException e) {
|
|
190
|
-
// Do nothing
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
long length = (assetFileDescriptor != null) ? assetFileDescriptor.getLength() : -1L;
|
|
194
|
-
if (length != -1L) {
|
|
195
|
-
return length;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
|
|
199
|
-
Cursor cursor = contentResolver.query(uri, new String[]{OpenableColumns.SIZE}, null, null, null);
|
|
200
|
-
if (cursor != null) {
|
|
201
|
-
try {
|
|
202
|
-
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
|
|
203
|
-
if (sizeIndex != -1 && cursor.moveToFirst()) {
|
|
204
|
-
try {
|
|
205
|
-
return cursor.getLong(sizeIndex);
|
|
206
|
-
} catch (Throwable ignored) {
|
|
207
|
-
return -1L;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
} finally {
|
|
211
|
-
cursor.close();
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return -1L;
|
|
215
|
-
} else {
|
|
216
|
-
return -1L;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
package com.reactnativecompressor;
|
|
2
|
-
|
|
3
|
-
import androidx.annotation.Nullable;
|
|
4
|
-
|
|
5
|
-
import com.facebook.react.bridge.NativeModule;
|
|
6
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
7
|
-
import com.facebook.react.module.model.ReactModuleInfo;
|
|
8
|
-
import com.facebook.react.module.model.ReactModuleInfoProvider;
|
|
9
|
-
import com.facebook.react.TurboReactPackage;
|
|
10
|
-
import java.util.HashMap;
|
|
11
|
-
import java.util.Map;
|
|
12
|
-
import com.reactnativecompressor.Video.VideoModule;
|
|
13
|
-
|
|
14
|
-
public class CompressorPackage extends TurboReactPackage {
|
|
15
|
-
|
|
16
|
-
@Nullable
|
|
17
|
-
@Override
|
|
18
|
-
public NativeModule getModule(String name, ReactApplicationContext reactContext) {
|
|
19
|
-
if (name.equals(CompressorModule.NAME)) {
|
|
20
|
-
return new CompressorModule(reactContext);
|
|
21
|
-
} else if (name.equals(VideoModule.NAME)) {
|
|
22
|
-
return new VideoModule(reactContext);
|
|
23
|
-
} else {
|
|
24
|
-
return null;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
@Override
|
|
29
|
-
public ReactModuleInfoProvider getReactModuleInfoProvider() {
|
|
30
|
-
return () -> {
|
|
31
|
-
final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
|
|
32
|
-
boolean isTurboModule = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;
|
|
33
|
-
moduleInfos.put(
|
|
34
|
-
CompressorModule.NAME,
|
|
35
|
-
new ReactModuleInfo(
|
|
36
|
-
CompressorModule.NAME,
|
|
37
|
-
CompressorModule.NAME,
|
|
38
|
-
false, // canOverrideExistingModule
|
|
39
|
-
false, // needsEagerInit
|
|
40
|
-
true, // hasConstants
|
|
41
|
-
false, // isCxxModule
|
|
42
|
-
isTurboModule // isTurboModule
|
|
43
|
-
));
|
|
44
|
-
moduleInfos.put(
|
|
45
|
-
VideoModule.NAME,
|
|
46
|
-
new ReactModuleInfo(
|
|
47
|
-
VideoModule.NAME,
|
|
48
|
-
VideoModule.NAME,
|
|
49
|
-
false, // canOverrideExistingModule
|
|
50
|
-
false, // needsEagerInit
|
|
51
|
-
true, // hasConstants
|
|
52
|
-
false, // isCxxModule
|
|
53
|
-
isTurboModule // isTurboModule
|
|
54
|
-
));
|
|
55
|
-
return moduleInfos;
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
}
|
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
package com.reactnativecompressor.Image;
|
|
2
|
-
|
|
3
|
-
import android.graphics.Bitmap;
|
|
4
|
-
import android.graphics.BitmapFactory;
|
|
5
|
-
import android.graphics.Canvas;
|
|
6
|
-
import android.graphics.Matrix;
|
|
7
|
-
import android.graphics.Paint;
|
|
8
|
-
import android.media.ExifInterface;
|
|
9
|
-
import android.net.Uri;
|
|
10
|
-
import android.util.Base64;
|
|
11
|
-
|
|
12
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
13
|
-
import com.reactnativecompressor.Image.utils.ImageCompressorOptions;
|
|
14
|
-
import com.reactnativecompressor.Image.utils.ImageSize;
|
|
15
|
-
|
|
16
|
-
import java.io.ByteArrayOutputStream;
|
|
17
|
-
import java.io.File;
|
|
18
|
-
import java.io.FileNotFoundException;
|
|
19
|
-
import java.io.FileOutputStream;
|
|
20
|
-
import java.io.IOException;
|
|
21
|
-
import java.net.MalformedURLException;
|
|
22
|
-
|
|
23
|
-
import static com.reactnativecompressor.Utils.Utils.generateCacheFilePath;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
public class ImageCompressor {
|
|
27
|
-
|
|
28
|
-
public static String getRNFileUrl(String filePath) {
|
|
29
|
-
File returnAbleFile= new File(filePath);
|
|
30
|
-
try {
|
|
31
|
-
filePath = returnAbleFile.toURL().toString();
|
|
32
|
-
} catch (MalformedURLException e) {
|
|
33
|
-
e.printStackTrace();
|
|
34
|
-
}
|
|
35
|
-
return filePath;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public static ImageSize findActualSize(Bitmap image, int maxWidth, int maxHeight) {
|
|
39
|
-
final float width = (float) image.getWidth();
|
|
40
|
-
final float height = (float) image.getHeight();
|
|
41
|
-
|
|
42
|
-
if (width > height) {
|
|
43
|
-
final int newHeight = Math.round(height / (width / maxWidth));
|
|
44
|
-
final float scale = newHeight / height;
|
|
45
|
-
|
|
46
|
-
return new ImageSize(maxWidth, newHeight, scale);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
final int newWidth = Math.round(width / (height / maxHeight));
|
|
50
|
-
final float scale = newWidth / width;
|
|
51
|
-
|
|
52
|
-
return new ImageSize(newWidth, maxHeight, scale);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public static Bitmap decodeImage(String value) {
|
|
56
|
-
final byte[] data = Base64.decode(value, Base64.DEFAULT);
|
|
57
|
-
return BitmapFactory.decodeByteArray(data, 0, data.length);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public static Bitmap loadImage(String value) {
|
|
61
|
-
Uri uri= Uri.parse(value);
|
|
62
|
-
String filePath = uri.getPath();
|
|
63
|
-
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
|
|
64
|
-
return bitmap;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
public static String encodeImage(ByteArrayOutputStream imageDataByteArrayOutputStream, Boolean isBase64,String outputExtension, ReactApplicationContext reactContext) {
|
|
70
|
-
if(isBase64)
|
|
71
|
-
{
|
|
72
|
-
byte[] imageData=imageDataByteArrayOutputStream.toByteArray();
|
|
73
|
-
return Base64.encodeToString(imageData, Base64.DEFAULT);
|
|
74
|
-
}
|
|
75
|
-
else
|
|
76
|
-
{
|
|
77
|
-
String outputUri=generateCacheFilePath(outputExtension,reactContext);
|
|
78
|
-
try {
|
|
79
|
-
FileOutputStream fos=new FileOutputStream(outputUri);
|
|
80
|
-
imageDataByteArrayOutputStream.writeTo(fos);
|
|
81
|
-
return getRNFileUrl(outputUri);
|
|
82
|
-
} catch (Exception e) {
|
|
83
|
-
e.printStackTrace();
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return "";
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
|
|
90
|
-
final ImageSize size = findActualSize(image, maxWidth, maxHeight);
|
|
91
|
-
|
|
92
|
-
final Bitmap scaledImage = Bitmap.createBitmap(size.width, size.height, image.getConfig());
|
|
93
|
-
final Matrix scaleMatrix = new Matrix();
|
|
94
|
-
final Canvas canvas = new Canvas(scaledImage);
|
|
95
|
-
final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
|
|
96
|
-
|
|
97
|
-
scaleMatrix.setScale(size.scale, size.scale, 0 ,0);
|
|
98
|
-
|
|
99
|
-
paint.setDither(true);
|
|
100
|
-
paint.setAntiAlias(true);
|
|
101
|
-
paint.setFilterBitmap(true);
|
|
102
|
-
|
|
103
|
-
canvas.drawBitmap(image, scaleMatrix, paint);
|
|
104
|
-
|
|
105
|
-
return scaledImage;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
public static ByteArrayOutputStream compress(Bitmap image, ImageCompressorOptions.OutputType output, float quality) {
|
|
109
|
-
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
110
|
-
final Bitmap.CompressFormat format = output == ImageCompressorOptions.OutputType.jpg
|
|
111
|
-
? Bitmap.CompressFormat.JPEG
|
|
112
|
-
: Bitmap.CompressFormat.PNG;
|
|
113
|
-
|
|
114
|
-
image.compress(format, Math.round(100 * quality), stream);
|
|
115
|
-
return stream;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public static String manualCompressImage(String imagePath,ImageCompressorOptions options, ReactApplicationContext reactContext) {
|
|
119
|
-
final Bitmap image = options.input == ImageCompressorOptions.InputType.base64
|
|
120
|
-
? ImageCompressor.decodeImage(imagePath)
|
|
121
|
-
: ImageCompressor.loadImage(imagePath);
|
|
122
|
-
|
|
123
|
-
final Bitmap resizedImage = ImageCompressor.resize(image, options.maxWidth, options.maxHeight);
|
|
124
|
-
final ByteArrayOutputStream imageDataByteArrayOutputStream = ImageCompressor.compress(resizedImage, options.output, options.quality);
|
|
125
|
-
Boolean isBase64=options.returnableOutputType==ImageCompressorOptions.ReturnableOutputType.base64;
|
|
126
|
-
|
|
127
|
-
String returnableResult = ImageCompressor.encodeImage(imageDataByteArrayOutputStream,isBase64,options.output.toString(),reactContext);
|
|
128
|
-
return returnableResult;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
public static String autoCompressImage(String imagePath,ImageCompressorOptions compressorOptions, ReactApplicationContext reactContext) {
|
|
133
|
-
String outputExtension=compressorOptions.output.toString();
|
|
134
|
-
float autoCompressMaxHeight = compressorOptions.maxHeight;
|
|
135
|
-
float autoCompressMaxWidth = compressorOptions.maxWidth;
|
|
136
|
-
Boolean isBase64=compressorOptions.returnableOutputType==ImageCompressorOptions.ReturnableOutputType.base64;
|
|
137
|
-
|
|
138
|
-
Uri uri= Uri.parse(imagePath);
|
|
139
|
-
imagePath = uri.getPath();
|
|
140
|
-
Bitmap scaledBitmap = null;
|
|
141
|
-
|
|
142
|
-
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
143
|
-
options.inJustDecodeBounds = true;
|
|
144
|
-
Bitmap bmp = BitmapFactory.decodeFile(imagePath, options);
|
|
145
|
-
|
|
146
|
-
int actualHeight = options.outHeight;
|
|
147
|
-
int actualWidth = options.outWidth;
|
|
148
|
-
|
|
149
|
-
float imgRatio = (float) actualWidth / (float) actualHeight;
|
|
150
|
-
float maxRatio = autoCompressMaxWidth / autoCompressMaxHeight;
|
|
151
|
-
|
|
152
|
-
if (actualHeight > autoCompressMaxHeight || actualWidth > autoCompressMaxWidth) {
|
|
153
|
-
if (imgRatio < maxRatio) {
|
|
154
|
-
imgRatio = autoCompressMaxHeight / actualHeight;
|
|
155
|
-
actualWidth = (int) (imgRatio * actualWidth);
|
|
156
|
-
actualHeight = (int) autoCompressMaxHeight;
|
|
157
|
-
} else if (imgRatio > maxRatio) {
|
|
158
|
-
imgRatio = autoCompressMaxWidth / actualWidth;
|
|
159
|
-
actualHeight = (int) (imgRatio * actualHeight);
|
|
160
|
-
actualWidth = (int) autoCompressMaxWidth;
|
|
161
|
-
} else {
|
|
162
|
-
actualHeight = (int) autoCompressMaxHeight;
|
|
163
|
-
actualWidth = (int) autoCompressMaxWidth;
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
|
|
169
|
-
options.inJustDecodeBounds = false;
|
|
170
|
-
options.inDither = false;
|
|
171
|
-
options.inPurgeable = true;
|
|
172
|
-
options.inInputShareable = true;
|
|
173
|
-
options.inTempStorage = new byte[16 * 1024];
|
|
174
|
-
|
|
175
|
-
try {
|
|
176
|
-
bmp = BitmapFactory.decodeFile(imagePath, options);
|
|
177
|
-
} catch (OutOfMemoryError exception) {
|
|
178
|
-
exception.printStackTrace();
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
try {
|
|
182
|
-
scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.RGB_565);
|
|
183
|
-
} catch (OutOfMemoryError exception) {
|
|
184
|
-
exception.printStackTrace();
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
float ratioX = actualWidth / (float) options.outWidth;
|
|
188
|
-
float ratioY = actualHeight / (float) options.outHeight;
|
|
189
|
-
float middleX = actualWidth / 2.0f;
|
|
190
|
-
float middleY = actualHeight / 2.0f;
|
|
191
|
-
|
|
192
|
-
Matrix scaleMatrix = new Matrix();
|
|
193
|
-
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
|
|
194
|
-
|
|
195
|
-
Canvas canvas = new Canvas(scaledBitmap);
|
|
196
|
-
canvas.setMatrix(scaleMatrix);
|
|
197
|
-
canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
|
|
198
|
-
|
|
199
|
-
if(bmp!=null)
|
|
200
|
-
{
|
|
201
|
-
bmp.recycle();
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
scaledBitmap = correctImageOrientation(scaledBitmap, imagePath);
|
|
205
|
-
|
|
206
|
-
final ByteArrayOutputStream imageDataByteArrayOutputStream = ImageCompressor.compress(scaledBitmap, compressorOptions.output, compressorOptions.quality);
|
|
207
|
-
String returnableResult = ImageCompressor.encodeImage(imageDataByteArrayOutputStream,isBase64,compressorOptions.output.toString(),reactContext);
|
|
208
|
-
return returnableResult;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
|
|
212
|
-
final int height = options.outHeight;
|
|
213
|
-
final int width = options.outWidth;
|
|
214
|
-
int inSampleSize = 1;
|
|
215
|
-
|
|
216
|
-
if (height > reqHeight || width > reqWidth) {
|
|
217
|
-
final int heightRatio = Math.round((float) height / (float) reqHeight);
|
|
218
|
-
final int widthRatio = Math.round((float) width / (float) reqWidth);
|
|
219
|
-
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
|
|
220
|
-
}
|
|
221
|
-
final float totalPixels = width * height;
|
|
222
|
-
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
|
|
223
|
-
|
|
224
|
-
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
|
|
225
|
-
inSampleSize++;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
return inSampleSize;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
public static Bitmap correctImageOrientation(Bitmap bitmap, String imagePath) {
|
|
232
|
-
try {
|
|
233
|
-
ExifInterface exif = new ExifInterface(imagePath);
|
|
234
|
-
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
|
|
235
|
-
Matrix matrix = new Matrix();
|
|
236
|
-
|
|
237
|
-
switch (orientation) {
|
|
238
|
-
case ExifInterface.ORIENTATION_ROTATE_90:
|
|
239
|
-
matrix.postRotate(90);
|
|
240
|
-
break;
|
|
241
|
-
case ExifInterface.ORIENTATION_ROTATE_180:
|
|
242
|
-
matrix.postRotate(180);
|
|
243
|
-
break;
|
|
244
|
-
case ExifInterface.ORIENTATION_ROTATE_270:
|
|
245
|
-
matrix.postRotate(270);
|
|
246
|
-
break;
|
|
247
|
-
default:
|
|
248
|
-
return bitmap; // No rotation needed
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
|
|
252
|
-
} catch (IOException e) {
|
|
253
|
-
e.printStackTrace();
|
|
254
|
-
return bitmap; // Return original bitmap if an error occurs
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
package/android/src/main/java/com/reactnativecompressor/Image/utils/ImageCompressorOptions.java
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
package com.reactnativecompressor.Image.utils;
|
|
2
|
-
|
|
3
|
-
import com.facebook.react.bridge.ReadableMap;
|
|
4
|
-
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
|
5
|
-
|
|
6
|
-
public class ImageCompressorOptions {
|
|
7
|
-
public static ImageCompressorOptions fromMap(ReadableMap map) {
|
|
8
|
-
final ImageCompressorOptions options = new ImageCompressorOptions();
|
|
9
|
-
final ReadableMapKeySetIterator iterator = map.keySetIterator();
|
|
10
|
-
|
|
11
|
-
while (iterator.hasNextKey()) {
|
|
12
|
-
final String key = iterator.nextKey();
|
|
13
|
-
|
|
14
|
-
switch (key) {
|
|
15
|
-
case "compressionMethod":
|
|
16
|
-
options.compressionMethod = CompressionMethod.valueOf(map.getString(key));
|
|
17
|
-
break;
|
|
18
|
-
case "maxWidth":
|
|
19
|
-
options.maxWidth = map.getInt(key);
|
|
20
|
-
break;
|
|
21
|
-
case "maxHeight":
|
|
22
|
-
options.maxHeight = map.getInt(key);
|
|
23
|
-
break;
|
|
24
|
-
case "quality":
|
|
25
|
-
options.quality = (float) map.getDouble(key);
|
|
26
|
-
break;
|
|
27
|
-
case "input":
|
|
28
|
-
options.input = InputType.valueOf(map.getString(key));
|
|
29
|
-
break;
|
|
30
|
-
case "output":
|
|
31
|
-
options.output = OutputType.valueOf(map.getString(key));
|
|
32
|
-
break;
|
|
33
|
-
case "returnableOutputType":
|
|
34
|
-
options.returnableOutputType = ReturnableOutputType.valueOf(map.getString(key));
|
|
35
|
-
break;
|
|
36
|
-
case "uuid":
|
|
37
|
-
options.uuid = map.getString(key);
|
|
38
|
-
break;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return options;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public enum InputType {
|
|
46
|
-
base64, uri
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
public enum OutputType {
|
|
50
|
-
png, jpg
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public enum ReturnableOutputType {
|
|
54
|
-
base64, uri
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public enum CompressionMethod {
|
|
58
|
-
auto, manual
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public CompressionMethod compressionMethod = CompressionMethod.auto;
|
|
62
|
-
public int maxWidth = 1280;
|
|
63
|
-
public int maxHeight = 1280;
|
|
64
|
-
public float quality = 0.8f;
|
|
65
|
-
public InputType input = InputType.uri;
|
|
66
|
-
public OutputType output = OutputType.jpg;
|
|
67
|
-
public String uuid = "";
|
|
68
|
-
public ReturnableOutputType returnableOutputType = ReturnableOutputType.uri;
|
|
69
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
package com.reactnativecompressor.Image.utils;
|
|
2
|
-
|
|
3
|
-
public class ImageSize {
|
|
4
|
-
public int width;
|
|
5
|
-
public int height;
|
|
6
|
-
public float scale;
|
|
7
|
-
|
|
8
|
-
public ImageSize(int width, int height, float scale) {
|
|
9
|
-
this.width = width;
|
|
10
|
-
this.height = height;
|
|
11
|
-
this.scale = scale;
|
|
12
|
-
}
|
|
13
|
-
}
|
package/android/src/main/java/com/reactnativecompressor/Utils/FileUplaoder/FileUploadHelper.java
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
package com.reactnativecompressor.Utils.FileUplaoder;
|
|
2
|
-
|
|
3
|
-
import com.facebook.react.bridge.ReadableMap;
|
|
4
|
-
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
|
5
|
-
|
|
6
|
-
public class FileUploadHelper {
|
|
7
|
-
public static FileUploadHelper fromMap(ReadableMap map) {
|
|
8
|
-
final FileUploadHelper options = new FileUploadHelper();
|
|
9
|
-
final ReadableMapKeySetIterator iterator = map.keySetIterator();
|
|
10
|
-
|
|
11
|
-
while (iterator.hasNextKey()) {
|
|
12
|
-
final String key = iterator.nextKey();
|
|
13
|
-
|
|
14
|
-
switch (key) {
|
|
15
|
-
case "uuid":
|
|
16
|
-
options.uuid = map.getString(key);
|
|
17
|
-
break;
|
|
18
|
-
|
|
19
|
-
case "method":
|
|
20
|
-
options.method = map.getString(key);
|
|
21
|
-
break;
|
|
22
|
-
|
|
23
|
-
case "headers":
|
|
24
|
-
options.headers = map.getMap(key);
|
|
25
|
-
break;
|
|
26
|
-
|
|
27
|
-
case "url":
|
|
28
|
-
options.url = map.getString(key);
|
|
29
|
-
break;
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return options;
|
|
34
|
-
}
|
|
35
|
-
public String uuid;
|
|
36
|
-
public String method;
|
|
37
|
-
public ReadableMap headers;
|
|
38
|
-
public String url;
|
|
39
|
-
}
|