封装二维码扫描业务
This commit is contained in:
@@ -37,7 +37,8 @@ public class CheckPermissionsActivity extends BaseActivity {
|
||||
Manifest.permission.ACCESS_FINE_LOCATION,
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.RECORD_AUDIO
|
||||
Manifest.permission.RECORD_AUDIO,
|
||||
Manifest.permission.CAMERA,
|
||||
};
|
||||
|
||||
private static final int PERMISSON_REQUESTCODE = 0;
|
||||
@@ -53,6 +54,7 @@ public class CheckPermissionsActivity extends BaseActivity {
|
||||
Manifest.permission.WRITE_EXTERNAL_STORAGE,
|
||||
Manifest.permission.READ_EXTERNAL_STORAGE,
|
||||
Manifest.permission.RECORD_AUDIO,
|
||||
Manifest.permission.CAMERA,
|
||||
BACKGROUND_LOCATION_PERMISSION
|
||||
};
|
||||
}
|
||||
|
||||
@@ -123,8 +123,6 @@ class MainActivity : BaseActivity() {
|
||||
checkIntent.action = TextToSpeech.Engine.ACTION_CHECK_TTS_DATA
|
||||
someActivityResultLauncher.launch(checkIntent)
|
||||
|
||||
|
||||
|
||||
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
|
||||
|
||||
//初始化地图
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package com.navinfo.omqs.ui.activity.scan
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Intent
|
||||
import android.graphics.Rect
|
||||
import android.graphics.RectF
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.camera.core.ImageCapture
|
||||
import androidx.camera.view.LifecycleCameraController
|
||||
import androidx.databinding.DataBindingUtil
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.navinfo.omqs.R
|
||||
import com.navinfo.omqs.databinding.ActivityQrCodeBinding
|
||||
import com.navinfo.omqs.ui.activity.login.LoginViewModel
|
||||
import com.navinfo.omqs.ui.listener.QRCodeAnalyser
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
/**
|
||||
* date:2021/6/18
|
||||
* author:zhangteng
|
||||
* description:二维码扫描
|
||||
*/
|
||||
class QRCodeActivity : AppCompatActivity() {
|
||||
private lateinit var binding: ActivityQrCodeBinding
|
||||
private lateinit var lifecycleCameraController: LifecycleCameraController
|
||||
private lateinit var cameraExecutor: ExecutorService
|
||||
private val viewModel by viewModels<QRCodeViewModel>()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
binding = DataBindingUtil.setContentView(this, R.layout.activity_qr_code)
|
||||
|
||||
binding.qrCodeModel = viewModel
|
||||
binding.lifecycleOwner = this
|
||||
binding.activity = this
|
||||
|
||||
initController()
|
||||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility", "UnsafeOptInUsageError")
|
||||
private fun initController() {
|
||||
cameraExecutor = Executors.newSingleThreadExecutor()
|
||||
lifecycleCameraController = LifecycleCameraController(this)
|
||||
lifecycleCameraController.bindToLifecycle(this)
|
||||
lifecycleCameraController.imageCaptureFlashMode = ImageCapture.FLASH_MODE_AUTO
|
||||
lifecycleCameraController.setImageAnalysisAnalyzer(
|
||||
cameraExecutor,
|
||||
QRCodeAnalyser { barcodes, imageWidth, imageHeight ->
|
||||
if (barcodes.isEmpty()) {
|
||||
return@QRCodeAnalyser
|
||||
}
|
||||
initScale(imageWidth, imageHeight)
|
||||
val list = ArrayList<RectF>()
|
||||
val strList = ArrayList<String>()
|
||||
|
||||
barcodes.forEach { barcode ->
|
||||
barcode.boundingBox?.let { rect ->
|
||||
val translateRect = translateRect(rect)
|
||||
list.add(translateRect)
|
||||
Log.e(
|
||||
"ztzt", "left:${translateRect.left} +" +
|
||||
" top:${translateRect.top} + right:${translateRect.right}" +
|
||||
" + bottom:${translateRect.bottom}"
|
||||
)
|
||||
Log.e("ztzt", "barcode.rawValue:${barcode.rawValue}")
|
||||
strList.add(barcode.rawValue ?: "No Value")
|
||||
}
|
||||
}
|
||||
judgeIntent(strList)
|
||||
binding.scanView.setRectList(list)
|
||||
|
||||
})
|
||||
binding.previewView.controller = lifecycleCameraController
|
||||
}
|
||||
|
||||
fun judgeIntent(list: ArrayList<String>) {
|
||||
val sb = StringBuilder()
|
||||
list.forEach {
|
||||
sb.append(it)
|
||||
sb.append("\n")
|
||||
}
|
||||
intentToResult(sb.toString())
|
||||
}
|
||||
|
||||
private fun intentToResult(result: String) {
|
||||
viewModel.connect(this, result)
|
||||
|
||||
Log.e("qj", "QRCodeActivity === $result")
|
||||
/* val intent = Intent(this, QRCodeResultActivity::class.java)
|
||||
intent.putExtra(QRCodeResultActivity.RESULT_KEY, result)
|
||||
startActivity(intent)
|
||||
finish()*/
|
||||
}
|
||||
|
||||
private var scaleX = 0f
|
||||
private var scaleY = 0f
|
||||
|
||||
private fun translateX(x: Float): Float = x * scaleX
|
||||
private fun translateY(y: Float): Float = y * scaleY
|
||||
|
||||
//将扫描的矩形换算为当前屏幕大小
|
||||
private fun translateRect(rect: Rect) = RectF(
|
||||
translateX(rect.left.toFloat()),
|
||||
translateY(rect.top.toFloat()),
|
||||
translateX(rect.right.toFloat()),
|
||||
translateY(rect.bottom.toFloat())
|
||||
)
|
||||
|
||||
//初始化缩放比例
|
||||
private fun initScale(imageWidth: Int, imageHeight: Int) {
|
||||
Log.e("ztzt", "imageWidth:${imageWidth} + imageHeight:${imageHeight}")
|
||||
scaleY = binding.scanView.height.toFloat() / imageWidth.toFloat()
|
||||
scaleX = binding.scanView.width.toFloat() / imageHeight.toFloat()
|
||||
Log.e("ztzt", "scaleX:${scaleX} + scaleY:${scaleY}")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.navinfo.omqs.ui.activity.scan
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.navinfo.omqs.databinding.ActivityResultBinding
|
||||
|
||||
/**
|
||||
* date:2021/6/18
|
||||
* author:zhangteng
|
||||
* description:
|
||||
*/
|
||||
class QRCodeResultActivity : AppCompatActivity() {
|
||||
private lateinit var binding: ActivityResultBinding
|
||||
|
||||
companion object {
|
||||
const val RESULT_KEY = "result_key";
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityResultBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
|
||||
val intent = intent
|
||||
binding.text.text = intent.getStringExtra(RESULT_KEY)
|
||||
binding.button.setOnClickListener {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package com.navinfo.omqs.ui.activity.scan
|
||||
|
||||
import android.content.Context
|
||||
import android.text.TextUtils
|
||||
import android.widget.Toast
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.navinfo.omqs.bean.QRCodeBean
|
||||
import com.navinfo.omqs.bean.SysUserBean
|
||||
import com.navinfo.omqs.http.DefaultResponse
|
||||
import com.navinfo.omqs.http.NetResult
|
||||
import com.navinfo.omqs.http.NetworkService
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.IOException
|
||||
import javax.inject.Inject
|
||||
|
||||
enum class QRCodeStatus {
|
||||
/**
|
||||
* 访问服务器登陆中
|
||||
*/
|
||||
LOGIN_STATUS_NET_LOADING,
|
||||
|
||||
/**
|
||||
* 访问离线地图列表
|
||||
*/
|
||||
LOGIN_STATUS_NET_OFFLINE_MAP,
|
||||
|
||||
/**
|
||||
* 初始化文件夹
|
||||
*/
|
||||
LOGIN_STATUS_FOLDER_INIT,
|
||||
|
||||
/**
|
||||
* 创建文件夹失败
|
||||
*/
|
||||
LOGIN_STATUS_FOLDER_FAILURE,
|
||||
|
||||
/**
|
||||
* 网络访问失败
|
||||
*/
|
||||
LOGIN_STATUS_NET_FAILURE,
|
||||
|
||||
/**
|
||||
* 成功
|
||||
*/
|
||||
LOGIN_STATUS_SUCCESS,
|
||||
|
||||
/**
|
||||
* 取消
|
||||
*/
|
||||
LOGIN_STATUS_CANCEL,
|
||||
}
|
||||
|
||||
@HiltViewModel
|
||||
class QRCodeViewModel @Inject constructor(
|
||||
private val networkService: NetworkService
|
||||
) : ViewModel() {
|
||||
//用户信息
|
||||
val qrCodeBean: MutableLiveData<QRCodeBean> = MutableLiveData()
|
||||
|
||||
//是不是连接成功
|
||||
val qrCodeStatus: MutableLiveData<QRCodeStatus> = MutableLiveData()
|
||||
|
||||
var jobQRCodeStatus: Job? = null;
|
||||
|
||||
init {
|
||||
qrCodeBean.value = QRCodeBean()
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 扫一扫按钮
|
||||
*/
|
||||
fun connect(context: Context, ips: String) {
|
||||
|
||||
if (TextUtils.isEmpty(ips)) {
|
||||
Toast.makeText(context, "获取ip失败!", Toast.LENGTH_LONG).show()
|
||||
return
|
||||
}
|
||||
|
||||
val ipArray = ips.split(";".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
||||
//测试代码
|
||||
//final String[] ipArray = new String[]{"172.21.2.137"};
|
||||
if (ipArray.isEmpty()) {
|
||||
Toast.makeText(context, "获取ip失败!", Toast.LENGTH_SHORT).show()
|
||||
return
|
||||
}
|
||||
|
||||
ipArray.forEach { ip ->
|
||||
if (!TextUtils.isEmpty(ip)) {
|
||||
viewModelScope.launch(Dispatchers.Default) {
|
||||
val ipTemp: String = ip
|
||||
val url = "http://$ipTemp:8080/sensor/service/keepalive"
|
||||
when (val result = networkService.connectIndoorTools(url)) {
|
||||
is NetResult.Success<*> -> {
|
||||
if (result.data != null) {
|
||||
try {
|
||||
val defaultUserResponse =
|
||||
result.data as DefaultResponse<SysUserBean>
|
||||
if (defaultUserResponse.success) {
|
||||
|
||||
} else {
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
"${defaultUserResponse.msg}",
|
||||
Toast.LENGTH_SHORT
|
||||
)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
|
||||
} catch (e: IOException) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
is NetResult.Error<*> -> {
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
"${result.exception.message}",
|
||||
Toast.LENGTH_SHORT
|
||||
)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
|
||||
is NetResult.Failure<*> -> {
|
||||
withContext(Dispatchers.Main) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
"${result.code}:${result.msg}",
|
||||
Toast.LENGTH_SHORT
|
||||
)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user