詹子聪 5 年 前
コミット
b436db3698

+ 20 - 9
app/src/main/AndroidManifest.xml

@@ -3,29 +3,39 @@
     xmlns:tools="http://schemas.android.com/tools"
     package="com.miekir.ocr">
 
-    <uses-permission android:name="android.permission.INTERNET"/>
-    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
-    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
+    <uses-permission android:name="android.permission.INTERNET" />
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
 
     <uses-permission android:name="android.permission.CAMERA" />
     <uses-feature android:name="android.hardware.camera2.full" />
 
+    <uses-permission android:name="android.permission.FLASHLIGHT" />
+    <permission
+        android:name="android.permission.FLASHLIGHT"
+        android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
+        android:protectionLevel="normal" />
+    <uses-feature
+        android:name="android.hardware.camera.flash"
+        android:required="false" />
+
     <application
+        android:name=".OCRApplication"
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
+        android:networkSecurityConfig="@xml/network"
+        android:requestLegacyExternalStorage="true"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/AppTheme"
-        tools:ignore="GoogleAppIndexingWarning"
-        android:name=".OCRApplication"
-        android:requestLegacyExternalStorage="true"
-        android:networkSecurityConfig="@xml/network">
+        tools:ignore="GoogleAppIndexingWarning">
         <!--targetSdk大于等于29会导致访问/storage/emulated/0/Pictures/ 权限被拒绝
         open failed: EACCES (Permission denied),需要加上requestLegacyExternalStorage-->
 
-        <activity android:name=".PermissionActivity"
+        <activity
+            android:name=".PermissionActivity"
             android:screenOrientation="portrait">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
@@ -33,7 +43,8 @@
             </intent-filter>
         </activity>
 
-        <activity android:name=".ui.OperationActivity"
+        <activity
+            android:name=".ui.OperationActivity"
             android:screenOrientation="portrait" />
     </application>
 

+ 60 - 2
app/src/main/java/com/miekir/ocr/ui/CameraActivity.java

@@ -30,6 +30,7 @@ import android.util.Size;
 import android.util.SparseIntArray;
 import android.view.Surface;
 import android.view.TextureView;
+import android.widget.ImageView;
 
 import androidx.annotation.NonNull;
 
@@ -82,6 +83,8 @@ public class CameraActivity extends BaseCameraActivity {
     private HandlerThread mBackgroundThread;
 
     private AutoFitTextureView textureView;
+    private boolean mIsFlashOpen = false;
+    private ImageView iv_flash;
 
     private Semaphore mCameraOpenCloseLock = new Semaphore(1);
 
@@ -151,6 +154,15 @@ public class CameraActivity extends BaseCameraActivity {
     public void initViews(Bundle savedInstanceState) {
         textureView = findViewById(R.id.textureView);
         textureView.setSurfaceTextureListener(textureListener);
+        iv_flash = findViewById(R.id.iv_flash);
+        iv_flash.setOnClickListener(v -> {
+           mIsFlashOpen = !mIsFlashOpen;
+           if (mIsFlashOpen) {
+               iv_flash.setImageResource(R.mipmap.flash_open);
+           } else {
+               iv_flash.setImageResource(R.mipmap.flash_close);
+           }
+        });
     }
 
 
@@ -279,6 +291,7 @@ public class CameraActivity extends BaseCameraActivity {
             // 解决预览太暗的问题
             captureRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, getRange());
             captureRequestBuilder.addTarget(surface);
+
             cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
                 @Override
                 public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
@@ -306,6 +319,7 @@ public class CameraActivity extends BaseCameraActivity {
             Log.e("tag", "updatePreview error, return");
         }
         captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
+        //justFlashLight(captureRequestBuilder);
         try {
             cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
         } catch (CameraAccessException e) {
@@ -402,8 +416,6 @@ public class CameraActivity extends BaseCameraActivity {
             // Orientation
             int rotation = getWindowManager().getDefaultDisplay().getRotation();
             captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
-
-
             reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
 
             final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@@ -416,6 +428,7 @@ public class CameraActivity extends BaseCameraActivity {
 
             };
 
+            justFlashLight(captureBuilder);
             cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
                 @Override
                 public void onConfigured(CameraCaptureSession session) {
@@ -435,6 +448,51 @@ public class CameraActivity extends BaseCameraActivity {
         }
     }
 
+    private void justFlashLight(CaptureRequest.Builder captureBuilder) {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+            if (mIsFlashOpen) {
+                captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
+                captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
+            } else {
+                captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
+                captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);
+            }
+
+            // 只有摄像头关闭的时候,才能用以下的方法
+            /*try {
+                //获取CameraManager
+                CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
+                //获取当前手机所有摄像头设备ID
+                String[] ids  = mCameraManager.getCameraIdList();
+                for (String id : ids) {
+                    CameraCharacteristics c = mCameraManager.getCameraCharacteristics(id);
+                    //查询该摄像头组件是否包含闪光灯
+                    Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
+                    Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
+                    if (flashAvailable != null && flashAvailable
+                            && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
+                        //打开或关闭手电筒
+                        mCameraManager.setTorchMode(id, true);
+                    }
+                }
+            } catch (CameraAccessException e) {
+                e.printStackTrace();
+            }*/
+        } else {
+            if (mIsFlashOpen) {
+                // 拍完关闭闪光灯
+                captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_SINGLE);
+                //captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
+
+            } else {
+                captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
+                //captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
+            }
+        }
+
+
+    }
+
     ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
         @Override
         public void onImageAvailable(ImageReader reader) {

+ 13 - 3
app/src/main/res/layout/activity_camera.xml

@@ -26,12 +26,20 @@
             android:src="@mipmap/logo" />
 
         <TextView
-            android:layout_width="match_parent"
+            android:layout_width="0dp"
+            android:layout_weight="1"
             android:layout_height="wrap_content"
-            android:gravity="end"
+            android:gravity="center"
             android:text="@string/app_name"
             android:textColor="@color/white"
-            android:textSize="18sp" />
+            android:textSize="18sp"
+            android:visibility="invisible"/>
+
+        <ImageView
+            android:id="@+id/iv_flash"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:src="@mipmap/flash_close"/>
     </LinearLayout>
 
 
@@ -184,6 +192,8 @@
                         android:id="@+id/tv_orientation"
                         android:layout_width="0dp"
                         android:layout_height="wrap_content"
+                        android:layout_marginStart="@dimen/margin_default"
+                        android:layout_marginEnd="@dimen/margin_default"
                         android:layout_weight="1"
                         android:gravity="center"
                         android:text="@string/landscape"

+ 2 - 2
app/src/main/res/layout/dialog_result.xml

@@ -78,7 +78,7 @@
             android:textColor="@color/white"
             android:paddingTop="@dimen/margin_default"
             android:paddingBottom="@dimen/margin_default"
-            android:textSize="@dimen/size_text_title"
+            android:textSize="@dimen/size_text_p"
             android:background="@drawable/selector_copy"
             android:text="@string/copy"/>
 
@@ -96,7 +96,7 @@
             android:gravity="center"
             android:paddingTop="@dimen/margin_default"
             android:paddingBottom="@dimen/margin_default"
-            android:textSize="@dimen/size_text_title"
+            android:textSize="@dimen/size_text_p"
             android:background="@drawable/selector_cancel"
             android:text="@string/cancel"/>
     </LinearLayout>

BIN
app/src/main/res/mipmap-xxxhdpi/flash_close.png


BIN
app/src/main/res/mipmap-xxxhdpi/flash_open.png


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

@@ -7,6 +7,7 @@
     <dimen name="width_focus">7dp</dimen>
 
 
+    <dimen name="size_text_p">14sp</dimen>
     <dimen name="size_text_title">16sp</dimen>
     <dimen name="size_text_title_p">18sp</dimen>
 </resources>