Sfoglia il codice sorgente

原图删除与否的处理

詹子聪 6 anni fa
parent
commit
8c056d9ace

+ 1 - 1
app/src/main/AndroidManifest.xml

@@ -58,7 +58,7 @@
             android:name=".ui.camera.otalia.OtaOperationActivity"
             android:screenOrientation="portrait"
             android:configChanges="keyboardHidden|orientation|screenSize"
-            />
+            android:launchMode="singleTask"/>
 
         <activity
             android:name=".ui.camera.google.GoogleCameraActivity"

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

@@ -32,6 +32,7 @@ import io.reactivex.ObservableOnSubscribe;
 import io.reactivex.android.schedulers.AndroidSchedulers;
 import io.reactivex.schedulers.Schedulers;
 import top.zibin.luban.Luban;
+import top.zibin.luban.OnCompressListener;
 
 import static android.os.Environment.DIRECTORY_PICTURES;
 
@@ -101,6 +102,7 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
                                     .get();
                             File compressedFile = fileList.get(0);
 
+                            //从相册选择图片上传的话,不要删除原图
                             //file.delete();
                             // 通知相册去获取
                             MediaScannerConnection.scanFile(context, new String[] { compressedFile.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
@@ -370,63 +372,10 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
                     croppedBitmapWidth = (int) (1.0f * (mRight - mLeft)/pivot);
                     croppedBitmapHeight = (int) (1.0f * (mBottom - mTop)/pivot);
                 }
-
-
-
-
-                /*//boolean isLandscape = mBottom-mTop > mRight-mLeft;
-
-                // 图片宽度与屏幕宽度的比
-                float widthD = 1.0f * rawBitmapWidth / mWidth;
-                // 图片高度与屏幕高度的比
-                float heightD = 1.0f * rawBitmapHeight / mHeight;
-                boolean isPortraitPhoto = BigDecimal.valueOf(heightD).compareTo(BigDecimal.valueOf(widthD)) > 0;
-                if (isPortraitPhoto) {
-                    // 高的倍距大
-                    if (widthD >= 1) {
-                        // 以宽为基准
-                        // 图片宽要缩小到屏幕宽
-                        float pivot = 1.0f * rawBitmapWidth / mWidth;
-                        realLeft = (int) (mLeft * pivot);
-                        realTop = (int) (mTop * pivot) + (int)(((rawBitmapHeight/pivot-mHeight)/2)*pivot);
-                        croppedBitmapWidth = (int) ((mRight - mLeft) * pivot);
-                        croppedBitmapHeight = (int) ((mBottom - mTop) * pivot);
-                    } else {
-                        // 以高为基准
-                        // 屏幕高要缩小到图片高
-                        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 >= 1) {
-                        // 以高为基准
-                        // 图片高要缩小到屏幕高,缩小了pivot倍
-                        float pivot = 1.0f * rawBitmapWidth / mWidth;
-                        realLeft = (int) (mLeft * pivot) + (int)((rawBitmapWidth/pivot - mWidth)/2*pivot);
-                        realTop = (int) (mTop * pivot);
-                        croppedBitmapWidth = (int) ((mRight - mLeft) * pivot);
-                        croppedBitmapHeight = (int) ((mBottom - mTop) * pivot);
-                    } else {
-                        // 以宽为基准
-                        // 屏幕宽要缩小到图片宽
-                        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, croppedBitmapWidth, croppedBitmapHeight);
 
                 // 旋转90度,让OCR可以识别
                 Bitmap rotatedBitmap;
-                //if (isLandscape) {
                 if (isScreenLandscape) {
                     rotatedBitmap = ImageUtil.rotateBitmap(croppedBmp, -90);
                 } else {
@@ -436,88 +385,85 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
                 if (!file.getParentFile().exists()) {
                     file.getParentFile().mkdirs();
                 }
-                int max = rotatedBitmap.getWidth() > rotatedBitmap.getHeight() ? rotatedBitmap.getWidth() : rotatedBitmap.getHeight();
-                float multi = 1.0f;
-//                if (max > 2000) {
-//                    multi = 1.0f * max / 2000 + 0.4f;
-//                } else if (multi > 1800) {
-//                    multi = 1.1f;
-//                }
 
                 // 压缩图片
-                boolean saveCropSuccess = ImageUtil.saveBitmapFile(rotatedBitmap, file, multi);
+                boolean saveCropSuccess = ImageUtil.saveBitmapFile(rotatedBitmap, file, 1);
 
-                //originalBmp.recycle();
                 normalBitmap.recycle();
                 croppedBmp.recycle();
                 rotatedBitmap.recycle();
                 if (saveCropSuccess) {
-                    /*File compressedFile = new Compressor(context.getApplicationContext())
-                            .setQuality(97)
-                            .setDestinationDirectoryPath(file.getParentFile().getAbsolutePath())
-                            .compressToFile(file, "cp_" + file.getName());*/
-
-                    List<File> fileList = Luban.with(context)
+                    Luban.with(context)
                             .load(file)
                             .ignoreBy(400)
                             .setTargetDir(file.getParentFile().getAbsolutePath())
-                            .get();
-                    File compressedFile = fileList.get(0);
+                            .setCompressListener(new OnCompressListener() {
+                        @Override
+                        public void onStart() {
+                            // TODO 压缩开始前调用,可以在方法内启动 loading UI
+                        }
 
-                    file.delete();
-                    // 通知相册去获取
-                    MediaScannerConnection.scanFile(context, new String[] { compressedFile.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
-                    //MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
-                    // 保存最终截图成功
-                    // 根据URI获取图片的base64字符串
-                    String photoBase64 = Base64Tool.getBase64FromFilePath(compressedFile.getAbsolutePath());
-                    if (TextUtils.isEmpty(photoBase64)) {
-                        emitter.onError(new Exception("Empty image"));
-                    } else {
-                        emitter.onNext(photoBase64);
-                    }
+                        @Override
+                        public void onSuccess(File compressedFile) {
+                            // 通知相册去获取
+                            MediaScannerConnection.scanFile(context, new String[] { compressedFile.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
+                            //MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
+                            // 保存最终截图成功,根据URI获取图片的base64字符串
+                            String photoBase64 = Base64Tool.getBase64FromFilePath(compressedFile.getAbsolutePath());
+                            // 文件名相同的话,表示没有压缩
+                            if (!TextUtils.equals(file.getAbsolutePath(), compressedFile.getAbsolutePath())) {
+                                file.delete();
+                            }
+                            if (TextUtils.isEmpty(photoBase64)) {
+                                emitter.onError(new Exception("Empty image"));
+                            } else {
+                                emitter.onNext(photoBase64);
+                            }
+                        }
+
+                        @Override
+                        public void onError(Throwable e) {
+                            emitter.onError(new Exception("Empty image"));
+                        }
+                    }).launch();
                 } else {
                     emitter.onError(new Exception("Empty image"));
                 }
             }
         })
-                /*.filter(base64String -> {
-                    // 图片base64不为空才继续,否则直接忽略,没有任何回调会被调用
-                    return !TextUtils.isEmpty(base64String);
-                })*/
-                .flatMap(photoBase64 -> {
-                    Map<String, Object> requestParams = new HashMap<>();
-                    requestParams.put("request_id", "035992000119071904311742_" + DateTool.getRequestId());
-                    requestParams.put("appid", "10233937");
-                    requestParams.put("image", photoBase64);
-                    Map<String, Object> options = new HashMap<>();
-                    options.put("scene", scene);
-                    requestParams.put("options", options);
-
-                    // 发送识别请求
-                    return RetrofitHelper.getInstance()
-                            .getRequestApi(ApiService.class)
-                            .getOcrResult(requestParams);
-                })
-                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
-                .subscribe(new BaseObserver<OcrResult>() {
-                    @Override
-                    public void onSuccess(OcrResult result) {
-                        if (getView() != null) {
-                            getView().dismissLoading();
-                            getView().onOcrResult(result);
-                        }
-                    }
+        .flatMap(photoBase64 -> {
+            Map<String, Object> requestParams = new HashMap<>();
+            requestParams.put("request_id", "035992000119071904311742_" + DateTool.getRequestId());
+            requestParams.put("appid", "10233937");
+            requestParams.put("image", photoBase64);
+            Map<String, Object> options = new HashMap<>();
+            options.put("scene", scene);
+            requestParams.put("options", options);
 
-                    @Override
-                    public void onFailure(Throwable e, String errMsg) {
-                        // 装饰器模式
-                        if (getView() != null) {
-                            getView().dismissLoading();
-                            getView().onOcrResult(null);
-                        }
-                    }
-                });
+            // 发送识别请求
+            return RetrofitHelper.getInstance()
+                    .getRequestApi(ApiService.class)
+                    .getOcrResult(requestParams);
+        })
+        .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
+        .subscribe(new BaseObserver<OcrResult>() {
+            @Override
+            public void onSuccess(OcrResult result) {
+                if (getView() != null) {
+                    getView().dismissLoading();
+                    getView().onOcrResult(result);
+                }
+            }
+
+            @Override
+            public void onFailure(Throwable e, String errMsg) {
+                // 装饰器模式
+                if (getView() != null) {
+                    getView().dismissLoading();
+                    getView().onOcrResult(null);
+                }
+            }
+        });
     }
 
     private int mWidth = SizeTool.SCREEN_WIDTH;

+ 12 - 12
cameraview/src/main/java/com/otaliastudios/cameraview/internal/OrientationHelper.java

@@ -41,7 +41,7 @@ public class OrientationHelper {
     final DisplayManager.DisplayListener mDisplayOffsetListener;
     private int mDisplayOffset = 0;
 
-    private boolean mEnabled = true;
+    private static final boolean mEnabled = true;
 
     /**
      * Creates a new orientation helper.
@@ -103,7 +103,7 @@ public class OrientationHelper {
      */
     public void enable() {
         if (mEnabled) return;
-        mEnabled = true;
+        //mEnabled = true;
         mDisplayOffset = findDisplayOffset();
         if (Build.VERSION.SDK_INT >= 17) {
             DisplayManager manager = (DisplayManager)
@@ -118,16 +118,16 @@ public class OrientationHelper {
      * Disables this listener.
      */
     public void disable() {
-        if (!mEnabled) return;
-        mEnabled = false;
-        mDeviceOrientationListener.disable();
-        if (Build.VERSION.SDK_INT >= 17) {
-            DisplayManager manager = (DisplayManager)
-                    mContext.getSystemService(Context.DISPLAY_SERVICE);
-            manager.unregisterDisplayListener(mDisplayOffsetListener);
-        }
-        mDisplayOffset = -1;
-        mDeviceOrientation = -1;
+//        if (!mEnabled) return;
+//        mEnabled = false;
+//        mDeviceOrientationListener.disable();
+//        if (Build.VERSION.SDK_INT >= 17) {
+//            DisplayManager manager = (DisplayManager)
+//                    mContext.getSystemService(Context.DISPLAY_SERVICE);
+//            manager.unregisterDisplayListener(mDisplayOffsetListener);
+//        }
+//        mDisplayOffset = -1;
+//        mDeviceOrientation = -1;
     }
 
     /**