123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716 |
- package com.example.chemical.ui.verify
- import android.os.*
- import android.view.LayoutInflater
- import android.view.MotionEvent
- import android.view.View
- import android.widget.TextView
- import com.bumptech.glide.Glide
- import com.bumptech.glide.load.engine.DiskCacheStrategy
- import com.bumptech.glide.request.RequestOptions
- import com.example.chemical.ChemicalApp
- import com.example.chemical.R
- import com.example.chemical.comm.Constants
- import com.example.chemical.databinding.ActivityTwoPersonBinding
- import com.example.chemical.ui.DoubleDialogBean
- import com.example.chemical.ui.common.BaseCountDownActivity
- import com.example.chemical.utils.MediaPlayerHelper
- import com.example.chemical.utils.SharedPreferencesHelper
- import com.example.chemical.utils.UiManager
- import com.example.chemical.weidith.CustomDialog
- import com.example.chemical.weidith.DoublePeopleDialog
- import com.rc.core.log.RcLog
- import com.rc.httpcore.HttpClient
- import com.rc.httpcore.HttpConfig
- import com.rc.httpcore.bean.UserValidationBean
- import com.rc.httpcore.client.ApiRepository
- import com.rc.httpcore.exception.NetException
- import com.rc.httpcore.vo.request.FaceCompareReq
- import io.fotoapparat.Fotoapparat
- import io.fotoapparat.facedetector.processor.FaceDetectorProcessor
- import io.fotoapparat.log.fileLogger
- import io.fotoapparat.log.logcat
- import io.fotoapparat.log.loggers
- import io.fotoapparat.selector.back
- import io.fotoapparat.selector.front
- import retrofit2.HttpException
- import java.io.File
- import java.net.ConnectException
- import java.net.SocketTimeoutException
- import java.text.SimpleDateFormat
- import java.util.*
- //双人-人脸验证
- class TwoPersonActivity : BaseCountDownActivity<ActivityTwoPersonBinding>() {
- //第一个人
- private lateinit var mFotoapparat: Fotoapparat
- //第二个人
- private lateinit var mFotoapparatTwo: Fotoapparat
- //人脸验证时间
- private val totalTimeInMillis: Long = 60 * 1000 // 总时常
- private var count = 1 //认证次数
- private var mChemicalLevel: Int = 0 //管控类型
- private var mTag = 0 //0 新增入库 1 归还废弃空瓶等 2 待入库 3 领用认证需要换api 需要判断不同权限 进行验证通过
- private var mUserId: String? = null //第二个人
- private var mDoorId: String? = null
- private var isLogin = false //是否是登陆人
- private val mDoubleDialogBean = mutableListOf<DoubleDialogBean>() //双人认证 需要显示的
- private val handlerBack = Handler(Looper.getMainLooper())
- private val handlerBackTwo = Handler(Looper.getMainLooper())
- private val countHandler = Handler(Looper.getMainLooper()) //整个页面 60秒后 直接关闭
- override fun createViewBinding() = ActivityTwoPersonBinding.inflate(LayoutInflater.from(this))
- override fun initViews(savedInstanceState: Bundle?) {
- super.initViews(savedInstanceState)
- Constants.AUTHENTICATION = false
- try {
- SharedPreferencesHelper.clearList(this)
- } catch (e: Exception) {
- }
- viewBinding.deptName.text = "${ChemicalApp.confs!!.deptName}-${ChemicalApp.confs!!.roomNum}"
- viewBinding.tvReturn.text = "返回${ChemicalApp.confs!!.backTime}s"
- Glide.with(this)
- .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.confs!!.circularLogo}")
- .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
- .into(viewBinding.image)
- mTag = intent.getIntExtra("mTag", 0)
- mDoorId = intent.getStringExtra("doorId") //柜子id
- mChemicalLevel = intent.getIntExtra("chemicalLevel", 0)
- val intExtra = intent.getIntExtra("hides", 0)
- when (intExtra) {
- 4 -> {
- viewBinding.lint.visibility = View.GONE
- viewBinding.lintTwo.visibility = View.GONE
- }
- 0 -> {
- viewBinding.scan.visibility = View.GONE
- viewBinding.scanTwo.visibility = View.GONE
- }
- 1 -> {
- viewBinding.campus.visibility = View.GONE
- viewBinding.campusTwo.visibility = View.GONE
- }
- }
- val map = mutableMapOf<String, Any>()
- //跳转扫码
- map["chemicalLevel"] = mChemicalLevel //管控类型
- map["doorId"] = "$mDoorId" //柜子id
- map["mTag"] = mTag
- map["hides"] = intExtra //隐藏扫码
- viewBinding.scan.setOnClickListener {
- UiManager.switcher(this, map, ScanCodeTwoActivity::class.java)
- finish()
- }
- viewBinding.campus.setOnClickListener {
- UiManager.switcher(this, map, SwipeCodeTwoActivity::class.java)
- finish()
- }
- viewBinding.scanTwo.setOnClickListener {
- UiManager.switcher(this, map, ScanCodeTwoActivity::class.java)
- finish()
- }
- viewBinding.campusTwo.setOnClickListener {
- UiManager.switcher(this, map, SwipeCodeTwoActivity::class.java)
- finish()
- }
- viewBinding.tvReturn.setOnClickListener {
- finish()
- }
- mFotoapparat = Fotoapparat.with(this)
- .into(viewBinding.cameraView)
- .lensPosition(
- if (Constants.FACE_TAG == 0) {
- front()
- } else {
- back()
- }
- )//front()前置 back 后置
- .frameProcessor(
- FaceDetectorProcessor.with(this)
- .listener { faces ->
- RcLog.info("=========认证中")
- viewBinding.rectanglesView.setRectangles(faces)
- }.build()
- )
- .logger(loggers(logcat(), fileLogger(this)))
- .build()
- MediaPlayerHelper.playRawMp3(
- this@TwoPersonActivity,
- R.raw.diyiren_renlianshibie
- )
- handlerBackTwo.post(countdownRunnableTwo)
- countHandler.postDelayed({
- finish()
- }, totalTimeInMillis) // 15秒后关闭对话框
- }
- override fun onStart() {
- super.onStart()
- mFotoapparat.start()
- }
- override fun onStop() {
- super.onStop()
- try {
- mFotoapparat.stop()
- } catch (e: Exception) {
- }
- try {
- mFotoapparatTwo.stop()
- } catch (e: Exception) {
- }
- }
- private fun toggleCameraTwo() {
- mFotoapparat.stop() //停止第一个 打开第二个
- mFotoapparatTwo.stop()
- mFotoapparatTwo.start()
- }
- //第二个人脸
- private fun faceTwo() {
- count++
- // toggleCameraTwo()
- mFotoapparatTwo = Fotoapparat.with(this)
- .into(viewBinding.cameraViewTwo)
- .lensPosition(
- if (Constants.FACE_TAG == 0) {
- front()
- } else {
- back()
- }
- )//front()前置 back 后置
- .frameProcessor(
- FaceDetectorProcessor.with(this)
- .listener { faces ->
- viewBinding.rectanglesViewTwo.setRectangles(faces)
- }.build()
- )
- .logger(loggers(logcat(), fileLogger(this)))
- .build()
- mFotoapparatTwo.stop()
- mFotoapparatTwo.start()
- }
- private val countdownRunnableTwo = object : Runnable {
- override fun run() {
- // if (timeLeftInSeconds > 0) {
- // mTvView!!.text = "${timeLeftInSeconds}秒后自动返回首页"
- // timeLeftInSeconds--
- // handlerBack.postDelayed(this, 1000)
- // } else {
- // mDialogsAut!!.dismiss()
- // finish()
- // }
- RcLog.info("=======双人刷卡人脸认证中")
- takePicture()
- handlerBackTwo.postDelayed(this, 4500)
- }
- }
- override fun onBackPressed() {
- super.onBackPressed()
- finish()
- }
- override fun cdTime(cd: Int) {
- viewBinding.tvReturn.text = "返回${cd}s"
- }
- private fun callFaceMatchingApi(
- featureData: File,
- ) {
- showLoading("认证中....")
- val param = FaceCompareReq().apply {
- data = null
- subId = ChemicalApp.subjectId
- doorId = mDoorId
- }
- val disposable = ApiRepository.checkUserFaceByPic(featureData, param)
- .subscribe({ data ->
- dismissLoading()
- // if (count == 1) {
- // RcLog.info("第a一个人 $mUserId ${data.userId}")
- // }
- if (mUserId!=null){
- if (mUserId.equals(data.userId)) {
- customDialogView(0, "不能是同一个人")
- // startHandler()
- return@subscribe
- }
- }
- //0 新增入库 1 领用归还废弃空瓶等 2 待入库 需要判断不同权限 进行验证通过
- when (mTag) {
- 0 -> {
- //新增入库
- if (count == 1) {
- if (data.userId == ChemicalApp.userData!!.userId) {
- isLogin = true
- faceTwo()
- customDialog(data.userName, data.faceImg,data.userId)
- var bean =
- DoubleDialogBean(data.userName, data.faceImg, data.userId)
- mDoubleDialogBean.add(bean)
- } else {
- if (mChemicalLevel == 1) {
- //管控 校级管理员或院级管理
- if (data.adminUser == true || data.safeUser == true || data.cabinetAdmin == true) {
- var bean = DoubleDialogBean(
- data.userName,
- data.faceImg,
- data.userId
- )
- mDoubleDialogBean.add(bean)
- customDialog(data.userName, data.faceImg, data.userId)
- faceTwo()
- // if (count == 2) {
- // var bean = DoubleDialogBean(
- // data.userName,
- // data.faceImg,
- // data.userId
- // )
- // mDoubleDialogBean.add(bean)
- // customDialog(data.userName, data.faceImg, data.userId)
- // faceTwo()
- // } else {
- // var bean = DoubleDialogBean(
- // data.userName,
- // data.faceImg,
- // data.userId
- // )
- // mDoubleDialogBean.add(bean)
- // passThrough()
- // }
- } else {
- customDialogView(0, "无权限信息")
- }
- } else {
- if (data.adminUser == true || data.safeUser == true || data.cabinetAdmin == true) {
- faceTwo()
- customDialog(data.userName, data.faceImg, data.userId)
- var bean = DoubleDialogBean(
- data.userName,
- data.faceImg,
- data.userId
- )
- mDoubleDialogBean.add(bean)
- // if (count == 2) {
- // faceTwo()
- // customDialog(data.userName, data.faceImg, data.userId)
- // var bean = DoubleDialogBean(
- // data.userName,
- // data.faceImg,
- // data.userId
- // )
- // mDoubleDialogBean.add(bean)
- // } else {
- // var bean = DoubleDialogBean(
- // data.userName,
- // data.faceImg,
- // data.userId
- // )
- // mDoubleDialogBean.add(bean)
- // passThrough()
- // }
- } else {
- customDialogView(0, "无权限信息")
- }
- }
- }
- } else {
- if (isLogin) {
- //已经有登陆人
- if (mChemicalLevel == 1) {
- //管控 校级管理员或院级管理
- if (data.adminUser == true || data.safeUser == true || data.cabinetAdmin == true) {
- var bean = DoubleDialogBean(
- data.userName,
- data.faceImg,
- data.userId
- )
- mDoubleDialogBean.add(bean)
- passThrough()
- } else {
- customDialogView(0, "无权限信息")
- }
- } else {
- if (data.adminUser == true || data.safeUser == true || data.cabinetAdmin == true) {
- var bean = DoubleDialogBean(
- data.userName,
- data.faceImg,
- data.userId
- )
- mDoubleDialogBean.add(bean)
- passThrough()
- } else {
- customDialogView(0, "无权限信息")
- }
- }
- } else {
- if (data.userId == ChemicalApp.userData!!.userId) {
- var bean =
- DoubleDialogBean(
- data.userName,
- data.faceImg,
- data.userId
- )
- mDoubleDialogBean.add(bean)
- passThrough()
- } else {
- customDialogView(0, "无权限信息")
- }
- }
- }
- }
- 1 -> { //归还业务 标签管理
- if (count == 1) {
- if (data.userId == ChemicalApp.userData!!.userId) {
- isLogin = true
- faceTwo()
- customDialog(data.userName, data.faceImg, data.userId)
- var bean =
- DoubleDialogBean(data.userName, data.faceImg, data.userId)
- mDoubleDialogBean.add(bean)
- } else {
- obtainCertification(data)
- }
- } else {
- if (isLogin) {
- if (data.adminUser == true || data.safeUser == true || data.cabinetAdmin == true) {
- var bean =
- DoubleDialogBean(
- data.userName,
- data.faceImg,
- data.userId
- )
- mDoubleDialogBean.add(bean)
- passThrough()
- } else {
- customDialogView(0, "认证失败")
- }
- } else {
- if (data.userId == ChemicalApp.userData!!.userId) {
- var bean =
- DoubleDialogBean(
- data.userName,
- data.faceImg,
- data.userId
- )
- mDoubleDialogBean.add(bean)
- passThrough()
- } else {
- customDialogView(0, "认证失败")
- }
- }
- }
- }
- 2 -> {//待入库
- if (count == 1) {
- if (data.userId == ChemicalApp.userData!!.userId) {
- isLogin = true
- count++
- faceTwo()
- customDialog(data.userName, data.faceImg, data.userId)
- var bean =
- DoubleDialogBean(data.userName, data.faceImg, data.userId)
- mDoubleDialogBean.add(bean)
- } else {
- toBeStored(data)
- }
- } else {
- if (isLogin) {
- if (mChemicalLevel == 1) {
- //管控
- if (data.adminUser == true || data.safeUser == true || data.cabinetAdmin == true) {
- var bean = DoubleDialogBean(
- data.userName,
- data.faceImg,
- data.userId
- )
- mDoubleDialogBean.add(bean)
- passThrough()
- } else {
- customDialogView(0, "认证失败")
- }
- } else {
- //非管控 实验室负责人or安全负责人or柜锁管理员
- if (data.safeUser == true || data.adminUser == true || data.cabinetAdmin == true) {
- var bean = DoubleDialogBean(
- data.userName,
- data.faceImg,
- data.userId
- )
- mDoubleDialogBean.add(bean)
- passThrough()
- } else {
- customDialogView(0, "认证失败")
- }
- }
- } else {
- if (data.userId == ChemicalApp.userData!!.userId) {
- var bean =
- DoubleDialogBean(
- data.userName,
- data.faceImg,
- data.userId
- )
- mDoubleDialogBean.add(bean)
- passThrough()
- } else {
- customDialogView(0, "认证失败")
- }
- }
- }
- }
- }
- },
- { throwable ->
- dismissLoading()
- // mFotoapparat.stop()
- // handlerBack.removeCallbacks(countdownRunnable)
- // handlerBack.removeCallbacksAndMessages(null)
- throwableView(throwable)
- })
- addDisposable(disposable)
- }
- private fun obtainCertification(data: UserValidationBean) {
- //校级管理员 schoolLevelAdmin
- //院级管理员 collegeAdmin
- //实验室负责人 adminUser
- //安全负责人 safeUser
- //柜锁管理员 cabinetAdmin
- //是否化学品归属人 belongUser
- //是否化学品归属课题组下成员 toipcUser
- if (data.adminUser == true || data.safeUser == true || data.cabinetAdmin == true) {
- count++
- faceTwo()
- customDialog(data.userName, data.faceImg, data.userId)
- var bean = DoubleDialogBean(data.userName, data.faceImg, data.userId)
- mDoubleDialogBean.add(bean)
- } else {
- customDialogView(0, "认证失败")
- }
- }
- private fun toBeStored(data: UserValidationBean) {
- //校级管理员 schoolLevelAdmin
- //院级管理员 collegeAdmin
- //实验室负责人 adminUser
- //安全负责人 safeUser
- //柜锁管理员 cabinetAdmin
- //是否化学品归属人 belongUser
- //是否化学品归属课题组下成员 toipcUser
- if (mChemicalLevel == 1) {
- //管控
- if (data.belongUser == true || data.toipcUser == true || data.adminUser == true || data.safeUser == true || data.cabinetAdmin == true) {
- count++
- faceTwo()
- customDialog(data.userName, data.faceImg, data.userId)
- var bean = DoubleDialogBean(data.userName, data.faceImg, data.userId)
- mDoubleDialogBean.add(bean)
- } else {
- customDialogView(0, "认证失败")
- }
- } else {
- //非管控 实验室负责人or安全负责人or柜锁管理员
- if (data.safeUser == true || data.adminUser == true || data.cabinetAdmin == true) {
- count++
- faceTwo()
- customDialog(data.userName, data.faceImg, data.userId)
- var bean = DoubleDialogBean(data.userName, data.faceImg, data.userId)
- mDoubleDialogBean.add(bean)
- } else {
- customDialogView(0, "认证失败")
- }
- }
- }
- private fun customDialog(userName: String, imgUrl: String?, userId: String) {
- mUserId = userId
- viewBinding.through.visibility = View.VISIBLE
- viewBinding.userName.text = "$userName"
- Glide.with(this)
- .load("${HttpConfig.API_BASE_IMG_URL}$imgUrl")
- .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
- .error(R.mipmap.icon_zhan_wei)
- .into(viewBinding.imgView)
- MediaPlayerHelper.playRawMp3(this@TwoPersonActivity, R.raw.dierwei_renlianshibie)
- }
- private fun takePicture() {
- val format = SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.getDefault())
- val fileName = "${format.format(Calendar.getInstance().time)}.jpg"
- val photoFile = File(this.getExternalFilesDir("photos"), fileName)
- RcLog.info("==========当前认证数 $count")
- if (count == 1) {
- mFotoapparat.takePicture()
- .saveToFile(photoFile)
- .whenAvailable {
- RcLog.info("=====人脸识别D=${HttpClient.token} $it $photoFile")
- callFaceMatchingApi(photoFile)
- }
- } else {
- mFotoapparatTwo.takePicture()
- .saveToFile(photoFile)
- .whenAvailable {
- RcLog.info("=====人脸识别B=${HttpClient.token}")
- callFaceMatchingApi(photoFile)
- }
- }
- }
- override fun onDestroy() {
- super.onDestroy()
- try {
- RcLog.info("=====清空双人认证1 人脸信息")
- mFotoapparat.stop()
- RcLog.info("=====清空双人认证2 人脸信息")
- RcLog.info("=====清空双人认证3 人脸信息")
- handlerBackTwo.removeCallbacks(countdownRunnableTwo)
- RcLog.info("=====清空双人认证4 人脸信息")
- handlerBackTwo.removeCallbacksAndMessages(null)
- RcLog.info("=====清空双人认证5 人脸信息")
- } catch (e: Exception) {
- }
- try {
- mFotoapparatTwo.stop()
- RcLog.info("=====清空双人认证6 人脸信息")
- } catch (e: Exception) {
- }
- try {
- handlerBack.removeCallbacks(countdownRunnable)
- RcLog.info("=====清空双人认证7 人脸信息")
- } catch (e: Exception) {
- }
- try {
- handlerBack.removeCallbacksAndMessages(null)
- RcLog.info("=====清空双人认证8 人脸信息")
- } catch (e: Exception) {
- }
- try {
- countHandler.removeCallbacksAndMessages(null)
- } catch (e: Exception) {
- }
- }
- /**
- * 0 没有图标 1 绿色(成功) 2红色(失败)
- * 失败或者成功的弹框
- */
- private fun customDialogView(types: Int, msg: String) {
- val customDialog = CustomDialog(this, types, msg)
- if (!this.isFinishing && !this.isDestroyed) {
- customDialog.show()
- }
- }
- /**
- * 异常处理
- */
- private fun throwableView(throwable: Throwable) {
- when (throwable) {
- is NetException -> {
- if (throwable.message.isNullOrEmpty()) {
- "接口请求失败(${throwable.code})"
- } else {
- throwable.message!!
- }
- }
- is SocketTimeoutException -> "请求超时,请稍后重试"
- is ConnectException -> "无法连接服务器,请检查网络"
- is HttpException -> "服务器繁忙,请稍后重试"
- else -> null
- }?.let { customDialogView(2, "$it") }
- }
- private lateinit var dialogsAut: DoublePeopleDialog
- private var timeLeftInSeconds = 2
- private var mTvView: TextView? = null
- //验证通过
- private fun passThrough() {
- // 在需要时关闭定时器
- try {
- RcLog.info("=====清空双人认证3 人脸信息")
- mFotoapparatTwo.stop()
- handlerBackTwo.removeCallbacks(countdownRunnableTwo)
- RcLog.info("=====清空双人认证4 人脸信息")
- handlerBackTwo.removeCallbacksAndMessages(null)
- } catch (e: Exception) {
- }
- Constants.AUTHENTICATION = true
- // 存储集合到 SharedPreferences
- SharedPreferencesHelper.saveList(this, mDoubleDialogBean)
- dialogsAut =
- DoublePeopleDialog(this, mDoubleDialogBean, object : DoublePeopleDialog.IClickLit {
- override fun onUpView(tvView: TextView) {
- mTvView = tvView
- }
- })
- dialogsAut.show()
- // 开始倒计时
- handlerBack.post(countdownRunnable)
- // 获取对话框的 Window 对象
- dialogsAut?.window?.decorView?.setOnTouchListener { _, event ->
- // 判断是否点击了对话框外部空白区域
- if (event.action == MotionEvent.ACTION_DOWN) {
- val x = event.x
- val y = event.y
- val dialogView = dialogsAut?.window?.decorView
- if (dialogView != null && (x < 0 || x > dialogView.width || y < 0 || y > dialogView.height)) {
- // 在此处执行点击对话框外部空白区域时的操作
- // 例如关闭对话框
- dialogsAut.dismiss()
- finish()
- return@setOnTouchListener true
- }
- }
- return@setOnTouchListener false
- }
- MediaPlayerHelper.playRawMp3(this, R.raw.shuangren_tongguo)
- }
- private val countdownRunnable = object : Runnable {
- override fun run() {
- if (timeLeftInSeconds > 0) {
- mTvView!!.text = "${timeLeftInSeconds}秒后自动返回首页"
- timeLeftInSeconds--
- handlerBack.postDelayed(this, 1000)
- } else {
- dialogsAut!!.dismiss()
- finish()
- }
- }
- }
- }
|