Bladeren bron

引入CropView

詹子聪 5 jaren geleden
bovenliggende
commit
cf67f03054

+ 6 - 0
app/src/main/java/com/miekir/ocr/OCRApplication.java

@@ -13,9 +13,15 @@ import com.yc.toollib.crash.CrashListener;
  * Description:
  */
 public class OCRApplication extends Application {
+    private static OCRApplication sInstance;
+    public static OCRApplication getsInstance() {
+        return sInstance;
+    }
+
     @Override
     public void onCreate() {
         super.onCreate();
+        sInstance = this;
 
         //RxActivityResult.register(this);
 

File diff suppressed because it is too large
+ 1894 - 0
app/src/main/java/com/miekir/ocr/widget/cropview/CropView.java


+ 58 - 0
app/src/main/java/com/miekir/ocr/widget/cropview/SizeUtils.java

@@ -0,0 +1,58 @@
+package com.miekir.ocr.widget.cropview;
+
+import com.miekir.ocr.OCRApplication;
+
+/**
+ * <pre>
+ *     author: Blankj
+ *     blog  : http://blankj.com
+ *     time  : 2016/08/02
+ *     desc  : utils about size
+ * </pre>
+ */
+public final class SizeUtils {
+
+    /**
+     * Value of dp to value of px.
+     *
+     * @param dpValue The value of dp.
+     * @return value of px
+     */
+    public static int dp2px(final float dpValue) {
+        final float scale = OCRApplication.getsInstance().getResources().getDisplayMetrics().density;
+        return (int) (dpValue * scale + 0.5f);
+    }
+
+    /**
+     * Value of px to value of dp.
+     *
+     * @param pxValue The value of px.
+     * @return value of dp
+     */
+    public static int px2dp(final float pxValue) {
+        final float scale = OCRApplication.getsInstance().getResources().getDisplayMetrics().density;
+        return (int) (pxValue / scale + 0.5f);
+    }
+
+    /**
+     * Value of sp to value of px.
+     *
+     * @param spValue The value of sp.
+     * @return value of px
+     */
+    public static int sp2px(final float spValue) {
+        final float fontScale = OCRApplication.getsInstance().getResources().getDisplayMetrics().scaledDensity;
+        return (int) (spValue * fontScale + 0.5f);
+    }
+
+    /**
+     * Value of px to value of sp.
+     *
+     * @param pxValue The value of px.
+     * @return value of sp
+     */
+    public static int px2sp(final float pxValue) {
+        final float fontScale = OCRApplication.getsInstance().getResources().getDisplayMetrics().scaledDensity;
+        return (int) (pxValue / fontScale + 0.5f);
+    }
+}

+ 63 - 0
app/src/main/java/com/miekir/ocr/widget/cropview/ThreadPoolManagerUtils.java

@@ -0,0 +1,63 @@
+package com.miekir.ocr.widget.cropview;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * <p>线程池管理类</p>
+ *
+ * @time Created by 2018/6/26 17:54
+ */
+public class ThreadPoolManagerUtils {
+    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
+    // 核心线程数
+    private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
+
+    // 线程池最大线程数
+    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
+    // 非核心线程闲置时超时0s
+    private static final long KEEP_ALIVE = 1L;
+
+    // 确保该类只有一个实例对象
+    private static ThreadPoolManagerUtils sInstance;
+    // 线程池的对象
+    private ThreadPoolExecutor executor;
+
+    private ThreadPoolManagerUtils() {
+    }
+
+    public synchronized static ThreadPoolManagerUtils getInstance() {
+        if (sInstance == null) {
+            sInstance = new ThreadPoolManagerUtils();
+        }
+        return sInstance;
+    }
+
+    /**
+     * 使用线程池,线程池中线程的创建完全是由线程池自己来维护的,我们不需要创建任何的线程
+     *
+     * @param runnable 在线程池里面运行的线程
+     */
+    public void execute(Runnable runnable) {
+        if (executor == null) {
+            executor =
+                new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS,
+                    new SynchronousQueue<Runnable>(), Executors.defaultThreadFactory(),
+                    new ThreadPoolExecutor.AbortPolicy());
+        }
+        executor.execute(runnable);// 添加任务
+    }
+
+    /**
+     * 移除指定的线程
+     *
+     * @param runnable 指定的线程
+     */
+    public void cancel(Runnable runnable) {
+        if (runnable != null) {
+            executor.getQueue().remove(runnable);// 移除任务
+        }
+    }
+}

+ 55 - 0
cameraview/src/main/res/values/attrs.xml

@@ -175,4 +175,59 @@
         <attr name="layout_drawOnVideoSnapshot" format="boolean"/>
 
     </declare-styleable>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+    <declare-styleable name="CropView">
+        <attr name="img_src" format="reference" />
+        <attr name="crop_mode">
+            <enum name="fit_image" value="0" />
+            <enum name="ratio_2_3" value="1" />
+            <enum name="ratio_3_2" value="2" />
+            <enum name="ratio_4_3" value="3" />
+            <enum name="ratio_3_4" value="4" />
+            <enum name="square" value="5" />
+            <enum name="ratio_16_9" value="6" />
+            <enum name="ratio_9_16" value="7" />
+            <enum name="free" value="8" />
+
+        </attr>
+        <attr name="background_color" format="reference|color" />
+        <attr name="overlay_color" format="reference|color" />
+        <attr name="frame_color" format="reference|color" />
+        <attr name="handle_color" format="reference|color" />
+        <attr name="handle_width" format="dimension" />
+        <attr name="handle_size" format="dimension" />
+        <attr name="guide_color" format="reference|color" />
+        <attr name="guide_show_mode">
+            <enum name="show_always" value="1" />
+            <enum name="show_on_touch" value="2" />
+            <enum name="not_show" value="3" />
+        </attr>
+        <attr name="handle_show_mode">
+            <enum name="show_always" value="1" />
+            <enum name="show_on_touch" value="2" />
+            <enum name="not_show" value="3" />
+        </attr>
+        <attr name="touch_padding" format="dimension" />
+        <attr name="min_frame_size" format="dimension" />
+        <attr name="frame_stroke_weight" format="dimension" />
+        <attr name="guide_stroke_weight" format="dimension" />
+        <attr name="crop_enabled" format="boolean" />
+        <attr name="initial_frame_scale" format="float" />
+    </declare-styleable>
 </resources>