|
@@ -0,0 +1,231 @@
|
|
|
+package jx.cn.escort.ui.activity
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import android.Manifest
|
|
|
+import android.Manifest.permission.*
|
|
|
+import android.content.Context
|
|
|
+import android.content.DialogInterface
|
|
|
+import android.content.Intent
|
|
|
+import android.content.SharedPreferences
|
|
|
+import android.content.pm.PackageManager
|
|
|
+import android.content.pm.PackageManager.PERMISSION_GRANTED
|
|
|
+import android.net.Uri
|
|
|
+import android.os.Build
|
|
|
+import android.os.Bundle
|
|
|
+import android.provider.Settings
|
|
|
+import android.util.Log
|
|
|
+import android.util.Size
|
|
|
+import android.view.LayoutInflater
|
|
|
+import android.view.View
|
|
|
+import android.widget.Toast
|
|
|
+import androidx.appcompat.app.AppCompatActivity
|
|
|
+import androidx.camera.core.*
|
|
|
+import androidx.camera.core.ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888
|
|
|
+import androidx.camera.core.resolutionselector.ResolutionSelector
|
|
|
+import androidx.camera.core.resolutionselector.ResolutionStrategy
|
|
|
+import androidx.camera.lifecycle.ProcessCameraProvider
|
|
|
+import androidx.camera.view.PreviewView
|
|
|
+import androidx.constraintlayout.widget.ConstraintLayout
|
|
|
+import androidx.core.app.ActivityCompat
|
|
|
+import androidx.core.content.ContextCompat
|
|
|
+import androidx.lifecycle.LifecycleOwner
|
|
|
+import com.xuexiang.xui.XUI
|
|
|
+import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog
|
|
|
+import jx.cn.escort.R
|
|
|
+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 org.koin.core.component.KoinComponent
|
|
|
+import org.koin.core.component.inject
|
|
|
+import java.util.concurrent.ExecutorService
|
|
|
+import java.util.concurrent.Executors
|
|
|
+
|
|
|
+
|
|
|
+class ActionTaskHandActivity : AppCompatActivity(), KoinComponent {
|
|
|
+
|
|
|
+ private val mBinding: ActivityActionTaskHandleBinding by binding<ActivityActionTaskHandleBinding>()
|
|
|
+
|
|
|
+ private val mContext: Context by inject()
|
|
|
+
|
|
|
+ private val defaultSharedPreferences: SharedPreferences = androidx.preference.PreferenceManager.getDefaultSharedPreferences(mContext)
|
|
|
+
|
|
|
+ private lateinit var cameraExecutor: ExecutorService
|
|
|
+ private var cameraProvider: ProcessCameraProvider? = null
|
|
|
+ private var preview: Preview? = null
|
|
|
+ private var imageCamera: ImageCapture? = null
|
|
|
+ private lateinit var camera: Camera //相机对象
|
|
|
+ private lateinit var imageAnalysis: ImageAnalysis
|
|
|
+
|
|
|
+
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
+ XUI.initTheme(this@ActionTaskHandActivity);
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
+ setContentView(mBinding.root)
|
|
|
+ var taskHandleFace=mBinding.taskHandleFace
|
|
|
+ val taskHandleTitle = mBinding.taskHandleTitle
|
|
|
+ taskHandleTitle.setTitle("人脸验证")
|
|
|
+ taskHandleTitle.setLeftClickListener(object:View.OnClickListener{
|
|
|
+ override fun onClick(v: View?) {
|
|
|
+ ActivityUtils.startActivity(this@ActionTaskHandActivity,MainActivity::class.java)
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ taskHandleFace.setOnClickListener(object:View.OnClickListener{
|
|
|
+ override fun onClick(v: View?) {
|
|
|
+ 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
|
|
|
+ cameralayoutc.addView(circle)
|
|
|
+ val dialog = MaterialDialog.Builder(this@ActionTaskHandActivity)
|
|
|
+ .customView(inflate, true)
|
|
|
+ .title("拿起手机刷脸").showListener(object: DialogInterface.OnShowListener{
|
|
|
+ override fun onShow(dialog: DialogInterface?) {
|
|
|
+ if( PackageManager.PERMISSION_GRANTED!= ContextCompat.checkSelfPermission(this@ActionTaskHandActivity , Manifest.permission.CAMERA)){
|
|
|
+
|
|
|
+
|
|
|
+ ActivityCompat.requestPermissions(this@ActionTaskHandActivity , arrayOf(Manifest.permission.CAMERA),100)
|
|
|
+
|
|
|
+ //打开自定义授权弹窗
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
+ // Android 13及以上完整照片和视频访问权限
|
|
|
+ if(ContextCompat.checkSelfPermission(baseContext, READ_MEDIA_IMAGES) == PERMISSION_GRANTED) {
|
|
|
+ ActivityCompat.requestPermissions(this@ActionTaskHandActivity , arrayOf(Manifest.permission.READ_MEDIA_IMAGES), 101)
|
|
|
+ }
|
|
|
+ if(ContextCompat.checkSelfPermission(baseContext, READ_MEDIA_VIDEO) == PERMISSION_GRANTED) {
|
|
|
+ ActivityCompat.requestPermissions(this@ActionTaskHandActivity, arrayOf(Manifest.permission.READ_MEDIA_VIDEO), 102)
|
|
|
+ }
|
|
|
+ if(ContextCompat.checkSelfPermission(baseContext, READ_MEDIA_AUDIO) == PERMISSION_GRANTED) {
|
|
|
+ ActivityCompat.requestPermissions(this@ActionTaskHandActivity , arrayOf(Manifest.permission.READ_MEDIA_AUDIO), 103)
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if(Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
|
|
|
+ // Android 12及以下完整本地读写访问权限
|
|
|
+ if( PackageManager.PERMISSION_GRANTED!= ContextCompat.checkSelfPermission( baseContext , Manifest.permission.READ_EXTERNAL_STORAGE)){
|
|
|
+ ActivityCompat.requestPermissions(this@ActionTaskHandActivity , arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),104)
|
|
|
+ }
|
|
|
+ if( PackageManager.PERMISSION_GRANTED!= ContextCompat.checkSelfPermission( baseContext , Manifest.permission.WRITE_EXTERNAL_STORAGE)){
|
|
|
+ ActivityCompat.requestPermissions(this@ActionTaskHandActivity , arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),105)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //弹窗回调完再打开相机
|
|
|
+
|
|
|
+ }else{
|
|
|
+ //打开相机
|
|
|
+ runCamera(this@ActionTaskHandActivity,cameralayoutc.findViewById(R.id.mPreviewView) , context = this@ActionTaskHandActivity)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ })
|
|
|
+ dialog.show()
|
|
|
+//检查手机相机权限
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
|
|
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
|
|
+ when(requestCode){
|
|
|
+ 100-> {
|
|
|
+ for (index in permissions.indices) {
|
|
|
+ Log.i("ActionTask", "申请权限为:${permissions[index]}" + "申请结果为:${grantResults[index]}")
|
|
|
+ }
|
|
|
+ //Toast.makeText(mContext as AppCompatActivity, "外部读取权限申请成功", Toast.LENGTH_SHORT).show()
|
|
|
+ // ActivityCompat.shouldShowRequestPermissionRationale(this@ActionTaskHandActivity,permissions[0])
|
|
|
+ }
|
|
|
+ else->{
|
|
|
+ //Toast.makeText(mContext as AppCompatActivity, "外部读取权限申请失败", Toast.LENGTH_SHORT).show()
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ fun runCamera(lifecycleOwner: LifecycleOwner, previewView: PreviewView?, context: Context){
|
|
|
+
|
|
|
+
|
|
|
+ val cameraSelector: CameraSelector = CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build()
|
|
|
+
|
|
|
+
|
|
|
+ cameraExecutor = Executors.newSingleThreadExecutor()
|
|
|
+ val cameraProviderFuture = ProcessCameraProvider.getInstance(context)
|
|
|
+ cameraProviderFuture.addListener({ cameraProvider = cameraProviderFuture.get()//获取相机信息
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ val resolutionBuilder = ResolutionSelector
|
|
|
+ .Builder()
|
|
|
+ .setResolutionStrategy(ResolutionStrategy(Size(640, 480), ResolutionStrategy.FALLBACK_RULE_NONE))
|
|
|
+ .build()
|
|
|
+ //预览配置
|
|
|
+ preview = Preview.Builder().setResolutionSelector(resolutionBuilder)
|
|
|
+ .build()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ imageCamera = ImageCapture.Builder()
|
|
|
+ .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
|
|
|
+ .build()
|
|
|
+ imageAnalysis = ImageAnalysis.Builder().setOutputImageFormat(OUTPUT_IMAGE_FORMAT_YUV_420_888)
|
|
|
+ .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
|
|
|
+ //.setTargetResolution(Size(640, 480))
|
|
|
+ .build()
|
|
|
+
|
|
|
+
|
|
|
+ imageAnalysis.setAnalyzer(cameraExecutor) { imageProxy ->
|
|
|
+ Log.d("ActionTask", "图片格式${imageProxy.format}")
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ cameraProvider?.unbindAll()//先解绑所有用例
|
|
|
+ camera = cameraProvider?.bindToLifecycle(
|
|
|
+ lifecycleOwner,
|
|
|
+ cameraSelector,
|
|
|
+ preview,
|
|
|
+ imageCamera,
|
|
|
+ imageAnalysis
|
|
|
+ )!!//绑定用例
|
|
|
+ preview?.setSurfaceProvider(previewView?.surfaceProvider)
|
|
|
+ if(null!=previewView) {
|
|
|
+ observeCameraLife(camera, lifecycleOwner, previewView)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.d("ActionTask","初始化相机异常${e.message}")
|
|
|
+ }
|
|
|
+
|
|
|
+ },ContextCompat.getMainExecutor(context))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ fun observeCameraLife(camera:Camera,lifecycleOwner:LifecycleOwner,previewView:PreviewView){
|
|
|
+ camera.cameraInfo.cameraState.observe(lifecycleOwner) { value ->
|
|
|
+ when (value.type) {
|
|
|
+ CameraState.Type.OPEN ->{
|
|
|
+ Log.d("demo", "相机正在打开中" )
|
|
|
+ }
|
|
|
+
|
|
|
+ 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", "相机已关闭" )
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|