本篇文章使用的是
Github:GitHub - yipianfengye/android-zxingLibrary: 几行代码快速集成二维码扫描功能
代码中使用了dataBinding
在app的build.gradle中添加
implementation 'cn.yipianfengye.android:zxing-library:2.2'
Application中执行初始化操作
ZXingLibrary.initDisplayOpinion(this)
?定制化显示扫描UI
activity_scan.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:navigationIcon="@drawable/ic_back">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/scan_setting"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold" />
</androidx.appcompat.widget.Toolbar>
<FrameLayout
android:id="@+id/fl_my_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
?扫描界面代码
ScanActivity.kt
class ScanActivity : AppCompatActivity() {
private lateinit var binding: ActivityScanBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityScanBinding.inflate(layoutInflater)
setContentView(binding.root)
initView()
}
private fun initView() {
val captureFragment = CaptureFragment()
CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera)
captureFragment.analyzeCallback = analyzeCallback
supportFragmentManager.beginTransaction().replace(R.id.fl_my_container, captureFragment)
.commit()
}
private val analyzeCallback = object : CodeUtils.AnalyzeCallback {
override fun onAnalyzeSuccess(bitmap: Bitmap, result: String) {
val resultIntent = Intent()
val bundle = Bundle()
bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS)
bundle.putString(CodeUtils.RESULT_STRING, result)
resultIntent.putExtras(bundle)
setResult(RESULT_OK, resultIntent)
finish()
}
override fun onAnalyzeFailed() {
val resultIntent = Intent()
val bundle = Bundle()
bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED)
bundle.putString(CodeUtils.RESULT_STRING, "")
resultIntent.putExtras(bundle)
setResult(RESULT_OK, resultIntent)
finish()
}
}
}
?MainActivity调用
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var registerForActivityResult: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
initData()
}
private fun initData() {
register()
//...
initListener()
}
private fun initListener() {
binding.btnScan.noFastClick {
registerForActivityResult.launch(Intent(this, ScanActivity::class.java))
}
}
private fun register() {
registerForActivityResult =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (it.data != null && it.resultCode == Activity.RESULT_OK) {
val bundle = it.data?.extras
bundle?.let {
if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
//扫码成功
val result = bundle.getString(CodeUtils.RESULT_STRING)
}
} else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
//扫码失败
}
}
}
}
}
override fun onDestroy() {
registerForActivityResult.unregister()
super.onDestroy()
}
}
更多请看作者GitHub
|