詹子聪 5 vuotta sitten
vanhempi
commit
9053eb85fe

+ 4 - 0
app/build.gradle

@@ -37,6 +37,10 @@ dependencies {
     implementation project(path: ':mvp')
     implementation project(path: ':adapter')
 
+    // 权限申请
     //implementation 'com.github.tbruyelle:rxpermissions:0.12'
     compile 'com.tbruyelle.rxpermissions2:rxpermissions:0.8.1@aar'
+
+    // 裁剪图片
+    compile 'com.isseiaoki:simplecropview:1.1.8'
 }

+ 4 - 0
app/src/main/java/com/miekir/ocr/CameraActivity.java

@@ -28,8 +28,10 @@ import android.util.SparseIntArray;
 import android.view.Surface;
 import android.view.TextureView;
 import android.view.View;
+import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.FrameLayout;
+import android.widget.ImageView;
 import android.widget.Toast;
 
 import androidx.annotation.NonNull;
@@ -156,6 +158,8 @@ public class CameraActivity extends BaseCameraActivity {
                 takePicture();
             }
         });
+
+
     }
 
 

+ 79 - 0
app/src/main/java/com/miekir/ocr/view/rect/CustomDrawable.java

@@ -0,0 +1,79 @@
+package com.miekir.ocr.view.rect;
+
+import android.graphics.Canvas;
+import android.graphics.ColorFilter;
+import android.graphics.Paint;
+import android.graphics.Path;
+import android.graphics.PorterDuff;
+import android.graphics.PorterDuffXfermode;
+import android.graphics.drawable.Drawable;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+/**
+ * 支持中间出现透明区域的drawable
+ * 通过{@link #setSrcPath(Path)}设定透明区域的形状
+ * Created by juan on 2018/07/20.
+ */
+public class CustomDrawable extends Drawable {
+    private Paint srcPaint;
+    private Path srcPath = new Path();
+
+    private Drawable innerDrawable;
+
+
+    public CustomDrawable(Drawable innerDrawable) {
+        this.innerDrawable = innerDrawable;
+        srcPath.addRect(100, 100, 200, 200, Path.Direction.CW);
+        srcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
+        srcPaint.setColor(0xffffffff);
+    }
+
+    /**
+     * 设置内部透明的部分
+     *
+     * @param srcPath
+     */
+    public void setSrcPath(Path srcPath) {
+        this.srcPath = srcPath;
+    }
+
+    @Override
+    public void draw(@NonNull Canvas canvas) {
+        innerDrawable.setBounds(getBounds());
+        if (srcPath == null || srcPath.isEmpty()) {
+            innerDrawable.draw(canvas);
+        } else {
+            //将绘制操作保存到新的图层,因为图像合成是很昂贵的操作,将用到硬件加速,这里将图像合成的处理放到离屏缓存中进行
+            int saveCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), srcPaint, Canvas.ALL_SAVE_FLAG);
+
+            //dst 绘制目标图
+            innerDrawable.draw(canvas);
+
+            //设置混合模式
+            srcPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
+            //src 绘制源图
+            canvas.drawPath(srcPath, srcPaint);
+            //清除混合模式
+            srcPaint.setXfermode(null);
+            //还原画布
+            canvas.restoreToCount(saveCount);
+        }
+    }
+
+    @Override
+    public void setAlpha(int alpha) {
+        innerDrawable.setAlpha(alpha);
+    }
+
+    @Override
+    public void setColorFilter(@Nullable ColorFilter colorFilter) {
+        innerDrawable.setColorFilter(colorFilter);
+    }
+
+    @Override
+    public int getOpacity() {
+        return innerDrawable.getOpacity();
+    }
+}

+ 97 - 0
app/src/main/java/com/miekir/ocr/view/rect/CustomLayout.java

@@ -0,0 +1,97 @@
+package com.miekir.ocr.view.rect;
+
+import android.annotation.SuppressLint;
+import android.content.Context;
+import android.graphics.Path;
+import android.util.AttributeSet;
+import android.view.View;
+import android.widget.FrameLayout;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import com.miekir.ocr.R;
+
+/**
+ * 根据layout中子View的位置,确定局部透明区域
+ * Created by juan on 2018/07/20.
+ *
+ * <!--灰色遮罩-->
+ * <com.miekir.ocr.view.rect.CustomLayout
+ *     android:id="@+id/layout"
+ *     android:layout_width="match_parent"
+ *     android:layout_height="match_parent"
+ *     android:background="@color/black_transparent">
+ *
+ *     <!-- 根据这个子View所在的位置,计算出透明矩形的位置 -->
+ *     <FrameLayout
+ *         android:id="@+id/iv_scan"
+ *         android:layout_width="200dp"
+ *         android:layout_height="200dp" />
+ *
+ * </com.miekir.ocr.view.rect.CustomLayout>
+ */
+public class CustomLayout extends FrameLayout {
+
+    private Context mContext;
+    private CustomDrawable background;
+
+    public CustomLayout(@NonNull Context context) {
+        super(context);
+        initView(context, null, 0);
+    }
+
+    public CustomLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
+        super(context, attrs);
+        this.mContext=context;
+        initView(context, attrs, 0);
+    }
+
+    public CustomLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+        initView(context, attrs, defStyleAttr);
+    }
+
+    @SuppressLint("NewApi")
+    private void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
+        background = new CustomDrawable(getBackground());
+        setBackground(background);
+    }
+
+    @Override
+    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+        super.onLayout(changed, left, top, right, bottom);
+        resetBackgroundHoleArea();
+    }
+
+    @SuppressLint("NewApi")
+    private void resetBackgroundHoleArea() {
+        Path path = null;
+        // 以子View为范围构造需要透明显示的区域
+        View view = findViewById(R.id.iv_scan);
+        if (view != null) {
+            path = new Path();
+            // 矩形透明区域
+            path.addRoundRect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), dp2Px(mContext,10), dp2Px(mContext,10),Path.Direction.CW);
+        }
+        if (path != null) {
+            background.setSrcPath(path);
+        }
+
+        /*FrameLayout iv_scan = findViewById(R.id.iv_scan);
+        iv_scan.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                ViewGroup.LayoutParams params = iv_scan.getLayoutParams();
+                params.width = 123;
+                params.height = 300;
+                iv_scan.requestLayout();
+            }
+        });*/
+    }
+
+    public int dp2Px(Context context, float dp) {
+        final float scale = context.getResources().getDisplayMetrics().density;
+        return (int) (dp * scale + 0.5f);
+    }
+}

+ 38 - 17
app/src/main/res/layout/activity_camera.xml

@@ -42,6 +42,30 @@
             android:layout_width="wrap_content"
             android:layout_height="match_parent" />
 
+        <com.isseiaoki.simplecropview.CropImageView
+            xmlns:custom="http://schemas.android.com/apk/res-auto"
+            android:id="@+id/cropImageView"
+            android:layout_weight="1"
+            android:paddingTop="16dp"
+            android:paddingBottom="16dp"
+            android:paddingLeft="16dp"
+            android:paddingRight="16dp"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            custom:scv_crop_mode="fit_image"
+            custom:scv_background_color="@color/black_transparent"
+            custom:scv_handle_color="@color/colorAccent"
+            custom:scv_guide_color="@color/colorAccent"
+            custom:scv_overlay_color="@color/black_transparent"
+            custom:scv_frame_color="@color/colorAccent"
+            custom:scv_handle_size="14dp"
+            custom:scv_touch_padding="8dp"
+            custom:scv_handle_show_mode="show_always"
+            custom:scv_guide_show_mode="show_always"
+            custom:scv_min_frame_size="50dp"
+            custom:scv_frame_stroke_weight="1dp"
+            custom:scv_guide_stroke_weight="1dp"/>
+
         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
@@ -95,7 +119,7 @@
                             android:layout_height="wrap_content"
                             android:text="邮政编码"
                             android:textColor="@color/white"
-                            android:textSize="@dimen/size_text_title"/>
+                            android:textSize="@dimen/size_text_title" />
                     </LinearLayout>
 
                     <Space
@@ -123,7 +147,7 @@
                             android:layout_height="wrap_content"
                             android:text="住址"
                             android:textColor="@color/white"
-                            android:textSize="@dimen/size_text_title"/>
+                            android:textSize="@dimen/size_text_title" />
                     </LinearLayout>
 
                     <Space
@@ -151,7 +175,7 @@
                             android:layout_height="wrap_content"
                             android:text="姓名"
                             android:textColor="@color/white"
-                            android:textSize="@dimen/size_text_title"/>
+                            android:textSize="@dimen/size_text_title" />
                     </LinearLayout>
 
                     <Space
@@ -179,7 +203,7 @@
                             android:layout_height="wrap_content"
                             android:text="全范围"
                             android:textColor="@color/white"
-                            android:textSize="@dimen/size_text_title"/>
+                            android:textSize="@dimen/size_text_title" />
                     </LinearLayout>
 
                     <Space
@@ -192,18 +216,19 @@
                 <LinearLayout
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
-                    android:orientation="horizontal"
-                    android:gravity="center_vertical">
+                    android:gravity="center_vertical"
+                    android:orientation="horizontal">
 
                     <RelativeLayout
                         android:layout_width="0dp"
                         android:layout_height="wrap_content"
                         android:layout_weight="1">
+
                         <ImageView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
-                            android:src="@mipmap/button_album"
-                            android:layout_centerInParent="true"/>
+                            android:layout_centerInParent="true"
+                            android:src="@mipmap/button_album" />
                     </RelativeLayout>
 
                     <FrameLayout
@@ -214,13 +239,14 @@
                         <ImageView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
-                            android:src="@mipmap/button_take"
-                            android:layout_gravity="center"/>
+                            android:layout_gravity="center"
+                            android:src="@mipmap/button_take" />
+
                         <ImageView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
-                            android:src="@mipmap/button_take_out"
-                            android:layout_gravity="center"/>
+                            android:layout_gravity="center"
+                            android:src="@mipmap/button_take_out" />
                     </FrameLayout>
 
                     <Space
@@ -231,11 +257,6 @@
             </LinearLayout>
         </LinearLayout>
 
-        <!--灰色遮罩-->
-        <FrameLayout
-            android:layout_width="match_parent"
-            android:layout_height="match_parent">
 
-        </FrameLayout>
     </FrameLayout>
 </LinearLayout>

+ 20 - 0
app/src/main/res/layout/view_crop.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+    <!--灰色遮罩-->
+    <com.miekir.ocr.view.rect.CustomLayout
+        android:id="@+id/layout"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:background="@color/black_transparent">
+
+        <!-- 根据这个子View所在的位置,计算出透明矩形的位置 -->
+        <FrameLayout
+            android:id="@+id/iv_scan"
+            android:layout_width="200dp"
+            android:layout_height="200dp" />
+
+    </com.miekir.ocr.view.rect.CustomLayout>
+</LinearLayout>