Bläddra i källkod

成功全屏预览与提高亮度亮度

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

+ 40 - 11
app/src/main/java/com/miekir/ocr/CameraActivity.java

@@ -1,6 +1,5 @@
 package com.miekir.ocr;
 
-import android.Manifest;
 import android.annotation.SuppressLint;
 import android.content.Context;
 import android.content.res.Configuration;
@@ -23,12 +22,14 @@ import android.os.Environment;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.util.Log;
+import android.util.Range;
 import android.util.Size;
 import android.util.SparseIntArray;
 import android.view.Surface;
 import android.view.TextureView;
 import android.view.View;
 import android.widget.Button;
+import android.widget.FrameLayout;
 import android.widget.Toast;
 
 import androidx.annotation.NonNull;
@@ -36,7 +37,6 @@ import androidx.annotation.NonNull;
 import com.miekir.ocr.base.BaseCameraActivity;
 import com.miekir.ocr.tool.Utils;
 import com.miekir.ocr.view.AutoFitTextureView;
-import com.tbruyelle.rxpermissions2.RxPermissions;
 
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -54,6 +54,7 @@ import java.util.concurrent.TimeUnit;
 import static android.os.Environment.DIRECTORY_PICTURES;
 
 public class CameraActivity extends BaseCameraActivity {
+    private CameraManager mCameraManager;
 
     private static SparseIntArray ORIENTATIONS = new SparseIntArray();
 
@@ -79,7 +80,7 @@ public class CameraActivity extends BaseCameraActivity {
     private HandlerThread mBackgroundThread;
 
     private AutoFitTextureView textureView;
-    private Button button;
+    private FrameLayout fl_take;
 
     private Semaphore mCameraOpenCloseLock = new Semaphore(1);
 
@@ -144,12 +145,12 @@ public class CameraActivity extends BaseCameraActivity {
     @SuppressLint("CheckResult")
     @Override
     public void initViews(Bundle savedInstanceState) {
-        textureView = (AutoFitTextureView) findViewById(R.id.textureView);
-        button = (Button) findViewById(R.id.button);
+        textureView = findViewById(R.id.textureView);
+        fl_take = findViewById(R.id.fl_take);
 
         textureView.setSurfaceTextureListener(textureListener);
 
-        button.setOnClickListener(new View.OnClickListener() {
+        fl_take.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 takePicture();
@@ -157,16 +158,17 @@ public class CameraActivity extends BaseCameraActivity {
         });
     }
 
+
     private void openCamera(int width, int height) {
-        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
+        mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
         Log.e("tag", "is camera open");
 
         try {
             if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
                 throw new RuntimeException("Time out waiting to lock camera opening.");
             }
-            cameraId = manager.getCameraIdList()[0];
-            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
+            cameraId = mCameraManager.getCameraIdList()[0];
+            CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(cameraId);
             StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
             assert map != null;
 
@@ -235,7 +237,7 @@ public class CameraActivity extends BaseCameraActivity {
                 textureView.setTransform(Utils.configureTransform(width, height, imageDimension, CameraActivity.this));
             }
 
-            manager.openCamera(cameraId, stateCallback, null);
+            mCameraManager.openCamera(cameraId, stateCallback, null);
         } catch (CameraAccessException e) {
             e.printStackTrace();
         } catch (InterruptedException e) {
@@ -244,6 +246,31 @@ public class CameraActivity extends BaseCameraActivity {
         Log.e("tag", "openCamera X");
     }
 
+    private Range<Integer> getRange() {
+        CameraCharacteristics chars = null;
+        try {
+            chars = mCameraManager.getCameraCharacteristics(cameraId);
+            Range<Integer>[] ranges = chars.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
+            Range<Integer> result = null;
+            for (Range<Integer> range : ranges) {
+                int upper = range.getUpper();
+                // 10 - min range upper for my needs
+                if (upper >= 10) {
+                    if (result == null || upper < result.getUpper().intValue()) {
+                        result = range;
+                    }
+                }
+            }
+            if (result == null) {
+                result = ranges[0];
+            }
+            return result;
+        } catch (CameraAccessException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
     protected void createCameraPreview() {
         try {
             SurfaceTexture texture = textureView.getSurfaceTexture();
@@ -251,7 +278,9 @@ public class CameraActivity extends BaseCameraActivity {
             texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
             Surface surface = new Surface(texture);
             captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
-            captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
+            //captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
+            // 解决预览太暗的问题
+            captureRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, getRange());
             captureRequestBuilder.addTarget(surface);
             cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
                 @Override

+ 18 - 1
app/src/main/java/com/miekir/ocr/view/AutoFitTextureView.java

@@ -48,7 +48,8 @@ public class AutoFitTextureView extends TextureView {
 
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+        // 官方demo,使得TextureView在宽高都不超过手机屏幕的情况下最大化显示
+        /*super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         int width = MeasureSpec.getSize(widthMeasureSpec);
         int height = MeasureSpec.getSize(heightMeasureSpec);
         if (0 == mRatioWidth || 0 == mRatioHeight) {
@@ -59,6 +60,22 @@ public class AutoFitTextureView extends TextureView {
             } else {
                 setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
             }
+        }*/
+
+
+        // 让相机预览全屏,让TextureView总是达到最大边界,超出部分不进行预览(实际上还是能拍到的)
+        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+        int width = MeasureSpec.getSize(widthMeasureSpec);
+        int height = MeasureSpec.getSize(heightMeasureSpec);
+        if (0 == mRatioWidth || 0 == mRatioHeight) {
+            setMeasuredDimension(width, height);
+        } else {
+            //注意这里骚操作,替换"小于号"为"大于号"
+            if (width > height * mRatioWidth / mRatioHeight) {
+                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
+            } else {
+                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
+            }
         }
     }
 

+ 5 - 0
app/src/main/res/drawable/shape_circle.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="oval">
+    <solid android:color="@color/colorPrimary" />
+</shape>

+ 224 - 12
app/src/main/res/layout/activity_camera.xml

@@ -8,22 +8,234 @@
 
     <LinearLayout
         android:layout_width="match_parent"
-        android:layout_height="56dp"
+        android:layout_height="wrap_content"
+        android:background="@color/colorPrimary"
+        android:gravity="center_vertical"
+        android:minHeight="64dp"
         android:orientation="horizontal"
-        android:background="@color/colorPrimary">
+        android:paddingStart="@dimen/margin_default"
+        android:paddingTop="@dimen/margin_ss"
+        android:paddingEnd="@dimen/margin_default"
+        android:paddingBottom="@dimen/margin_ss">
+
+        <ImageView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:src="@mipmap/logo" />
 
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:gravity="end"
+            android:text="@string/app_name"
+            android:textColor="@color/white"
+            android:textSize="18sp" />
     </LinearLayout>
 
 
-    <com.miekir.ocr.view.AutoFitTextureView
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_alignParentTop="true"
-        android:id="@+id/textureView" />
+    <FrameLayout
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
 
-    <Button
-        android:text="Button"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:id="@+id/button" />
+        <com.miekir.ocr.view.AutoFitTextureView
+            android:id="@+id/textureView"
+            android:layout_width="wrap_content"
+            android:layout_height="match_parent" />
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:orientation="vertical">
+
+            <Space
+                android:layout_width="wrap_content"
+                android:layout_height="0dp"
+                android:layout_weight="4" />
+
+
+            <LinearLayout
+                android:layout_width="match_parent"
+                android:layout_height="0dp"
+                android:layout_weight="3"
+                android:orientation="vertical">
+
+                <View
+                    android:layout_width="match_parent"
+                    android:layout_height="0.8dp"
+                    android:background="@color/white" />
+
+                <LinearLayout
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    android:gravity="center_vertical"
+                    android:orientation="horizontal">
+
+                    <Space
+                        android:layout_width="0dp"
+                        android:layout_height="wrap_content"
+                        android:layout_weight="1" />
+
+                    <LinearLayout
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:gravity="center_horizontal"
+                        android:orientation="vertical"
+                        android:paddingTop="@dimen/margin_s"
+                        android:paddingBottom="@dimen/margin_default">
+
+                        <View
+                            android:layout_width="@dimen/width_focus"
+                            android:layout_height="@dimen/width_focus"
+                            android:layout_marginTop="@dimen/margin_s"
+                            android:layout_marginBottom="@dimen/margin_s"
+                            android:background="@drawable/shape_circle" />
+
+                        <TextView
+                            android:layout_width="wrap_content"
+                            android:layout_height="wrap_content"
+                            android:text="邮政编码"
+                            android:textColor="@color/white"
+                            android:textSize="@dimen/size_text_title"/>
+                    </LinearLayout>
+
+                    <Space
+                        android:layout_width="0dp"
+                        android:layout_height="wrap_content"
+                        android:layout_weight="1" />
+
+                    <LinearLayout
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:gravity="center_horizontal"
+                        android:orientation="vertical"
+                        android:paddingTop="@dimen/margin_s"
+                        android:paddingBottom="@dimen/margin_default">
+
+                        <View
+                            android:layout_width="@dimen/width_focus"
+                            android:layout_height="@dimen/width_focus"
+                            android:layout_marginTop="@dimen/margin_s"
+                            android:layout_marginBottom="@dimen/margin_s"
+                            android:background="@drawable/shape_circle" />
+
+                        <TextView
+                            android:layout_width="wrap_content"
+                            android:layout_height="wrap_content"
+                            android:text="住址"
+                            android:textColor="@color/white"
+                            android:textSize="@dimen/size_text_title"/>
+                    </LinearLayout>
+
+                    <Space
+                        android:layout_width="0dp"
+                        android:layout_height="wrap_content"
+                        android:layout_weight="1" />
+
+                    <LinearLayout
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:gravity="center_horizontal"
+                        android:orientation="vertical"
+                        android:paddingTop="@dimen/margin_s"
+                        android:paddingBottom="@dimen/margin_default">
+
+                        <View
+                            android:layout_width="@dimen/width_focus"
+                            android:layout_height="@dimen/width_focus"
+                            android:layout_marginTop="@dimen/margin_s"
+                            android:layout_marginBottom="@dimen/margin_s"
+                            android:background="@drawable/shape_circle" />
+
+                        <TextView
+                            android:layout_width="wrap_content"
+                            android:layout_height="wrap_content"
+                            android:text="姓名"
+                            android:textColor="@color/white"
+                            android:textSize="@dimen/size_text_title"/>
+                    </LinearLayout>
+
+                    <Space
+                        android:layout_width="0dp"
+                        android:layout_height="wrap_content"
+                        android:layout_weight="1" />
+
+                    <LinearLayout
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content"
+                        android:gravity="center_horizontal"
+                        android:orientation="vertical"
+                        android:paddingTop="@dimen/margin_s"
+                        android:paddingBottom="@dimen/margin_default">
+
+                        <View
+                            android:layout_width="@dimen/width_focus"
+                            android:layout_height="@dimen/width_focus"
+                            android:layout_marginTop="@dimen/margin_s"
+                            android:layout_marginBottom="@dimen/margin_s"
+                            android:background="@drawable/shape_circle" />
+
+                        <TextView
+                            android:layout_width="wrap_content"
+                            android:layout_height="wrap_content"
+                            android:text="全范围"
+                            android:textColor="@color/white"
+                            android:textSize="@dimen/size_text_title"/>
+                    </LinearLayout>
+
+                    <Space
+                        android:layout_width="0dp"
+                        android:layout_height="wrap_content"
+                        android:layout_weight="1" />
+                </LinearLayout>
+
+
+                <LinearLayout
+                    android:layout_width="match_parent"
+                    android:layout_height="match_parent"
+                    android:orientation="horizontal"
+                    android:gravity="center_vertical">
+
+                    <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"/>
+                    </RelativeLayout>
+
+                    <FrameLayout
+                        android:id="@+id/fl_take"
+                        android:layout_width="wrap_content"
+                        android:layout_height="wrap_content">
+
+                        <ImageView
+                            android:layout_width="wrap_content"
+                            android:layout_height="wrap_content"
+                            android:src="@mipmap/button_take"
+                            android:layout_gravity="center"/>
+                        <ImageView
+                            android:layout_width="wrap_content"
+                            android:layout_height="wrap_content"
+                            android:src="@mipmap/button_take_out"
+                            android:layout_gravity="center"/>
+                    </FrameLayout>
+
+                    <Space
+                        android:layout_width="0dp"
+                        android:layout_height="wrap_content"
+                        android:layout_weight="1" />
+                </LinearLayout>
+            </LinearLayout>
+        </LinearLayout>
+
+        <!--灰色遮罩-->
+        <FrameLayout
+            android:layout_width="match_parent"
+            android:layout_height="match_parent">
+
+        </FrameLayout>
+    </FrameLayout>
 </LinearLayout>

BIN
app/src/main/res/mipmap-xhdpi/angle_bottom_left.png


BIN
app/src/main/res/mipmap-xhdpi/angle_bottom_right.png


BIN
app/src/main/res/mipmap-xhdpi/angle_top_left.png


BIN
app/src/main/res/mipmap-xhdpi/angle_top_right.png


BIN
app/src/main/res/mipmap-xhdpi/button_album.png


BIN
app/src/main/res/mipmap-xhdpi/button_take.png


BIN
app/src/main/res/mipmap-xhdpi/button_take_out.png


BIN
app/src/main/res/mipmap-xhdpi/logo.png


BIN
app/src/main/res/mipmap-xxhdpi/button_take_big.png


+ 2 - 0
app/src/main/res/values/colors.xml

@@ -3,5 +3,7 @@
     <color name="colorPrimary">#12ABE3</color>
     <color name="colorPrimaryDark">#12ABE3</color>
     <color name="colorAccent">#D81B60</color>
+    <color name="white">#FFFFFF</color>
+    <color name="black_transparent">#33000000</color>
 
 </resources>

+ 10 - 0
app/src/main/res/values/dimens.xml

@@ -0,0 +1,10 @@
+<resources>
+    <!-- Default screen margins, per the Android Design guidelines. -->
+    <dimen name="margin_default">16dp</dimen>
+    <dimen name="margin_ss">4dp</dimen>
+    <dimen name="margin_s">8dp</dimen>
+    <dimen name="width_focus">7dp</dimen>
+
+
+    <dimen name="size_text_title">16sp</dimen>
+</resources>

+ 1 - 1
app/src/main/res/values/strings.xml

@@ -1,3 +1,3 @@
 <resources>
-    <string name="app_name">OCR Demo</string>
+    <string name="app_name">NRI AIOCR</string>
 </resources>