| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package com.nova.brain.glass.ui
- import android.content.Intent
- import android.graphics.BitmapFactory
- import android.os.CountDownTimer
- import android.os.Environment
- import android.view.WindowManager
- import androidx.recyclerview.widget.RecyclerView
- import com.nova.brain.glass.R
- import com.nova.brain.glass.databinding.ActivitySprayingOcrBinding
- import com.nova.brain.glass.helper.OfflineCmdListener
- import com.nova.brain.glass.helper.OfflineCmdServiceHelper
- import com.nova.brain.glass.model.ItemItem
- import com.nova.brain.glass.viewmodel.SprayingOCRVM
- import com.nova.brain.glass.viewmodel.SprayingVM
- import com.rokid.security.glass3.open.sdk.GlassSdk
- import com.rokid.security.glass3.sdk.base.data.media.PhotoResolution
- import com.rokid.security.system.server.media.callback.PhotoFileCallback
- import com.xuqm.base.adapter.BasePagedAdapter
- import com.xuqm.base.adapter.CommonPagedAdapter
- import com.xuqm.base.adapter.ViewHolder
- import com.xuqm.base.common.LogHelper
- import com.xuqm.base.extensions.showMessage
- import com.xuqm.base.ui.BaseListFormLayoutNormalActivity
- import java.io.File
- import java.util.UUID
- class SprayingOCRActivity :
- BaseListFormLayoutNormalActivity<ItemItem, SprayingOCRVM, ActivitySprayingOcrBinding>() {
- override fun getLayoutId(): Int = R.layout.activity_spraying_ocr
- override fun fullscreen(): Boolean = true
- override fun getRecyclerOrientation(): Int = RecyclerView.VERTICAL
- private val listener = object : OfflineCmdListener {
- override fun onOfflineCmd(cmd: String) {
- runOnUiThread {
- when (cmd) {
- "退出", "返回", "退回" -> {
- finish()
- }
- "开始", "拍照", "开始拍照", "开始任务", "重拍", "重新拍", "在拍一次" -> {
- runOnUiThread {
- binding.hint.text = "拍照中,请稍后..."
- }
- isPhoto = true
- takePhoto()
- }
- }
- }
- }
- }
- fun takePhoto() {
- resultCountdown?.cancel()
- resultCountdown = null
- val fileName = "test_${System.currentTimeMillis()}.png"
- val publicPicturesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
- val file = File(publicPicturesDir, fileName)
- GlassSdk.getGlassMediaService()?.takePhoto(PhotoResolution.RESOLUTION_480P, file.absolutePath)
- }
- private val photoCallbackId = UUID.randomUUID().toString()
- private var resultCountdown: CountDownTimer? = null
- private val mPhotoFileCallback = object : PhotoFileCallback.Stub() {
- override fun onTakePhoto(path: String) {
- LogHelper.d("onTakePhoto-->path = $path")
- }
- override fun getCallbackId(): String {
- return photoCallbackId
- }
- override fun onTakePhotoV2(path: String?, width: Int, height: Int) {
- LogHelper.d("width:$width--height:$height")
- if (path == null) {
- if (isPhoto) {
- isPhoto = false
- takePhoto()
- } else {
- runOnUiThread {
- binding.hint.text = "单击或语音输入“重拍”,可重新拍摄"
- }
- "相机异常".showMessage()
- }
- } else {
- runOnUiThread {
- binding.hint.text = "单击或语音输入“重拍”,可重新拍摄"
- showPhoto(path)
- }
- }
- }
- }
- override fun initData() {
- super.initData()
- window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
- intent.getStringExtra("path")?.apply {
- showPhoto(this)
- }
- }
- override fun onResume() {
- super.onResume()
- OfflineCmdServiceHelper.addOnLineListener(listener)
- GlassSdk.getGlassMediaService()?.addPhotoCallback(mPhotoFileCallback)
- }
- override fun onPause() {
- super.onPause()
- resultCountdown?.cancel()
- OfflineCmdServiceHelper.removeOnLineListener(listener)
- GlassSdk.getGlassMediaService()?.removePhotoCallback(mPhotoFileCallback)
- }
- private var isPhoto = false
- private val adapter = object : CommonPagedAdapter<ItemItem>(R.layout.item_photo) {
- override fun convert(holder: ViewHolder, item: ItemItem, position: Int) {
- holder
- .setText(R.id.text, item.text)
- .setClickListener(R.id.photo) {
- when (item.text) {
- "重拍" -> {
- runOnUiThread {
- binding.hint.text = "拍照中,请稍后..."
- }
- isPhoto = true
- takePhoto()
- }
- }
- }
- }
- }
- override fun adapter(): BasePagedAdapter<ItemItem> = adapter
- private fun showPhoto(path: String) {
- binding.content.setImageBitmap(BitmapFactory.decodeFile(path))
- restartResultCountdown(path)
- }
- private fun restartResultCountdown(path: String) {
- resultCountdown?.cancel()
- resultCountdown = object : CountDownTimer(5_000, 1_000) {
- override fun onTick(millisUntilFinished: Long) {
- }
- override fun onFinish() {
- startActivity(Intent(this@SprayingOCRActivity, SprayingResultActivity::class.java).apply {
- putExtra("path", path)
- })
- finish()
- }
- }.start()
- }
- }
|