Ver código fonte

OPPO调试完成

詹子聪 5 anos atrás
pai
commit
a09c2621e1

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

@@ -72,12 +72,12 @@ public final class ImageUtil {
      * @param multi
      * @return
      */
-    public static boolean saveBitmapFile(Bitmap bitmap, File file, int multi) {
+    public static boolean saveBitmapFile(Bitmap bitmap, File file, float multi) {
         try {
             Bitmap compressedBitmap = bitmap;
             if (multi > 1) {
-                int targetWidth = bitmap.getWidth()/multi;
-                int targetHeight = bitmap.getHeight()/multi;
+                int targetWidth = (int) (bitmap.getWidth()/multi);
+                int targetHeight = (int) (bitmap.getHeight()/multi);
                 compressedBitmap = Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
             }
 

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

@@ -198,7 +198,7 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
                 //boolean isLandscape = mBottom-mTop > mRight-mLeft;
 
                 // 图片宽度与屏幕宽度的比
-                float widthD = 1.0f * normalWidth / SizeTool.SCREEN_WIDTH;
+                float widthD = 1.0f * normalWidth / mWidth;
                 // 图片高度与屏幕高度的比
                 float heightD = 1.0f * normalHeight / mHeight;
                 boolean isPortraitPhoto = BigDecimal.valueOf(heightD).compareTo(BigDecimal.valueOf(widthD)) > 0;
@@ -206,14 +206,14 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
                     // 高的倍距大,以宽为基准
                     if (widthD > 0) {
                         // 图片宽要缩小到屏幕宽
-                        float pivot = 1.0f * normalWidth / SizeTool.SCREEN_WIDTH;
+                        float pivot = 1.0f * normalWidth / mWidth;
                         realLeft = (int) (mLeft * pivot);
                         realTop = (int) (mTop * pivot);
                         realWidth = (int) ((mRight - mLeft) * pivot);
                         realHeight = (int) ((mBottom - mTop) * pivot);
                     } else {
                         // 屏幕宽要缩小到图片宽
-                        float pivot = 1.0f * normalWidth / SizeTool.SCREEN_WIDTH;
+                        float pivot = 1.0f * normalWidth / mWidth;
                         realLeft = (int) (mLeft * pivot);
                         realTop = (int) (mTop * pivot);
                         realWidth = (int) ((mRight - mLeft) * pivot);
@@ -337,60 +337,60 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
             public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                 // 所以要先恢复原来的图片再裁剪
                 //Bitmap normalBitmap = ImageUtil.rotateBitmap90(originalBmp);
-                int normalWidth = normalBitmap.getWidth();
-                int normalHeight = normalBitmap.getHeight();
+                int rawBitmapWidth = normalBitmap.getWidth();
+                int rawBitmapHeight = normalBitmap.getHeight();
 
                 // 图片不旋转,以屏幕为参考系进行缩小或者放大,直到图片一条边等于屏幕的一条边,图片的另一条边大于等于屏幕的另一条边
                 int realLeft;
                 int realTop;
-                int realWidth;
-                int realHeight;
+                int croppedBitmapWidth;
+                int croppedBitmapHeight;
 
                 //boolean isLandscape = mBottom-mTop > mRight-mLeft;
 
                 // 图片宽度与屏幕宽度的比
-                float widthD = 1.0f * normalWidth / SizeTool.SCREEN_WIDTH;
+                float widthD = 1.0f * rawBitmapWidth / mWidth;
                 // 图片高度与屏幕高度的比
-                float heightD = 1.0f * normalHeight / mHeight;
+                float heightD = 1.0f * rawBitmapHeight / mHeight;
                 boolean isPortraitPhoto = BigDecimal.valueOf(heightD).compareTo(BigDecimal.valueOf(widthD)) > 0;
                 if (isPortraitPhoto) {
                     // 高的倍距大,以宽为基准
-                    if (widthD > 0) {
+                    if (widthD > 1) {
                         // 图片宽要缩小到屏幕宽
-                        float pivot = 1.0f * normalWidth / SizeTool.SCREEN_WIDTH;
+                        float pivot = 1.0f * rawBitmapWidth / mWidth;
                         realLeft = (int) (mLeft * pivot);
                         realTop = (int) (mTop * pivot);
-                        realWidth = (int) ((mRight - mLeft) * pivot);
-                        realHeight = (int) ((mBottom - mTop) * pivot);
+                        croppedBitmapWidth = (int) ((mRight - mLeft) * pivot);
+                        croppedBitmapHeight = (int) ((mBottom - mTop) * pivot);
                     } else {
-                        // 屏幕宽要缩小到图片宽
-                        float pivot = 1.0f * normalWidth / SizeTool.SCREEN_WIDTH;
-                        realLeft = (int) (mLeft * pivot);
-                        realTop = (int) (mTop * pivot);
-                        realWidth = (int) ((mRight - mLeft) * pivot);
-                        realHeight = (int) ((mBottom - mTop) * pivot);
+                        // 屏幕高要缩小到图片高
+                        float pivot = 1.0f * mHeight / rawBitmapHeight;
+                        realLeft = (int) (mLeft / pivot);
+                        realTop = (int) (mTop / pivot);
+                        croppedBitmapWidth = (int) (1.0f * (mRight - mLeft) / pivot);
+                        croppedBitmapHeight = (int) (1.0f * (mBottom - mTop) / pivot);
                     }
                 } else {
                     // 宽的倍距大,以高为基准
-                    if (heightD > 0) {
+                    if (heightD > 1) {
                         // 图片高要缩小到屏幕高
-                        float pivot = 1.0f * normalHeight / mHeight;
+                        float pivot = 1.0f * rawBitmapHeight / mHeight;
                         realLeft = (int) (mLeft * pivot);
                         realTop = (int) (mTop * pivot);
-                        realWidth = (int) ((mRight - mLeft) * pivot);
-                        realHeight = (int) ((mBottom - mTop) * pivot);
+                        croppedBitmapWidth = (int) ((mRight - mLeft) * pivot);
+                        croppedBitmapHeight = (int) ((mBottom - mTop) * pivot);
                     } else {
-                        // 屏幕高要缩小到图片高,其实一样
-                        float pivot = 1.0f * normalHeight / mHeight;
-                        realLeft = (int) (mLeft * pivot);
-                        realTop = (int) (mTop * pivot);
-                        realWidth = (int) ((mRight - mLeft) * pivot);
-                        realHeight = (int) ((mBottom - mTop) * pivot);
+                        // 屏幕宽要缩小到图片宽
+                        float pivot = 1.0f * mWidth / rawBitmapWidth;
+                        realLeft = (int) (mLeft/pivot);
+                        realTop = (int) (mTop/pivot);
+                        croppedBitmapWidth = (int) (1.0f * (mRight - mLeft)/pivot);
+                        croppedBitmapHeight = (int) (1.0f * (mBottom - mTop)/pivot);
                     }
                 }
 
 
-                Bitmap croppedBmp = Bitmap.createBitmap(normalBitmap, realLeft, realTop, realWidth, realHeight);
+                Bitmap croppedBmp = Bitmap.createBitmap(normalBitmap, realLeft, realTop, croppedBitmapWidth, croppedBitmapHeight);
 
                 // 旋转90度,让OCR可以识别
                 Bitmap rotatedBitmap;
@@ -405,9 +405,9 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
                     file.getParentFile().mkdirs();
                 }
                 int max = rotatedBitmap.getWidth() > rotatedBitmap.getHeight() ? rotatedBitmap.getWidth() : rotatedBitmap.getHeight();
-                int multi = 1;
+                float multi = 1.0f;
                 if (max > 1000) {
-                    multi = max / 1000 + 1;
+                    multi = 1.0f * max / 1000 + 0.2f;
                 }
 
                 // 压缩图片