Просмотр исходного кода

1.相机权限2.相机进度条

everywindchase 4 месяцев назад
Родитель
Сommit
62dbe2f643

+ 44 - 11
app/src/main/java/jx/cn/escort/ui/activity/ActionTaskHandActivity.kt

@@ -13,6 +13,8 @@ import android.content.pm.PackageManager.PERMISSION_GRANTED
 import android.net.Uri
 import android.os.Build
 import android.os.Bundle
+import android.os.Handler
+import android.os.Looper
 import android.provider.Settings
 import android.util.Log
 import android.util.Size
@@ -30,6 +32,7 @@ import androidx.constraintlayout.widget.ConstraintLayout
 import androidx.core.app.ActivityCompat
 import androidx.core.content.ContextCompat
 import androidx.lifecycle.LifecycleOwner
+import androidx.lifecycle.lifecycleScope
 import com.xuexiang.xui.XUI
 import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
 import jx.cn.escort.R
@@ -37,6 +40,8 @@ import jx.cn.escort.databinding.ActivityActionTaskHandleBinding
 import jx.cn.escort.extension.ExtensionBinding.Companion.binding
 import jx.cn.escort.ui.widget.CircleProgressView
 import jx.cn.escort.utils.ActivityUtils
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
 import org.koin.core.component.KoinComponent
 import org.koin.core.component.inject
 import java.util.concurrent.ExecutorService
@@ -57,6 +62,7 @@ class ActionTaskHandActivity : AppCompatActivity(), KoinComponent {
     private var imageCamera: ImageCapture? = null
     private lateinit var camera: Camera //相机对象
     private lateinit   var imageAnalysis: ImageAnalysis
+    private lateinit  var circle:CircleProgressView
 
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -77,8 +83,9 @@ class ActionTaskHandActivity : AppCompatActivity(), KoinComponent {
                 val li = LayoutInflater.from(this@ActionTaskHandActivity)
                 val inflate = li.inflate(jx.cn.escort.R.layout.layout_task_face,null)
                 val cameralayoutc = inflate.findViewById<ConstraintLayout>(jx.cn.escort.R.id.camera_layoutc)
-                var circle:View= CircleProgressView(this@ActionTaskHandActivity)
-                //circle.id= androidx.camera.core.R.id.faceCircle
+                circle= CircleProgressView(this@ActionTaskHandActivity)
+               // circle.id= androidx.camera.core.R.id.faceCircle
+                circle.bringToFront()
                 cameralayoutc.addView(circle)
                 val dialog = MaterialDialog.Builder(this@ActionTaskHandActivity)
                     .customView(inflate, true)
@@ -87,7 +94,7 @@ class ActionTaskHandActivity : AppCompatActivity(), KoinComponent {
                             if( PackageManager.PERMISSION_GRANTED!= ContextCompat.checkSelfPermission(this@ActionTaskHandActivity    , Manifest.permission.CAMERA)){
 
 
-                                ActivityCompat.requestPermissions(this@ActionTaskHandActivity , arrayOf(Manifest.permission.CAMERA),100)
+                                ActivityCompat.requestPermissions(this@ActionTaskHandActivity  , arrayOf(Manifest.permission.CAMERA),100)
 
                                 //打开自定义授权弹窗
                                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
@@ -116,13 +123,23 @@ class ActionTaskHandActivity : AppCompatActivity(), KoinComponent {
                                 //弹窗回调完再打开相机
 
                             }else{
+                              var     mpreview=  inflate.findViewById<PreviewView>(R.id.mPreviewView)
+
                                 //打开相机
-                                runCamera(this@ActionTaskHandActivity,cameralayoutc.findViewById(R.id.mPreviewView) , context = this@ActionTaskHandActivity)
+                               runCamera(this@ActionTaskHandActivity,mpreview , context = this@ActionTaskHandActivity)
                             }
                         }
 
                     })
-                dialog.show()
+                dialog.dismissListener(object:DialogInterface.OnDismissListener{
+                    override fun onDismiss(dialog: DialogInterface?) {
+                        if (preview != null) {
+                            cameraProvider?.unbindAll()
+                            preview = null;
+                        }
+                    }
+
+                }).show()
 //检查手机相机权限
 
 
@@ -185,10 +202,26 @@ class ActionTaskHandActivity : AppCompatActivity(), KoinComponent {
 
 
             imageAnalysis.setAnalyzer(cameraExecutor) { imageProxy ->
-                    Log.d("ActionTask", "图片格式${imageProxy.format}")
+                Log.d("ActionTask", "图片格式${imageProxy.format}")
+
+
+                // 定义一个Runnable,它将被重复执行
+
+                lifecycleScope.launch {
+                    circle.setProgress(60)
+                    delay(5000)
+                    circle.setProgress(80)
+                    delay(5000)
+                    circle.setProgress(100)
+                }
+
+
 
             }
 
+
+
+
             try {
                 cameraProvider?.unbindAll()//先解绑所有用例
                 camera = cameraProvider?.bindToLifecycle(
@@ -217,13 +250,13 @@ class ActionTaskHandActivity : AppCompatActivity(), KoinComponent {
         camera.cameraInfo.cameraState.observe(lifecycleOwner) { value ->
             when (value.type) {
                 CameraState.Type.OPEN         ->{
-                    Log.d("demo", "相机正在打开中" )
+                    Log.d("ActionTask", "相机正在打开中" )
                 }
 
-                CameraState.Type.PENDING_OPEN -> Log.d("demo", "相机待打开" )
-                CameraState.Type.OPENING      -> Log.d("demo", "相机已打开" )
-                CameraState.Type.CLOSING      ->Log.d("demo", "相机关闭中" )
-                CameraState.Type.CLOSED       -> Log.d("demo", "相机已关闭" )
+                CameraState.Type.PENDING_OPEN -> Log.d("ActionTask", "相机待打开" )
+                CameraState.Type.OPENING      -> Log.d("ActionTask", "相机已打开" )
+                CameraState.Type.CLOSING      ->Log.d("ActionTask", "相机关闭中" )
+                CameraState.Type.CLOSED       -> Log.d("ActionTask", "相机已关闭" )
             }
 
         }

+ 1 - 1
app/src/main/java/jx/cn/escort/ui/widget/CircleProgressView.kt

@@ -98,7 +98,7 @@ class CircleProgressView(context: Context) : View(context) {
             mProgressPaint!!.isDither = true
             //设置状态
             mProgressPaint!!.style = Paint.Style.STROKE
-            //头部小圆
+            //头部小圆
             mProgressPaint!!.strokeCap = Paint.Cap.ROUND
         }
 

+ 12 - 18
app/src/main/res/layout/layout_task_face.xml

@@ -17,6 +17,18 @@
      >
 
 
+          <androidx.camera.view.PreviewView
+                  android:id="@+id/mPreviewView"
+                  android:layout_width="260dp"
+                  android:layout_height="268dp"
+                  android:layout_gravity="center"
+                  android:gravity="center"
+                  app:layout_constraintBottom_toBottomOf="parent"
+                  app:layout_constraintHorizontal_bias="0.50"
+                  app:layout_constraintLeft_toLeftOf="parent"
+                  app:layout_constraintRight_toRightOf="parent"
+                  app:layout_constraintTop_toTopOf="parent"
+                  app:layout_constraintVertical_bias="0.50"/>
 
 
 
@@ -27,22 +39,4 @@
      </androidx.constraintlayout.widget.ConstraintLayout>
 
 
-
-
-
-     <androidx.camera.view.PreviewView
-             android:id="@+id/mPreviewView"
-             android:layout_width="100dp"
-             android:layout_height="100dp"
-             android:layout_gravity="center"
-             android:gravity="center"
-             app:layout_constraintBottom_toBottomOf="parent"
-             app:layout_constraintHorizontal_bias="1.0"
-             app:layout_constraintLeft_toLeftOf="parent"
-             app:layout_constraintRight_toRightOf="parent"
-             app:layout_constraintTop_toTopOf="parent"
-             app:layout_constraintVertical_bias="0.0" />
-
-
-
 </FrameLayout>