├── .gitignore
├── android
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── strings.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── react_native_compress_image
│ │ │ ├── CompressImagePackage.java
│ │ │ ├── UtilFunction.java
│ │ │ └── CompressImageModule.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── react_native_compress_image
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── react_native_compress_image
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
├── build.gradle
└── react-native-compress-image.iml
├── .idea
├── misc.xml
├── vcs.xml
├── encodings.xml
├── modules.xml
├── react-native-compress-images.iml
└── workspace.xml
├── package.json
├── index.js
├── readme.md
└── EN
└── readme-EN.md
/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/android/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/android/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | react-native-Compress-Image
3 |
4 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/android/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/react-native-compress-images.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/android/src/test/java/com/example/react_native_compress_image/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.example.react_native_compress_image;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-native-compress-images",
3 | "version": "0.2.2",
4 | "description": "react-native图片压缩组件",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "echo \"Error: no test specified\" && exit 1"
8 | },
9 | "repository": {
10 | "type": "git",
11 | "url": "git+https://github.com/IMMC/react-native-compress-images.git"
12 | },
13 | "author": "cjp",
14 | "license": "ISC",
15 | "bugs": {
16 | "url": "https://github.com/IMMC/react-native-compress-images/issues"
17 | },
18 | "dependencies": {
19 | "prop-types": "^15.6.0"
20 | },
21 | "homepage": "https://github.com/IMMC/react-native-compress-images#readme"
22 | }
23 |
--------------------------------------------------------------------------------
/android/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\Admin\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/android/src/androidTest/java/com/example/react_native_compress_image/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.react_native_compress_image;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.example.react_native_compress_image.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/android/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 26
5 | buildToolsVersion "26.0.2"
6 |
7 | defaultConfig {
8 | minSdkVersion 16
9 | targetSdkVersion 22
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 |
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | compile fileTree(dir: 'libs', include: ['*.jar'])
25 | compile "com.facebook.react:react-native:0.19.+"
26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
27 | exclude group: 'com.android.support', module: 'support-annotations'
28 | })
29 | testCompile 'junit:junit:4.12'
30 | compile 'top.zibin:Luban:1.1.3'
31 | }
32 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | ** author: cjp
3 | ** time: 2018/1/15
4 | **/
5 | import {NativeModules} from 'react-native';
6 | import PropTypes from 'prop-types';
7 | const optionsTypes = {
8 | maxWidth: PropTypes.number,
9 | maxHeight: PropTypes.number,
10 | quality: PropTypes.number,
11 | urlList: PropTypes.array,
12 | saveImages: PropTypes.bool,
13 | resultBase64: PropTypes.bool
14 | };
15 | const secondOptions = {
16 | urlList: PropTypes.array
17 | };
18 | // const CompressImage = NativeModules.CompressImage;
19 | export default function CompressImages(options) {
20 | if (options.type === 'quality') {
21 | PropTypes.checkPropTypes(secondOptions, options, 'props', 'react-native-compress-images');
22 | return NativeModules.CompressImages.compressQuality(options);
23 | } else {
24 | PropTypes.checkPropTypes(optionsTypes, options, 'prop', 'react-native-compress-images');
25 | return NativeModules.CompressImages.compressSize(options);
26 | }
27 |
28 | };
--------------------------------------------------------------------------------
/android/src/main/java/com/example/react_native_compress_image/CompressImagePackage.java:
--------------------------------------------------------------------------------
1 | package com.example.react_native_compress_image;
2 |
3 | import com.facebook.react.ReactPackage;
4 | import com.facebook.react.bridge.JavaScriptModule;
5 | import com.facebook.react.bridge.NativeModule;
6 | import com.facebook.react.bridge.ReactApplicationContext;
7 | import com.facebook.react.uimanager.ViewManager;
8 |
9 | import java.util.ArrayList;
10 | import java.util.Collections;
11 | import java.util.List;
12 |
13 | /**
14 | * Created by Admin on 2018/1/15.
15 | */
16 |
17 | public class CompressImagePackage implements ReactPackage{
18 |
19 | @Override
20 | public List createNativeModules(ReactApplicationContext reactContext) {
21 | List module = new ArrayList<>();
22 | module.add(new CompressImageModule(reactContext));
23 | return module;
24 | }
25 |
26 | @Override
27 | public List> createJSModules() {
28 | return Collections.emptyList();
29 | }
30 |
31 | @Override
32 | public List createViewManagers(ReactApplicationContext reactContext) {
33 | return Collections.emptyList();
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # react native compress images
2 |  
3 |
4 | ----------
5 | ( [English](https://github.com/IMMC/react-native-compress-images/blob/master/EN/readme-EN.md))
6 | react-native图片压缩组件。(目前仅支持安卓平台)
7 |
8 | ## 导入
9 |
10 | yarn add react-native-compress-images
11 | ## 自动连接
12 | react-native link
13 | ## 使用
14 | 1, import CompressImages from 'react-native-compress-images';
15 |
16 | 2,
17 | ```
18 | CompressImages({
19 | urlList: ['storage/emulated/0/Pictures/image-b1ae0ff3-c014-4e37-b510-f00d6ec88b08.jpg']
20 | }).then((res) => {
21 | console.log(res);
22 | // base64
23 | console.log("data:image/jpeg;base64,"+data.base64List[0]);
24 | })
25 | ```
26 | ## Type
27 | 现在支持两种形式的图片压缩
28 | ##### type = 'compressSzie'
29 | 这种方式会压缩图片的质量和尺寸。可以把图片大小压缩到非常小。如果你对图片细节,清晰度要求不高,可以选择这种压缩形式。
30 | ##### type = 'quality'
31 | 压缩图片质量。可以保证图片清晰,并按比例压缩。压缩后图片大小相对于第一种方式会大一些。对图片压缩后质量要求较高可以选择改方式.
32 | (支持库: [Luban](https://github.com/Curzibn/Luban))
33 | ## Options
34 |
35 | | option | Info |
36 | | :-------- |:------: |
37 | | type| compressSize 或者 quality. 默认值: compressSize|
38 | | urlList | 图片路径数组。例子: ['storage/emulated/0/Pictures/image-b1ae0ff3-c014-4e37-b510-f00d6ec88b08.jpg', 'image2.png'] |
39 | |maxWidth| 设置图片压缩后的最大宽度. 默认 : 380(该参数仅仅在type等于"compressSize"时有效)|
40 | |maxHeight| 设置图片压缩后的最大高度. 默认: 600(该参数仅仅在type等于"compressSize"时有效)|
41 | |quality| 0~100 default: 60|
42 | |saveImages | 设置是否保存压缩图片到本地。如果设置为false, 会返回一个压缩后图片的base64数组。默认值: true(该参数仅仅在type等于"compressSize"时有效)|
43 | |resultBase64 | 设置是否返回压缩图片的base64数组。默认值: false(该参数仅仅在type等于"compressSize"时有效)|
44 | ## 返回值
45 | | option | Info |
46 | | :-------- |:------: |
47 | | state | success or error|
48 | | msg | 发生错误时,会返回|
49 | | urlList | 返回压缩后的图片路径数组(only saveImages === true)|
50 | | base64List | 压缩图片的base64数组(saveImages === false or resultBase64 === true)|
51 | #### 注意
52 | 如果设置type等于compressSzie,并且保存图片到本地,将会覆盖上一次压缩的图片.
--------------------------------------------------------------------------------
/EN/readme-EN.md:
--------------------------------------------------------------------------------
1 | # react native compress images
2 |  
3 | ----------
4 | A React Native module that allows you to compress you images.( currently just support android).
5 |
6 | ## install
7 |
8 | yarn add react-native-compress-images
9 | ## Automatic Installation
10 | react-native link
11 | ## Usage
12 | 1, import CompressImages from 'react-native-compress-images';
13 |
14 | 2,
15 | ```
16 | CompressImages({
17 | urlList: ['storage/emulated/0/Pictures/image-b1ae0ff3-c014-4e37-b510-f00d6ec88b08.jpg']
18 | }).then((res) => {
19 | console.log(res);
20 | // base64
21 | console.log("data:image/jpeg;base64,"+data.base64List[0]);
22 | })
23 | ```
24 | ## Type
25 | now Support two types.
26 | ##### set type = 'compressSzie'
27 | Compress picture quality and size. this way will change picture size. Picture size is small
28 | #####set type = 'quality'
29 | this way just compress picture quality.
30 | (support library: [Luban](https://github.com/Curzibn/Luban))
31 | ## Options
32 |
33 | | option | Info |
34 | | :-------- |:------: |
35 | | type| compressSize or quality. default compressSize|
36 | | urlList | image path array。just like this: ['storage/emulated/0/Pictures/image-b1ae0ff3-c014-4e37-b510-f00d6ec88b08.jpg', 'image2.png'] |
37 | |maxWidth| definition compressed picture maxWidth. default : 380(Only when the type is equal to the 'compressSzie' is valid)|
38 | |maxHeight| definition compressed picture maxHeight. default : 600(Only when the type is equal to the 'compressSzie' is valid)|
39 | |quality| 0~100 default: 60|
40 | |saveImages | Save the picture to the local. if set false, module will return base64 image list. default true(Only when the type is equal to the 'compressSzie' is valid)|
41 | |resultBase64 | if set true, module will return base64 picture list. default: false(Only when the type is equal to the 'compressSzie' is valid)|
42 | ## The Response Object
43 | | option | Info |
44 | | :-------- |:------: |
45 | | status | success or error|
46 | | msg | when status equals 'error' will return|
47 | | urlList | local picture path list.(only saveImages === true)|
48 | | base64List | compressed picture base64 list.(saveImages === false or resultBase64 === true)|
49 | #### Note
50 | Repeatedly call the module, the compressed picture will overwrite the last compressed picture. So make sure the last compressed image is invalid.
--------------------------------------------------------------------------------
/android/src/main/java/com/example/react_native_compress_image/UtilFunction.java:
--------------------------------------------------------------------------------
1 | package com.example.react_native_compress_image;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.os.Environment;
6 | import android.util.Base64;
7 |
8 | import java.io.ByteArrayOutputStream;
9 | import java.io.File;
10 | import java.io.FileOutputStream;
11 | import java.io.IOException;
12 |
13 | /**
14 | * Created by Admin on 2018/1/16.
15 | */
16 |
17 | public class UtilFunction {
18 | /*
19 | * ***************辅助函数,保存,bitmap到文件夹**********
20 | * */
21 | public String saveBitmapToFile(Context context, String fileName , Bitmap bitmap, int quality) {
22 | FileOutputStream fOut = null;
23 | try {
24 | File file = null;
25 | String fileDstPath = "";
26 | // 判断是否挂载SD卡
27 | if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
28 | // 保存到sd卡的路径
29 | fileDstPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + "compressCatch" + File.separator + fileName;
30 | // 判断父级目录是否存在
31 | File homeDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
32 | + File.separator + "compressCatch" + File.separator);
33 | if (!homeDir.exists()) {
34 | homeDir.mkdirs();
35 | }
36 | } else {
37 | // 保存到file目录
38 | fileDstPath = context.getFilesDir().getAbsolutePath()
39 | + File.separator + "compressCatch" + File.separator + fileName;
40 |
41 | File homeDir = new File(context.getFilesDir().getAbsolutePath()
42 | + File.separator + "compressCatch" + File.separator);
43 | if (!homeDir.exists()) {
44 | homeDir.mkdir();
45 | }
46 | }
47 | // 新建文件
48 | file = new File(fileDstPath);
49 | // 文件存在就删除
50 | if (file.exists()) {
51 | file.delete();
52 | }
53 | // 保存文件
54 | fOut = new FileOutputStream(file);
55 | if (fileDstPath.endsWith("jpg")) {
56 | bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fOut);
57 | } else {
58 | bitmap.compress(Bitmap.CompressFormat.PNG, quality, fOut);
59 | }
60 | fOut.flush();
61 | fOut.close();
62 | bitmap.recycle();
63 | // 返回路径
64 | return fileDstPath;
65 | } catch (Exception e) {
66 | String sOut = "";
67 | StackTraceElement[] trace = e.getStackTrace();
68 | for (StackTraceElement s : trace) {
69 | sOut += "\tat " + s + "\r\n";
70 | }
71 | return "error";
72 | }
73 | }
74 | /*
75 | * **** bitmap转base64
76 | * */
77 | public static String bitmapToBase64(String fileName , Bitmap bitmap, int quality) {
78 | String result = null;
79 | ByteArrayOutputStream baos = null;
80 | try {
81 | if (bitmap != null) {
82 | baos = new ByteArrayOutputStream();
83 | if (fileName.endsWith("jpg")) {
84 | bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
85 | } else {
86 | bitmap.compress(Bitmap.CompressFormat.PNG, quality, baos);
87 | }
88 | baos.flush();
89 | baos.close();
90 | byte[] bitmapBytes = baos.toByteArray();
91 | result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
92 | }
93 | } catch (IOException e) {
94 | e.printStackTrace();
95 | } finally {
96 | try {
97 | if (baos != null) {
98 | baos.flush();
99 | baos.close();
100 | }
101 | } catch (IOException e) {
102 | e.printStackTrace();
103 | }
104 | }
105 | return result;
106 | }
107 | /*
108 | * ************获得路径*************
109 | * */
110 | public static String getSaveUrl (Context context) {
111 | String savePath = null;
112 | // 判断是否挂载SD卡
113 | if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
114 | // 判断父级目录是否存在
115 | savePath = Environment.getExternalStorageDirectory().getAbsolutePath()
116 | + File.separator + "compressCatch" + File.separator;
117 | File homeDir = new File(savePath);
118 | if (!homeDir.exists()) {
119 | homeDir.mkdirs();
120 | }
121 | } else {
122 | savePath = context.getFilesDir().getAbsolutePath()
123 | + File.separator + "compressCatch" + File.separator;
124 | File homeDir = new File(savePath);
125 | if (!homeDir.exists()) {
126 | homeDir.mkdir();
127 | }
128 | }
129 | return savePath;
130 | }
131 | }
132 |
--------------------------------------------------------------------------------
/android/src/main/java/com/example/react_native_compress_image/CompressImageModule.java:
--------------------------------------------------------------------------------
1 | package com.example.react_native_compress_image;
2 | import android.graphics.Bitmap;
3 | import android.graphics.BitmapFactory;
4 |
5 | import com.facebook.react.bridge.Arguments;
6 | import com.facebook.react.bridge.Promise;
7 | import com.facebook.react.bridge.ReactApplicationContext;
8 | import com.facebook.react.bridge.ReactContextBaseJavaModule;
9 | import com.facebook.react.bridge.ReactMethod;
10 | import com.facebook.react.bridge.ReadableArray;
11 | import com.facebook.react.bridge.ReadableMap;
12 | import com.facebook.react.bridge.WritableArray;
13 | import com.facebook.react.bridge.WritableMap;
14 | import com.facebook.react.bridge.WritableNativeArray;
15 |
16 | import java.io.File;
17 | import java.util.ArrayList;
18 |
19 | import top.zibin.luban.Luban;
20 | import top.zibin.luban.OnCompressListener;
21 |
22 | /**
23 | * Created by chenjinpei on 2018/1/15.
24 | */
25 |
26 | public class CompressImageModule extends ReactContextBaseJavaModule{
27 | public CompressImageModule(ReactApplicationContext reactContext) {
28 | super(reactContext);
29 | }
30 |
31 | @Override
32 | public String getName() {
33 | return "CompressImages";
34 | }
35 | // 以质量压缩
36 | @ReactMethod
37 | public void compressQuality(ReadableMap Options, final Promise promise) {
38 | final WritableArray resultList = new WritableNativeArray();
39 | // 判断是否有路径参数列表
40 | if (Options.hasKey("urlList")) {
41 | ReadableArray urlList = Options.getArray("urlList");
42 | final ArrayList photos = new ArrayList<>();
43 | // 循环遍历。添加图片
44 | for (int i = 0; i < urlList.size(); i++) {
45 | photos.add(urlList.getString(i));
46 | }
47 | // 调用Luban接口压缩图片
48 | Luban.with(getReactApplicationContext())
49 | .load(photos)
50 | .ignoreBy(100)
51 | .setTargetDir(UtilFunction.getSaveUrl(getReactApplicationContext()))
52 | .setCompressListener(new OnCompressListener() {
53 | @Override
54 | public void onStart() {
55 |
56 | }
57 | // 成功回调
58 | @Override
59 | public void onSuccess(File file) {
60 | // 保存路径
61 | resultList.pushString(file.getAbsolutePath());
62 | // 判断是否对所有图片执行了操作
63 | if (resultList.size() == photos.size()) {
64 | WritableMap map = Arguments.createMap();
65 | map.putString("status", "success");
66 | map.putArray("urlList", resultList);
67 | promise.resolve(map);
68 | }
69 | }
70 | // 发生错误
71 | @Override
72 | public void onError(Throwable e) {
73 | resultList.pushString(e.toString());
74 | // 判断是否对所有图片执行了操作
75 | if (resultList.size() == photos.size()) {
76 | WritableMap map = Arguments.createMap();
77 | map.putString("status", "success");
78 | map.putArray("urlList", resultList);
79 | promise.resolve(map);
80 | }
81 | }
82 | }).launch();
83 | } else {
84 | WritableMap map = Arguments.createMap();
85 | map.putString("status", "success");
86 | map.putString("errorMsg", "must have urlList");
87 | promise.resolve(map);
88 | }
89 | }
90 | // 以尺寸压缩
91 | @ReactMethod
92 | public void compressSize(ReadableMap Options, Promise promise) {
93 | WritableArray resultList = new WritableNativeArray();
94 | WritableArray base64List = new WritableNativeArray();
95 | float maxHeight = 600;
96 | float maxWidth = 380;
97 | int quality = 60;
98 | Boolean saveImages = true; // 设置是否保存图片
99 | Boolean resultBase64 = false; // 设置是否返回base64
100 | String status = "success";
101 | String msg = "";
102 | UtilFunction util = new UtilFunction();
103 | // 判断是否包含最大宽度
104 | if (Options.hasKey("maxWidth")) {
105 | maxWidth = Options.getInt("maxWidth");
106 | }
107 | // 判断是否包含参数最大高度
108 | if (Options.hasKey("maxHeight")) {
109 | maxHeight = Options.getInt("maxHeight");
110 | }
111 | // 判断是否包含参数质量
112 | if (Options.hasKey("quality")) {
113 | if (0 < Options.getInt("quality") && Options.getInt("quality") < 100) {
114 | quality = Options.getInt("quality");
115 | }
116 | }
117 | // 判断是否有
118 | if (Options.hasKey("saveImages")) {
119 | saveImages = Options.getBoolean("saveImages");
120 | }
121 | if (Options.hasKey("resultBase64")) {
122 | resultBase64 = Options.getBoolean("resultBase64");
123 | }
124 | // 判断是否有路径参数列表
125 | if (Options.hasKey("urlList")) {
126 | ReadableArray urlList = Options.getArray("urlList");
127 | // 循环遍历。压缩图片
128 | for (int i = 0; i < urlList.size(); i++) {
129 | String imgPath = urlList.getString(i);
130 | BitmapFactory.Options option = new BitmapFactory.Options();
131 | option.inJustDecodeBounds = true; //只获取图片的大小信息,而不是将整张图片载入在内存中,避免内存溢出
132 | Bitmap bitmap = BitmapFactory.decodeFile(imgPath, option); // 为null
133 | //获取到这个图片的原始宽度和高度
134 | int picWidth = option.outWidth;
135 | int picHeight = option.outHeight;
136 | int inSampleSize = 1; // 默认像素压缩比例,
137 | if (picWidth > picHeight && picWidth > maxWidth) {//如果宽度大的话根据宽度固定大小缩放
138 | inSampleSize = (int) Math.ceil((option.outWidth / maxWidth));
139 | } else if (picWidth < picHeight && picHeight > maxHeight) {//如果高度高的话根据宽度固定大小缩放
140 | inSampleSize = (int) Math.ceil((option.outHeight / maxHeight));
141 | }
142 | if (inSampleSize <= 0)
143 | inSampleSize = 1;
144 | option.inJustDecodeBounds = false;
145 | option.inSampleSize = inSampleSize;
146 | // 取缩放后的图片
147 | bitmap = BitmapFactory.decodeFile(imgPath, option);
148 | // 保存catch图片
149 | String saveName = "";
150 | if (imgPath.endsWith("jpg")) {
151 | saveName = "compressCatch"+i+".jpg";
152 | } else {
153 | saveName = "compressCatch"+i+".png";
154 | }
155 | // String catchPath = saveBitmapToFile(getReactApplicationContext(),saveName, bitmap);
156 | if (saveImages) {
157 | String catchPath = util.saveBitmapToFile(getReactApplicationContext(),saveName, bitmap, quality);
158 | resultList.pushString(catchPath);
159 | } else {
160 | String imgBase64 = util.bitmapToBase64(saveName, bitmap, quality);
161 | base64List.pushString(imgBase64);
162 | }
163 | //需要返回base64
164 | if (resultBase64 && saveImages) {
165 | String imgBase64 = util.bitmapToBase64(saveName, bitmap, quality);
166 | base64List.pushString(imgBase64);
167 | }
168 | }
169 | } else {
170 | status = "error";
171 | msg = "must have urlList";
172 | }
173 | // 确定返回值
174 | WritableMap map = Arguments.createMap();
175 | map.putString("status", status);
176 | if (status.equals("error")) {
177 | map.putString("errorMsg", msg);
178 | } else {
179 | // 设置返回值
180 | if (saveImages) {
181 | map.putArray("urlList", resultList);
182 | } else {
183 | map.putArray("base64List", base64List);
184 | }
185 | if (resultBase64 && saveImages) {
186 | map.putArray("base64List", base64List);
187 | }
188 | }
189 | promise.resolve(map);
190 | }
191 |
192 | }
193 |
--------------------------------------------------------------------------------
/android/react-native-compress-image.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | generateDebugSources
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 | true
106 |
107 | false
108 | true
109 | true
110 |
111 |
112 | true
113 | DEFINITION_ORDER
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 | General
129 |
130 |
131 | XPath
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |
203 |
204 |
205 | 1516001686492
206 |
207 |
208 | 1516001686492
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 |
226 |
227 |
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
239 |
240 |
241 |
242 |
243 |
244 |
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
275 |
276 |
277 |
278 |
279 |
280 |
281 |
282 |
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 |
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 |
319 |
320 |
321 |
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 |
331 |
332 |
333 |
334 |
335 |
336 |
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 |
346 |
347 |
348 |
349 |
350 |
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
359 |
360 |
361 |
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
370 |
371 |
372 |
373 |
374 |
375 |
376 |
377 |
378 |
379 |
380 |
381 |
382 |
383 |
384 |
385 |
386 |
387 |
388 |
389 |
390 |
391 |
392 |
393 |
394 |
395 |
396 |
397 |
398 |
399 |
400 |
401 |
402 |
403 |
404 |
405 |
406 |
407 |
408 |
409 |
410 |
411 |
412 |
413 |
414 |
415 |
416 |
417 |
418 |
419 |
420 |
421 |
422 |
423 |
424 |
425 |
426 |
427 |
428 |
429 |
430 |
431 |
432 |
433 |
434 |
435 |
436 |
437 |
438 |
439 |
440 |
441 |
442 |
443 |
444 |
445 |
446 |
447 |
448 |
449 |
450 |
451 |
452 |
453 |
454 |
455 |
456 |
457 |
458 |
459 |
460 |
461 |
462 |
463 |
464 |
465 |
466 |
467 |
468 |
469 |
470 |
471 |
472 |
473 |
474 |
475 |
476 |
477 |
478 |
479 |
480 |
481 |
482 |
483 |
484 |
485 |
486 |
487 |
488 |
489 |
490 |
491 |
492 |
493 |
494 |
495 |
496 |
--------------------------------------------------------------------------------