|
|
@@ -0,0 +1,316 @@
|
|
|
+package com.miekir.ocr.ui.camera.wechat;
|
|
|
+
|
|
|
+import android.app.AlertDialog;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.pm.ActivityInfo;
|
|
|
+import android.graphics.Bitmap;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.Environment;
|
|
|
+import android.text.TextUtils;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.LinearLayout;
|
|
|
+import android.widget.TextView;
|
|
|
+
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+
|
|
|
+import com.miekir.common.utils.ToastTool;
|
|
|
+import com.miekir.common.utils.ViewTool;
|
|
|
+import com.miekir.mvp.presenter.InjectPresenter;
|
|
|
+import com.miekir.ocr.R;
|
|
|
+import com.miekir.ocr.bean.OcrResult;
|
|
|
+import com.miekir.ocr.tool.AnimateManager;
|
|
|
+import com.miekir.ocr.tool.SystemTool;
|
|
|
+import com.miekir.ocr.ui.IOperationView;
|
|
|
+import com.miekir.ocr.ui.OperationPresenter;
|
|
|
+import com.miekir.ocr.widget.CropView;
|
|
|
+import com.miekir.ocr.widget.GlideV4ImageEngine;
|
|
|
+import com.miekir.ocr.widget.IndicatorText;
|
|
|
+import com.zhihu.matisse.Matisse;
|
|
|
+import com.zhihu.matisse.MimeType;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static android.os.Environment.DIRECTORY_PICTURES;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Copyright (C), 2019-2020, Genlot
|
|
|
+ *
|
|
|
+ * @author 詹子聪
|
|
|
+ * @date 2020/7/27 9:01
|
|
|
+ * Description: 菜单操作
|
|
|
+ */
|
|
|
+public class WechatOperationActivity extends WechatCameraActivity implements CropView.onLocationListener, View.OnClickListener, IOperationView {
|
|
|
+ private static final int REQUEST_CODE_CHOOSE = 6;
|
|
|
+
|
|
|
+ @InjectPresenter
|
|
|
+ OperationPresenter mOperationPresenter;
|
|
|
+
|
|
|
+ private int mCurrentType = CropView.SCAN_TYPE_ALL;
|
|
|
+ private String mCurrentScene = CropView.SCAN_SCENES[mCurrentType];
|
|
|
+ private int[] MENU_ID_LIST = {R.id.it_postal, R.id.it_address, R.id.it_name, R.id.it_all};
|
|
|
+
|
|
|
+ private TextView tv_orientation;
|
|
|
+ private String mLandscapeString;
|
|
|
+ private String mPortraitString;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void initViews(Bundle savedInstanceState) {
|
|
|
+ super.initViews(savedInstanceState);
|
|
|
+
|
|
|
+ int[] clickIds = new int[]{R.id.it_postal, R.id.fl_take, R.id.it_address, R.id.it_name, R.id.it_all, R.id.fl_album};
|
|
|
+ ViewTool.setOnClickListener(this, clickIds, this);
|
|
|
+
|
|
|
+ // 裁剪View
|
|
|
+ pcv_scan.setLocationListener(this);
|
|
|
+ //showResultDialog(null);
|
|
|
+ //showLoading("Loading...");
|
|
|
+
|
|
|
+ mLandscapeString = getResources().getString(R.string.landscape);
|
|
|
+ mPortraitString = getResources().getString(R.string.portrait);
|
|
|
+ tv_orientation = findViewById(R.id.tv_orientation);
|
|
|
+ tv_orientation.setOnClickListener(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClick(View v) {
|
|
|
+ switch (v.getId()) {
|
|
|
+ case R.id.fl_take:
|
|
|
+ if (jCameraView != null) {
|
|
|
+ jCameraView.takePhoto();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case R.id.fl_album:
|
|
|
+ Matisse.from(this)
|
|
|
+ .choose(MimeType.ofImage())
|
|
|
+ .countable(true)
|
|
|
+ .maxSelectable(1)
|
|
|
+ //.addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
|
|
|
+ //.gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
|
|
|
+ .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
|
|
|
+ .thumbnailScale(0.85f)
|
|
|
+ //.imageEngine(new GlideEngine())
|
|
|
+ .imageEngine(new GlideV4ImageEngine())
|
|
|
+ //.showPreview(false) // Default is `true`
|
|
|
+ .forResult(REQUEST_CODE_CHOOSE);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case R.id.it_postal:
|
|
|
+ case R.id.it_address:
|
|
|
+ case R.id.it_name:
|
|
|
+ case R.id.it_all:
|
|
|
+ setScanArea(v.getId());
|
|
|
+ if (v.getId() == R.id.it_all) {
|
|
|
+ tv_orientation.setText(mLandscapeString);
|
|
|
+ tv_orientation.setVisibility(View.VISIBLE);
|
|
|
+ } else {
|
|
|
+ tv_orientation.setVisibility(View.INVISIBLE);
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+
|
|
|
+ case R.id.tv_orientation:
|
|
|
+ String textOld = tv_orientation.getText().toString();
|
|
|
+ if (TextUtils.equals(mLandscapeString, textOld)) {
|
|
|
+ tv_orientation.setText(mPortraitString);
|
|
|
+ } else {
|
|
|
+ tv_orientation.setText(mLandscapeString);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (tv_orientation.getVisibility() == View.VISIBLE) {
|
|
|
+ // 切换横竖屏
|
|
|
+ AnimateManager.getInstance().stopAnimation();
|
|
|
+ pcv_scan.switchOrientation();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置裁剪区域
|
|
|
+ * @param viewId
|
|
|
+ */
|
|
|
+ private void setScanArea(int viewId) {
|
|
|
+ for (int index = 0, len = MENU_ID_LIST.length; index < len; index++) {
|
|
|
+ IndicatorText indicatorText = findViewById(MENU_ID_LIST[index]);
|
|
|
+ if (MENU_ID_LIST[index] == viewId) {
|
|
|
+ indicatorText.onIndicatorClicked();
|
|
|
+ mCurrentType = CropView.SCAN_TYPES[index];
|
|
|
+ mCurrentScene = CropView.SCAN_SCENES[index];
|
|
|
+ } else {
|
|
|
+ indicatorText.onIndicatorRelease();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ pcv_scan.setScanArea(mCurrentType);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
|
|
+ super.onActivityResult(requestCode, resultCode, data);
|
|
|
+ if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_CHOOSE && data != null) {
|
|
|
+ List<Uri> selectedList = Matisse.obtainResult(data);
|
|
|
+ if (selectedList != null && selectedList.size() > 0) {
|
|
|
+ if (tv_orientation.getVisibility() == View.VISIBLE) {
|
|
|
+ mOperationPresenter.startOcrFromUri(this, selectedList.get(0), mCurrentScene, pcv_scan.isLandscape());
|
|
|
+ } else {
|
|
|
+ mOperationPresenter.startOcrFromUri(this, selectedList.get(0), mCurrentScene, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void locationRect(int startX, int startY, int endX, int endY) {
|
|
|
+ // 截取的区域坐标信息
|
|
|
+ //System.out.println("x:"+startX + " y:" + startY + " endX:" + endX + " endY" + endY);
|
|
|
+ /*mLeft = startY;
|
|
|
+ mTop = SizeTool.SCREEN_WIDTH - endX;
|
|
|
+ mRight = endY;
|
|
|
+ mBottom = SizeTool.SCREEN_WIDTH - startX;*/
|
|
|
+
|
|
|
+ mLeft = startX;
|
|
|
+ mTop = startY;
|
|
|
+ mRight = endX;
|
|
|
+ mBottom = endY;
|
|
|
+
|
|
|
+ /*mLeft = startY;
|
|
|
+ mTop = SizeTool.SCREEN_WIDTH - endX;
|
|
|
+ mRight = endY;
|
|
|
+ mBottom = SizeTool.SCREEN_WIDTH - startX;*/
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onOcrResult(OcrResult result) {
|
|
|
+ // 识别结果
|
|
|
+ showResultDialog(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 弹出识别对话框
|
|
|
+ * @param result
|
|
|
+ */
|
|
|
+ private void showResultDialog(OcrResult result) {
|
|
|
+ AlertDialog dialog = new AlertDialog.Builder(this).setView(R.layout.dialog_result).create();
|
|
|
+ dialog.show();
|
|
|
+ dialog.setCancelable(false);
|
|
|
+ dialog.setCanceledOnTouchOutside(false);
|
|
|
+
|
|
|
+ dialog.findViewById(R.id.tv_copy).setOnClickListener(v -> {
|
|
|
+ dialog.cancel();
|
|
|
+ if (result == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ SystemTool.copyText(WechatOperationActivity.this, "");
|
|
|
+ // 复制全部识别信息
|
|
|
+ if (mCurrentType == CropView.SCAN_TYPE_ALL) {
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ if (result.postcode != null) {
|
|
|
+ builder.append(result.postcode.getText()).append(",");
|
|
|
+ }
|
|
|
+ if (result.address != null) {
|
|
|
+ builder.append(result.address.getText()).append(",");
|
|
|
+ }
|
|
|
+ if (result.name != null) {
|
|
|
+ builder.append(result.name.getText()).append(",");
|
|
|
+ }
|
|
|
+ if (builder.length() > 0) {
|
|
|
+ builder.deleteCharAt(builder.length()-1);
|
|
|
+ SystemTool.copyText(WechatOperationActivity.this, builder.toString());
|
|
|
+ } else {
|
|
|
+ SystemTool.copyText(WechatOperationActivity.this, " ");
|
|
|
+ }
|
|
|
+ } else if (mCurrentType == CropView.SCAN_TYPE_POSTAL) {
|
|
|
+ if (result.postcode != null) {
|
|
|
+ SystemTool.copyText(WechatOperationActivity.this, result.postcode.getText());
|
|
|
+ } else {
|
|
|
+ SystemTool.copyText(WechatOperationActivity.this, " ");
|
|
|
+ }
|
|
|
+ } else if (mCurrentType == CropView.SCAN_TYPE_ADDRESS) {
|
|
|
+ if (result.address != null) {
|
|
|
+ SystemTool.copyText(WechatOperationActivity.this, result.address.getText());
|
|
|
+ } else {
|
|
|
+ SystemTool.copyText(WechatOperationActivity.this, " ");
|
|
|
+ }
|
|
|
+ } else if (mCurrentType == CropView.SCAN_TYPE_NAME) {
|
|
|
+ if (result.name != null) {
|
|
|
+ SystemTool.copyText(WechatOperationActivity.this, result.name.getText());
|
|
|
+ } else {
|
|
|
+ SystemTool.copyText(WechatOperationActivity.this, " ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ToastTool.showShort(getString(R.string.copy_success));
|
|
|
+ });
|
|
|
+ dialog.findViewById(R.id.tv_cancel).setOnClickListener(v -> {
|
|
|
+ dialog.cancel();
|
|
|
+ });
|
|
|
+
|
|
|
+ LinearLayout ll_postal = dialog.findViewById(R.id.ll_postal);
|
|
|
+ LinearLayout ll_address = dialog.findViewById(R.id.ll_address);
|
|
|
+ LinearLayout ll_name = dialog.findViewById(R.id.ll_name);
|
|
|
+ if (mCurrentType == CropView.SCAN_TYPE_ALL) {
|
|
|
+ ll_postal.setVisibility(View.VISIBLE);
|
|
|
+ ll_address.setVisibility(View.VISIBLE);
|
|
|
+ ll_name.setVisibility(View.VISIBLE);
|
|
|
+ } else if (mCurrentType == CropView.SCAN_TYPE_POSTAL) {
|
|
|
+ ll_postal.setVisibility(View.VISIBLE);
|
|
|
+ ll_address.setVisibility(View.GONE);
|
|
|
+ ll_name.setVisibility(View.GONE);
|
|
|
+ } else if (mCurrentType == CropView.SCAN_TYPE_ADDRESS) {
|
|
|
+ ll_address.setVisibility(View.VISIBLE);
|
|
|
+ ll_postal.setVisibility(View.GONE);
|
|
|
+ ll_name.setVisibility(View.GONE);
|
|
|
+
|
|
|
+ } else if (mCurrentType == CropView.SCAN_TYPE_NAME) {
|
|
|
+ ll_name.setVisibility(View.VISIBLE);
|
|
|
+ ll_postal.setVisibility(View.GONE);
|
|
|
+ ll_address.setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+
|
|
|
+ TextView tv_postal = dialog.findViewById(R.id.tv_postal);
|
|
|
+ TextView tv_address = dialog.findViewById(R.id.tv_address);
|
|
|
+ TextView tv_name = dialog.findViewById(R.id.tv_name);
|
|
|
+ tv_postal.setText("");
|
|
|
+ tv_address.setText("");
|
|
|
+ tv_name.setText("");
|
|
|
+
|
|
|
+
|
|
|
+ if (result == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result.postcode != null) {
|
|
|
+ tv_postal.setText(result.postcode.getText());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result.address != null) {
|
|
|
+ tv_address.setText(result.address.getText());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (result.name != null) {
|
|
|
+ tv_name.setText(result.name.getText());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void takePhotoResult(Bitmap bitmap) {
|
|
|
+ super.takePhotoResult(bitmap);
|
|
|
+ if (bitmap == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 得到裁剪后的图片数据
|
|
|
+ int[] rectData = new int[]{mLeft, mTop, mRight, mBottom};
|
|
|
+ File file = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES) + "/" + System.currentTimeMillis() + ".jpg");
|
|
|
+ if (tv_orientation.getVisibility() == View.VISIBLE) {
|
|
|
+ mOperationPresenter.startOcrFromBitmap(this, bitmap, rectData, file, mCurrentScene, pcv_scan.isLandscape());
|
|
|
+ } else {
|
|
|
+ mOperationPresenter.startOcrFromBitmap(this, bitmap, rectData, file, mCurrentScene, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|