Jelajahi Sumber

相册选择压缩太严重了

詹子聪 5 tahun lalu
induk
melakukan
ec0367cf25

+ 1 - 1
app/src/main/java/com/miekir/ocr/tool/Base64Tool.java

@@ -49,7 +49,7 @@ public class Base64Tool {
         return encodedString;
     }
 
-    private static String bitmap2Base64(Bitmap bitmap) {
+    public static String bitmap2Base64(Bitmap bitmap) {
         if (bitmap == null) {
             return "";
         }

+ 33 - 25
app/src/main/java/com/miekir/ocr/ui/OperationPresenter.java

@@ -79,33 +79,42 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
                         file.getParentFile().mkdirs();
                     }
 
-                    // 开始压缩图片
-                    boolean saveCropSuccess = ImageUtil.saveBitmapFile(rotatedBitmap, file, multi);
-
-                    rotatedBitmap.recycle();
-                    if (saveCropSuccess) {
-                        File compressedFile = new Compressor(context.getApplicationContext())
-                                .setQuality(98)
-                                .setDestinationDirectoryPath(file.getParentFile().getAbsolutePath())
-                                .compressToFile(file, "cp_" + file.getName());
-
-                        file.delete();
-                        // 保存最终截图成功
-                        // 根据URI获取图片的base64字符串
-                        photoBase64 = Base64Tool.getBase64FromFilePath(compressedFile.getAbsolutePath());
-
-                        if (TextUtils.isEmpty(photoBase64)) {
-                            photoBase64 = Base64Tool.getBase64FromUri(context, uri);
-                        }
-
-                        if (TextUtils.isEmpty(photoBase64)) {
-                            emitter.onError(new Exception("Empty image"));
+                    if (multi > 1) {
+                        // 开始压缩图片
+                        boolean saveCropSuccess = ImageUtil.saveBitmapFile(rotatedBitmap, file, multi);
+
+                        rotatedBitmap.recycle();
+                        if (saveCropSuccess) {
+                            File compressedFile = new Compressor(context.getApplicationContext())
+                                    .setQuality(99)
+                                    .setDestinationDirectoryPath(file.getParentFile().getAbsolutePath())
+                                    .compressToFile(file, "cp_" + file.getName());
+
+                            file.delete();
+                            // 保存最终截图成功
+                            // 根据URI获取图片的base64字符串
+                            photoBase64 = Base64Tool.getBase64FromFilePath(compressedFile.getAbsolutePath());
+
+                            if (TextUtils.isEmpty(photoBase64)) {
+                                photoBase64 = Base64Tool.getBase64FromUri(context, uri);
+                            }
+
+                            if (TextUtils.isEmpty(photoBase64)) {
+                                emitter.onError(new Exception("Empty image"));
+                            } else {
+                                emitter.onNext(photoBase64);
+                            }
                         } else {
-                            emitter.onNext(photoBase64);
+                            photoBase64 = Base64Tool.bitmap2Base64(rotatedBitmap);
+                            if (TextUtils.isEmpty(photoBase64)) {
+                                emitter.onError(new Exception("Empty image"));
+                            } else {
+                                emitter.onNext(photoBase64);
+                            }
                         }
                     } else {
-                        photoBase64 = Base64Tool.getBase64FromUri(context, uri);
-
+                        // 不用压缩
+                        photoBase64 = Base64Tool.bitmap2Base64(rotatedBitmap);
                         if (TextUtils.isEmpty(photoBase64)) {
                             emitter.onError(new Exception("Empty image"));
                         } else {
@@ -263,7 +272,6 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
                 } else {
                     emitter.onError(new Exception("Empty image"));
                 }
-
             }
         })
                 /*.filter(base64String -> {