詹子聪 5 år sedan
förälder
incheckning
f1131e2768

+ 6 - 12
app/src/main/java/com/miekir/ocr/ui/camera/otalia/OtaOperationActivity.java

@@ -8,7 +8,6 @@ import android.net.Uri;
 import android.os.Bundle;
 import android.os.Environment;
 import android.text.TextUtils;
-import android.view.MotionEvent;
 import android.view.View;
 import android.widget.LinearLayout;
 import android.widget.TextView;
@@ -27,7 +26,6 @@ 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.miekir.ocr.widget.OnFingerUpListener;
 import com.zhihu.matisse.Matisse;
 import com.zhihu.matisse.MimeType;
 
@@ -57,7 +55,7 @@ public class OtaOperationActivity extends OtaCameraActivity implements CropView.
     private String mLandscapeString;
     private String mPortraitString;
 
-    protected CropView pcv_scan;
+    private CropView pcv_scan;
 
     @Override
     public void initViews(Bundle savedInstanceState) {
@@ -69,16 +67,12 @@ public class OtaOperationActivity extends OtaCameraActivity implements CropView.
         // 裁剪View
         pcv_scan = findViewById(R.id.pcv_scan);
         pcv_scan.setLocationListener(this);
-        OnFingerUpListener listener = new OnFingerUpListener() {
-            @Override
-            public void onFingerUp(final MotionEvent event) {
-                if (camera != null) {
-                    camera.startAutoFocus(event.getX(), event.getY());
-                }
+        // debug,lambda可以回调,类实现接口可以回调,内部类可以回调,但是匿名内部类不行(实际上已经执行),估计是AS4的bug
+        pcv_scan.setOnFingerUpListener(event -> {
+            if (camera != null) {
+                camera.startAutoFocus(event.getX(), event.getY());
             }
-        };
-        pcv_scan.setOnFingerUpListener(listener);
-
+        });
 
         mLandscapeString = getResources().getString(R.string.landscape);
         mPortraitString = getResources().getString(R.string.portrait);

+ 31 - 20
app/src/main/java/com/miekir/ocr/widget/CropView.java

@@ -98,8 +98,8 @@ public class CropView extends View {
                 // 图片放在不同的文件夹下,outHeight会不一样,放在dpi越高的文件夹,值越小
                 minWidth = horizontalOptions.outHeight * 2;
                 minHeight = minWidth;
-                //doLayout(mViewWidth, mViewHeight);
-                setScanArea(SCAN_TYPE_ALL); //height is ready
+                doLayout(mViewWidth, mViewHeight);
+                setScanArea(SCAN_TYPE_ALL);
             }
         });
         //post(() -> );
@@ -134,12 +134,12 @@ public class CropView extends View {
                 break;
             case SCAN_TYPE_ALL:
                 isLandscape = true;
-                coverHeight = getHeight();
+                coverHeight = getHeight() - getPaddingTop() - getPaddingBottom();
                 coverWidth = (int) (coverHeight/1.73);
 
                 // 起始位置
                 sX = (getWidth()*1.0f-coverWidth)/2;
-                sY = 0;
+                sY = getPaddingTop();
                 mFrameRectF = new RectF(sX, sY, sX + coverWidth, sY + coverHeight);
                 break;
             default:
@@ -180,6 +180,13 @@ public class CropView extends View {
 
     @SuppressWarnings("deprecation")
     private void init() {
+        float handleLineLeftX = mFrameRectF.left - mHandleWidth;
+        float handleLineRightX = mFrameRectF.right + mHandleWidth;
+        float handleLineTopY = mFrameRectF.top - mHandleWidth;
+        float handleLineBottomY = mFrameRectF.bottom + mHandleWidth;
+        coverWidth = (int) (handleLineRightX - handleLineLeftX);
+        coverHeight = (int) (handleLineBottomY - handleLineTopY);
+
         WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
         int width = manager.getDefaultDisplay().getWidth();
         int height = manager.getDefaultDisplay().getHeight();
@@ -187,8 +194,8 @@ public class CropView extends View {
         mBitmapCover = makeBitmap(width, height, getResources().getColor(R.color.black_transparent), 0, 0);
         mBitmapRectBlack = makeBitmap(coverWidth, coverHeight, 0xff000000, coverWidth, coverHeight);
 
-        eX = sX + coverWidth;
-        eY = sY + coverHeight;
+        eX = mFrameRectF.right;
+        eY = mFrameRectF.bottom;
 
         xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
 
@@ -251,7 +258,7 @@ public class CropView extends View {
 
     @Override
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
-        doLayout(mViewWidth, mViewHeight);
+        //doLayout(mViewWidth, mViewHeight);
     }
 
     @Override
@@ -730,13 +737,27 @@ public class CropView extends View {
      * @param canvas
      */
     private void drawHandleLines(Canvas canvas) {
-        mFramePaint.setColor(mHandleColor);
-        mFramePaint.setStyle(Paint.Style.FILL);
         // 指导线最边界(最左/最右/最下/最上)的x和y
         float handleLineLeftX = mFrameRectF.left - mHandleWidth;
         float handleLineRightX = mFrameRectF.right + mHandleWidth;
         float handleLineTopY = mFrameRectF.top - mHandleWidth;
         float handleLineBottomY = mFrameRectF.bottom + mHandleWidth;
+
+        // 画遮罩
+        if (mPaint == null) {
+            return;
+        }
+        mPaint.setFilterBitmap(false);
+        int sc = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, Canvas.ALL_SAVE_FLAG);
+        canvas.drawBitmap(mBitmapCover, 0, 0, mPaint);
+        mPaint.setXfermode(xfermode);
+        canvas.drawBitmap(mBitmapRectBlack, handleLineLeftX, handleLineTopY, mPaint);
+        mPaint.setXfermode(null);
+        canvas.restoreToCount(sc);
+
+        //画4个角
+        mFramePaint.setColor(mHandleColor);
+        mFramePaint.setStyle(Paint.Style.FILL);
         // 左上竖向
         RectF ltRectFVertical =
                 new RectF(handleLineLeftX, handleLineTopY, mFrameRectF.left, handleLineTopY + mHandleSize);
@@ -771,17 +792,7 @@ public class CropView extends View {
         canvas.drawRect(rbRectFVertical, mFramePaint);
         canvas.drawRect(rbRectFHorizontal, mFramePaint);
 
-        // 画遮罩
-        if (mPaint == null) {
-            return;
-        }
-        mPaint.setFilterBitmap(false);
-        int sc = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, Canvas.ALL_SAVE_FLAG);
-        canvas.drawBitmap(mBitmapCover, 0, 0, mPaint);
-        mPaint.setXfermode(xfermode);
-        canvas.drawBitmap(mBitmapRectBlack, handleLineLeftX, handleLineTopY, mPaint);
-        mPaint.setXfermode(null);
-        canvas.restoreToCount(sc);
+
     }
 
 

+ 2 - 1
app/src/main/res/layout/activity_otalia_camera.xml

@@ -87,7 +87,8 @@
                 <com.miekir.ocr.widget.CropView
                     android:id="@+id/pcv_scan"
                     android:layout_width="match_parent"
-                    android:layout_height="match_parent"/>
+                    android:layout_height="match_parent"
+                    android:padding="2dp"/>
 
                 <com.miekir.ocr.widget.ScalableImageView
                     android:id="@+id/iv_animation"