SprayingOCRActivity.kt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.nova.brain.glass.ui
  2. import android.content.Intent
  3. import android.graphics.BitmapFactory
  4. import android.os.CountDownTimer
  5. import android.os.Environment
  6. import android.view.WindowManager
  7. import androidx.recyclerview.widget.RecyclerView
  8. import com.nova.brain.glass.R
  9. import com.nova.brain.glass.databinding.ActivitySprayingOcrBinding
  10. import com.nova.brain.glass.helper.OfflineCmdListener
  11. import com.nova.brain.glass.helper.OfflineCmdServiceHelper
  12. import com.nova.brain.glass.model.ItemItem
  13. import com.nova.brain.glass.viewmodel.SprayingOCRVM
  14. import com.nova.brain.glass.viewmodel.SprayingVM
  15. import com.rokid.security.glass3.open.sdk.GlassSdk
  16. import com.rokid.security.glass3.sdk.base.data.media.PhotoResolution
  17. import com.rokid.security.system.server.media.callback.PhotoFileCallback
  18. import com.xuqm.base.adapter.BasePagedAdapter
  19. import com.xuqm.base.adapter.CommonPagedAdapter
  20. import com.xuqm.base.adapter.ViewHolder
  21. import com.xuqm.base.common.LogHelper
  22. import com.xuqm.base.extensions.showMessage
  23. import com.xuqm.base.ui.BaseListFormLayoutNormalActivity
  24. import java.io.File
  25. import java.util.UUID
  26. class SprayingOCRActivity :
  27. BaseListFormLayoutNormalActivity<ItemItem, SprayingOCRVM, ActivitySprayingOcrBinding>() {
  28. override fun getLayoutId(): Int = R.layout.activity_spraying_ocr
  29. override fun fullscreen(): Boolean = true
  30. override fun getRecyclerOrientation(): Int = RecyclerView.VERTICAL
  31. private val listener = object : OfflineCmdListener {
  32. override fun onOfflineCmd(cmd: String) {
  33. runOnUiThread {
  34. when (cmd) {
  35. "退出", "返回", "退回" -> {
  36. finish()
  37. }
  38. "开始", "拍照", "开始拍照", "开始任务", "重拍", "重新拍", "在拍一次" -> {
  39. runOnUiThread {
  40. binding.hint.text = "拍照中,请稍后..."
  41. }
  42. isPhoto = true
  43. takePhoto()
  44. }
  45. }
  46. }
  47. }
  48. }
  49. fun takePhoto() {
  50. resultCountdown?.cancel()
  51. resultCountdown = null
  52. val fileName = "test_${System.currentTimeMillis()}.png"
  53. val publicPicturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
  54. val file = File(publicPicturesDir, fileName)
  55. GlassSdk.getGlassMediaService()?.takePhoto(PhotoResolution.RESOLUTION_480P, file.absolutePath)
  56. }
  57. private val photoCallbackId = UUID.randomUUID().toString()
  58. private var resultCountdown: CountDownTimer? = null
  59. private val mPhotoFileCallback = object : PhotoFileCallback.Stub() {
  60. override fun onTakePhoto(path: String) {
  61. LogHelper.d("onTakePhoto-->path = $path")
  62. }
  63. override fun getCallbackId(): String {
  64. return photoCallbackId
  65. }
  66. override fun onTakePhotoV2(path: String?, width: Int, height: Int) {
  67. LogHelper.d("width:$width--height:$height")
  68. if (path == null) {
  69. if (isPhoto) {
  70. isPhoto = false
  71. takePhoto()
  72. } else {
  73. runOnUiThread {
  74. binding.hint.text = "单击或语音输入“重拍”,可重新拍摄"
  75. }
  76. "相机异常".showMessage()
  77. }
  78. } else {
  79. runOnUiThread {
  80. binding.hint.text = "单击或语音输入“重拍”,可重新拍摄"
  81. showPhoto(path)
  82. }
  83. }
  84. }
  85. }
  86. override fun initData() {
  87. super.initData()
  88. window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
  89. intent.getStringExtra("path")?.apply {
  90. showPhoto(this)
  91. }
  92. }
  93. override fun onResume() {
  94. super.onResume()
  95. OfflineCmdServiceHelper.addOnLineListener(listener)
  96. GlassSdk.getGlassMediaService()?.addPhotoCallback(mPhotoFileCallback)
  97. }
  98. override fun onPause() {
  99. super.onPause()
  100. resultCountdown?.cancel()
  101. OfflineCmdServiceHelper.removeOnLineListener(listener)
  102. GlassSdk.getGlassMediaService()?.removePhotoCallback(mPhotoFileCallback)
  103. }
  104. private var isPhoto = false
  105. private val adapter = object : CommonPagedAdapter<ItemItem>(R.layout.item_photo) {
  106. override fun convert(holder: ViewHolder, item: ItemItem, position: Int) {
  107. holder
  108. .setText(R.id.text, item.text)
  109. .setClickListener(R.id.photo) {
  110. when (item.text) {
  111. "重拍" -> {
  112. runOnUiThread {
  113. binding.hint.text = "拍照中,请稍后..."
  114. }
  115. isPhoto = true
  116. takePhoto()
  117. }
  118. }
  119. }
  120. }
  121. }
  122. override fun adapter(): BasePagedAdapter<ItemItem> = adapter
  123. private fun showPhoto(path: String) {
  124. binding.content.setImageBitmap(BitmapFactory.decodeFile(path))
  125. restartResultCountdown(path)
  126. }
  127. private fun restartResultCountdown(path: String) {
  128. resultCountdown?.cancel()
  129. resultCountdown = object : CountDownTimer(5_000, 1_000) {
  130. override fun onTick(millisUntilFinished: Long) {
  131. }
  132. override fun onFinish() {
  133. startActivity(Intent(this@SprayingOCRActivity, SprayingResultActivity::class.java).apply {
  134. putExtra("path", path)
  135. })
  136. finish()
  137. }
  138. }.start()
  139. }
  140. }