詹子聪 %!s(int64=5) %!d(string=hai) anos
pai
achega
5be26fe5d5

+ 41 - 0
app/src/main/java/com/miekir/ocr/tool/ImageUtil.java

@@ -60,6 +60,32 @@ public final class ImageUtil {
         return out.toByteArray();
         return out.toByteArray();
     }
     }
 
 
+    /**
+     * 保存图片并压缩指定倍数
+     * @param bitmap
+     * @param file
+     * @param multi
+     * @return
+     */
+    public static boolean saveBitmapFile(Bitmap bitmap, File file, int multi) {
+        try {
+            Bitmap compressedBitmap = bitmap;
+            if (multi > 1) {
+                int targetWidth = bitmap.getWidth()/multi;
+                int targetHeight = bitmap.getHeight()/multi;
+                compressedBitmap = Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
+            }
+
+            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
+            compressedBitmap.compress(Bitmap.CompressFormat.JPEG, 98, bos);
+            bos.flush();
+            bos.close();
+            return true;
+        } catch (IOException e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
 
 
     public static boolean saveBitmapFile(Bitmap bitmap, File file){
     public static boolean saveBitmapFile(Bitmap bitmap, File file){
         try {
         try {
@@ -74,6 +100,21 @@ public final class ImageUtil {
         }
         }
     }
     }
 
 
+    public static Bitmap getResizedBitmap(Bitmap image, int maxSize) {
+        int width = image.getWidth();
+        int height = image.getHeight();
+
+        float bitmapRatio = (float)width / (float) height;
+        if (bitmapRatio > 1) {
+            width = maxSize;
+            height = (int) (width / bitmapRatio);
+        } else {
+            height = maxSize;
+            width = (int) (height * bitmapRatio);
+        }
+        return Bitmap.createScaledBitmap(image, width, height, true);
+    }
+
     /**
     /**
      * 顺时针旋转90度
      * 顺时针旋转90度
      * @return
      * @return

+ 9 - 5
app/src/main/java/com/miekir/ocr/ui/OperationPresenter.java

@@ -259,17 +259,21 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
                 if (!file.getParentFile().exists()) {
                 if (!file.getParentFile().exists()) {
                     file.mkdirs();
                     file.mkdirs();
                 }
                 }
-                int rawWidth = rotatedBitmap.getWidth();
+                int max = rotatedBitmap.getWidth() > rotatedBitmap.getHeight() ? rotatedBitmap.getWidth() : rotatedBitmap.getHeight();
+                int multi = 1;
+                if (max > 1000) {
+                    multi = max / 1000;
+                }
+
                 // 压缩图片
                 // 压缩图片
-                boolean saveCropSuccess = ImageUtil.saveBitmapFile(rotatedBitmap, file);
+                boolean saveCropSuccess = ImageUtil.saveBitmapFile(rotatedBitmap, file, multi);
+
                 originalBmp.recycle();
                 originalBmp.recycle();
                 croppedBmp.recycle();
                 croppedBmp.recycle();
                 rotatedBitmap.recycle();
                 rotatedBitmap.recycle();
                 if (saveCropSuccess) {
                 if (saveCropSuccess) {
-                    long multiple = file.length()/1024/1024+1;
                     File compressedFile = new Compressor(context.getApplicationContext())
                     File compressedFile = new Compressor(context.getApplicationContext())
-                            .setMaxWidth((int) (rawWidth/multiple))
-                            .setQuality(85)
+                            .setQuality(92)
                             .setDestinationDirectoryPath(file.getParentFile().getAbsolutePath())
                             .setDestinationDirectoryPath(file.getParentFile().getAbsolutePath())
                             .compressToFile(file, "cp_" + file.getName());
                             .compressToFile(file, "cp_" + file.getName());
 
 

+ 1 - 1
app/src/main/res/values-zh-rCN/strings.xml

@@ -1,7 +1,7 @@
 <resources>
 <resources>
     <string name="app_name">NRI AIOCR</string>
     <string name="app_name">NRI AIOCR</string>
     <string name="copy">复制</string>
     <string name="copy">复制</string>
-    <string name="cancel">下一步下一步下一步下一步</string>
+    <string name="cancel">下一步</string>
     <string name="done">完成</string>
     <string name="done">完成</string>
     <string name="loading">请稍候...</string>
     <string name="loading">请稍候...</string>
     <string name="postal_code">邮政编号</string>
     <string name="postal_code">邮政编号</string>