|
|
@@ -2,7 +2,6 @@ package com.miekir.ocr.ui;
|
|
|
|
|
|
import android.content.Context;
|
|
|
import android.graphics.Bitmap;
|
|
|
-import android.graphics.BitmapFactory;
|
|
|
import android.media.MediaScannerConnection;
|
|
|
import android.net.Uri;
|
|
|
import android.os.Environment;
|
|
|
@@ -21,7 +20,6 @@ import com.miekir.ocr.tool.ImageOrientation;
|
|
|
import com.miekir.ocr.tool.ImageUtil;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.math.BigDecimal;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
@@ -194,169 +192,6 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
|
|
|
|
|
|
@Override
|
|
|
public void onFailure(Throwable e, String errMsg) {
|
|
|
- // 装饰器模式
|
|
|
- if (getView() != null) {
|
|
|
- getView().dismissLoading();
|
|
|
- getView().onOcrResult(null);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- public void startOcrFromFile(Context context, byte[] rawPhotoBytes, int[] rectData, File file, String scene, boolean isScreenLandscape) {
|
|
|
- getView().showLoading(context.getResources().getString(R.string.loading));
|
|
|
- int mLeft = rectData[0];
|
|
|
- int mTop = rectData[1];
|
|
|
- int mRight = rectData[2];
|
|
|
- int mBottom = rectData[3];
|
|
|
- Observable.create(new ObservableOnSubscribe<String>() {
|
|
|
- @Override
|
|
|
- public void subscribe(ObservableEmitter<String> emitter) throws Exception {
|
|
|
- Bitmap originalBmp = BitmapFactory.decodeByteArray(rawPhotoBytes, 0, rawPhotoBytes.length);
|
|
|
- // 这里由于要快速拍照,使用的格式是ImageFormat.YUV_420_888,导致拍出来的照片自动逆时针90度,
|
|
|
- // 所以要先恢复原来的图片再裁剪
|
|
|
- Bitmap normalBitmap = ImageUtil.rotateBitmap90(originalBmp);
|
|
|
- int normalWidth = normalBitmap.getWidth();
|
|
|
- int normalHeight = normalBitmap.getHeight();
|
|
|
-
|
|
|
- // 图片不旋转,以屏幕为参考系进行缩小或者放大,直到图片一条边等于屏幕的一条边,图片的另一条边大于等于屏幕的另一条边
|
|
|
- int realLeft;
|
|
|
- int realTop;
|
|
|
- int realWidth;
|
|
|
- int realHeight;
|
|
|
-
|
|
|
- //boolean isLandscape = mBottom-mTop > mRight-mLeft;
|
|
|
-
|
|
|
- // 图片宽度与屏幕宽度的比
|
|
|
- float widthD = 1.0f * normalWidth / mWidth;
|
|
|
- // 图片高度与屏幕高度的比
|
|
|
- float heightD = 1.0f * normalHeight / mHeight;
|
|
|
- boolean isPortraitPhoto = BigDecimal.valueOf(heightD).compareTo(BigDecimal.valueOf(widthD)) > 0;
|
|
|
- if (isPortraitPhoto) {
|
|
|
- // 高的倍距大,以宽为基准
|
|
|
- if (widthD > 0) {
|
|
|
- // 图片宽要缩小到屏幕宽
|
|
|
- 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 / mWidth;
|
|
|
- realLeft = (int) (mLeft * pivot);
|
|
|
- realTop = (int) (mTop * pivot);
|
|
|
- realWidth = (int) ((mRight - mLeft) * pivot);
|
|
|
- realHeight = (int) ((mBottom - mTop) * pivot);
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 宽的倍距大,以高为基准
|
|
|
- if (heightD > 0) {
|
|
|
- // 图片高要缩小到屏幕高
|
|
|
- float pivot = 1.0f * normalHeight / mHeight;
|
|
|
- realLeft = (int) (mLeft * pivot);
|
|
|
- realTop = (int) (mTop * pivot);
|
|
|
- realWidth = (int) ((mRight - mLeft) * pivot);
|
|
|
- realHeight = (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);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- Bitmap croppedBmp = Bitmap.createBitmap(normalBitmap, realLeft, realTop, realWidth, realHeight);
|
|
|
-
|
|
|
- // 旋转90度,让OCR可以识别
|
|
|
- Bitmap rotatedBitmap;
|
|
|
- //if (isLandscape) {
|
|
|
- if (isScreenLandscape) {
|
|
|
- rotatedBitmap = ImageUtil.rotateBitmap(croppedBmp, -90);
|
|
|
- } else {
|
|
|
- rotatedBitmap = croppedBmp;
|
|
|
- }
|
|
|
-
|
|
|
- if (!file.getParentFile().exists()) {
|
|
|
- file.getParentFile().mkdirs();
|
|
|
- }
|
|
|
- int max = rotatedBitmap.getWidth() > rotatedBitmap.getHeight() ? rotatedBitmap.getWidth() : rotatedBitmap.getHeight();
|
|
|
- int multi = 1;
|
|
|
- if (max > 1000) {
|
|
|
- multi = max / 1000 + 1;
|
|
|
- }
|
|
|
-
|
|
|
- // 压缩图片
|
|
|
- boolean saveCropSuccess = ImageUtil.saveBitmapFile(rotatedBitmap, file, multi);
|
|
|
-
|
|
|
- originalBmp.recycle();
|
|
|
- croppedBmp.recycle();
|
|
|
- rotatedBitmap.recycle();
|
|
|
- if (saveCropSuccess) {
|
|
|
- List<File> fileList = Luban.with(context)
|
|
|
- .load(file)
|
|
|
- .ignoreBy(400)
|
|
|
- .setTargetDir(file.getParentFile().getAbsolutePath())
|
|
|
- .get();
|
|
|
- if (fileList != null && fileList.size() == 1) {
|
|
|
- File compressedFile = fileList.get(0);
|
|
|
-
|
|
|
- if (!TextUtils.equals(compressedFile.getAbsolutePath(), file.getAbsolutePath())) {
|
|
|
- file.delete();
|
|
|
- }
|
|
|
-
|
|
|
- // 通知相册去获取
|
|
|
- MediaScannerConnection.scanFile(context, new String[] { compressedFile.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);
|
|
|
- }
|
|
|
- } else {
|
|
|
- emitter.onNext("Empty image");
|
|
|
- }
|
|
|
- } 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);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onFailure(Throwable e, String errMsg) {
|
|
|
- // 装饰器模式
|
|
|
if (getView() != null) {
|
|
|
getView().dismissLoading();
|
|
|
getView().onOcrResult(null);
|
|
|
@@ -365,7 +200,6 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-
|
|
|
public void startOcrFromBitmap(Context context, Bitmap normalBitmap, int[] rectData, File file, String scene, boolean isScreenLandscape) {
|
|
|
getView().showLoading(context.getResources().getString(R.string.loading));
|
|
|
int mLeft = rectData[0];
|
|
|
@@ -437,7 +271,6 @@ public class OperationPresenter extends BasePresenter<IOperationView> {
|
|
|
// .setDestinationDirectoryPath(file.getParentFile().getAbsolutePath())
|
|
|
// .compressToFile(file, "cp_" + file.getName());
|
|
|
//
|
|
|
-// if (compressedFile.exists()) {
|
|
|
// 通知相册去获取
|
|
|
MediaScannerConnection.scanFile(context, new String[] { compressedFile.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
|
|
|
//MediaScannerConnection.scanFile(context, new String[] { file.getAbsolutePath() }, new String[] { "image/jpeg" }, null);
|