android - 如何在启动应用程序后立即启动二维码扫描器?

标签 android qr-code

我的二维码扫描代码如下:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //View objects
        buttonScan = findViewById<View>(R.id.buttonScan) as Button
        textViewName = findViewById<View>(R.id.textViewName) as TextView
        textViewAddress = findViewById<View>(R.id.textViewAddress) as TextView

        //intializing scan object
        qrScan = IntentIntegrator(this)

        //attaching onclick listener
        buttonScan!!.setOnClickListener(this)


    }

    //Getting the scan results
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
        if (result != null) {
            //if qrcode has nothing in it
            if (result.contents == null) {
                Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show()
            } else {
                //if qr contains data
                try {

                    //converting the data to json
                    val obj = JSONObject(result.contents)
                    //setting values to textviews
                    textViewName!!.text = obj.getString("busno")

                    textViewAddress!!.text = obj.getString("busname")


                } catch (e: JSONException) {
                    e.printStackTrace()
                    //if control comes here
                    //that means the encoded format not matches
                    //in this case you can display whatever data is available on the qrcode
                    //to a toast
                    Toast.makeText(this, result.contents, Toast.LENGTH_LONG).show()
                }

            }
        } else {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }

    override fun onClick(view: View) {
        //initiating the qr code scan
        qrScan!!.initiateScan()
    }

在我按下声明为 buttonScan 的按钮后,QR 码扫描仪启动。如何在打开应用程序后立即启动二维码扫描仪,然后显示扫描详情?它需要新的 Activity 吗?

我使用 ZXING 库来实现扫描器。

谢谢!

最佳答案

使用您的代码,您可以通过两种方式启动 QR 扫描仪:

第一种方式是在这个qrScan = IntentIntegrator(this)

之后直接启动扫描

所以你的onCreate可以是:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //View objects
        buttonScan = findViewById<View>(R.id.buttonScan) as Button
        textViewName = findViewById<View>(R.id.textViewName) as TextView
        textViewAddress = findViewById<View>(R.id.textViewAddress) as TextView

        //intializing scan object
        qrScan = IntentIntegrator(this)

        //initiate scan directly
        qrScan!!.initiateScan()

        //attaching onclick listener
        buttonScan!!.setOnClickListener(this)


    }

第二种方式是给buttonScan触发click事件,它会直接发起扫描。

buttonScan!!.callOnClick

关于android - 如何在启动应用程序后立即启动二维码扫描器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52906140/

相关文章:

java - HttpResponse 返回 502 错误,似乎没有到达端点

javascript - 为什么 PhoneGap 的性能 WebGL 与 firefox 或 chrome 不同?

qr-code - 二维码回车

qr-code - 二维码数据格式的规范是什么?我在任何地方都找不到它

xcode - 如何使用swift从VCard中检索数据

android - 将数据发布到android中的服务器

android - 我可以将 Flash Professional SWF 集成到 Flash Builder 中吗?

java - 尝试调用 getLayoutInflater 时出现空指针异常

linux - 从/dev/hidraw1 设备读取连接到 linux 系统

ios - 我如何知道我的 Swift QR 码是否被扫描