Android开发教程二维码扫描功能
二维码扫描大一点的app都有的功能,因为扫一下真的很方便
一、思路:
用zxing库
二、效果图:
看视频更加直观点:
Android开发教程实战案例源码分享-二维码扫描功能
三、关键代码:
class FCaptureActivity : AppCompatActivity(), SurfaceHolder.Callback {
// 相机控制
private var cameraManager: MyCameraManager? = null
private var handler: CaptureActivityHandler? = null
private var viewfinderView: ViewfinderView? = null
private var hasSurface: Boolean = false
private var decodeFormats: Collection<BarcodeFormat>? = null
private val decodeHints: Map<DecodeHintType, *>? = null
private var characterSet: String? = null
private var REQUEST_CODE: Int = 1001
private lateinit var open :CheckBox
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.find_activity_zxing)
val window = window
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
initUI()
}
fun initUI() {
open = findViewById(R.id.open)
open.setOnCheckedChangeListener { _, isChecked ->
when (isChecked) {
true -> openFlashLight()
else -> closeFlashLight()
}
}
}
fun getViewfinderView(): ViewfinderView? {
return viewfinderView
}
fun getHandler(): Handler? {
return handler
}
fun getCameraManager(): MyCameraManager? {
return cameraManager
}
fun drawViewfinder() {
viewfinderView?.drawViewfinder()
}
override fun onResume() {
super.onResume()
// CameraManager必须在这里初始化,而不是在onCreate()中。
// 这是必须的,因为当我们第一次进入时需要显示帮助页,我们并不想打开Camera,测量屏幕大小
// 当扫描框的尺寸不正确时会出现bug
cameraManager = MyCameraManager(this)
viewfinderView = findViewById(R.id.viewfinder_view)
viewfinderView?.setCameraManager(cameraManager)
viewfinderView?.setWord(true)
handler = null
val surfaceView = findViewById<SurfaceView>(R.id.preview_view)
val surfaceHolder = surfaceView.holder
if (hasSurface) {
// activity在paused时但不会stopped,因此surface仍旧存在;
// surfaceCreated()不会调用,因此在这里初始化camera
initCamera(surfaceHolder)
} else {
// 重置callback,等待surfaceCreated()来初始化camera
surfaceHolder.addCallback(this)
}
decodeFormats = null
characterSet = null
}
override fun onPause() {
if (handler != null) {
handler?.quitSynchronously()
handler = null
}
cameraManager?.closeDriver()
if (!hasSurface) {
val surfaceView = findViewById<SurfaceView>(R.id.preview_view)
val surfaceHolder = surfaceView.holder
surfaceHolder.removeCallback(this)
}
super.onPause()
}
/**
* 初始化Camera
*
* @param surfaceHolder
*/
private fun initCamera(surfaceHolder: SurfaceHolder?) {
if (surfaceHolder == null) {
throw IllegalStateException("No SurfaceHolder provided")
}
if (cameraManager!!.isOpen) {
return
}
try {
// 打开Camera硬件设备
cameraManager?.openDriver(surfaceHolder)
// 创建一个handler来打开预览,并抛出一个运行时异常
if (handler == null) {
handler = CaptureActivityHandler(this, decodeFormats, decodeHints, characterSet, cameraManager)
}
} catch (ioe: Exception) {
}
}
/**
* 扫描成功,处理反馈信息
*
* @param rawResult
* @param barcode
* @param scaleFactor
*/
fun handleDecode(result: Result?, barcode: Bitmap?, scaleFactor: Float?) {
Log.d("handleDecode", "结果${result?.text}")
Toast.makeText(this,"结果 ${result?.text}",Toast.LENGTH_LONG).show()
// if (!TextUtils.isEmpty(result?.text)) {
// if (result?.text!!.indexOf("scan/regID=") != -1) {//扫码登录
//
// } else if (result?.text!!.indexOf("scan/pcLogin") != -1){ //pc官网登录
//
// } else {
// //基本不会出现这种情况
// showToastDialog("温馨提示", "无法识别该图片,请用相机扫描", "确认")
//
// }
//
// }
}
/**
* 开灯
*/
private fun openFlashLight() {
if (!cameraManager!!.isOpen) {
return
}
val parameter = cameraManager?.camera?.parameters
parameter?.flashMode = FLASH_MODE_TORCH
cameraManager?.camera?.parameters = parameter
}
/**
* 关闭闪光灯
*/
private fun closeFlashLight() {
if (!cameraManager!!.isOpen) {
return
}
val parameter = cameraManager?.camera?.parameters
parameter?.flashMode = FLASH_MODE_OFF
cameraManager?.camera?.parameters = parameter
}
override fun surfaceCreated(holder: SurfaceHolder) {
if (!hasSurface) {
hasSurface = true
initCamera(holder)
}
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
hasSurface = false
}
}
四、项目demo源码结构图
有问题或者需要完整源码的私信我