需要获取条形码阅读器的价值并在不关闭扫描仪的情况下在同一屏幕上获取数据

时间:2019-07-15 10:09:20

标签: android zxing

我打开扫描仪屏幕以读取值,当获取值时,它将关闭扫描仪并发送回我的应用程序屏幕,我不想关闭该屏幕。

图书馆:ZXing

我想使用该值获取数据并在同一屏幕上的弹出窗口中显示。

我粘贴以下代码以获取帮助

这是我的第一个活动代码:

class FirstActivity : AppCompatActivity() {

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

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        val result = IntentIntegrator.parseActivityResult(resultCode, data)

        if (result.contents == null) {
            Log.d("FirstActivity", "Cancelled scan")
            Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show()
        } else {
            Log.d("FirstActivity", "Scanned")
            Toast.makeText(this, "Scanned: " + result.contents, Toast.LENGTH_LONG).show()
        }
    }
    // Navigate to the custom barcode scanner activity
    fun navigate(v: View){
        val integrator = IntentIntegrator(this)
        integrator.captureActivity = SecondActivity::class.java
        integrator.initiateScan()
    }
}

这是扫描仪活动

class SecondActivity : AppCompatActivity(), DecoratedBarcodeView.TorchListener {

    private var capture: CaptureManager? = null
    private var barcodeScannerView: DecoratedBarcodeView? = null
    private var switchFlashlightButton: AppCompatButton? = null
    private var viewfinderView: ViewfinderView? = null

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

        barcodeScannerView = findViewById(R.id.zxing_barcode_scanner)
        barcodeScannerView?.setTorchListener(this)

        switchFlashlightButton = findViewById<AppCompatButton>(R.id.switch_flashlight)

        viewfinderView = findViewById<ViewfinderView>(R.id.zxing_viewfinder_view)

        // if the device does not have flashlight in its camera,
        // then remove the switch flashlight button...
        if (!hasFlash()) {
            switchFlashlightButton?.visibility = View.GONE
        }
        capture = CaptureManager(this@SecondActivity, barcodeScannerView)
        capture?.initializeFromIntent(intent, savedInstanceState)
        capture?.decode()

        changeMaskColor()
    }

    override fun onResume() {
        super.onResume()
        capture?.onResume()
    }

    override fun onPause() {
        super.onPause()
        capture?.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        capture?.onDestroy()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        capture?.onSaveInstanceState(outState)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        return barcodeScannerView!!.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event)
    }

    private fun hasFlash(): Boolean {
        return applicationContext.packageManager
            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
    }

    fun switchFlashlight(v: View) {
        if (getString(R.string.turn_on_flashlight) == (v as AppCompatButton).text) {
            barcodeScannerView?.setTorchOn()
        } else {
            barcodeScannerView?.setTorchOff()
        }
    }

    private fun changeMaskColor() {
//        val rnd = Random
//        val color = Color.argb(100, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))
        viewfinderView?.setBackgroundColor(Color.parseColor("#00FFFFFF"))
    }

    override fun onTorchOn() {
        switchFlashlightButton?.setText(R.string.turn_off_flashlight)
    }

    override fun onTorchOff() {
        switchFlashlightButton?.setText(R.string.turn_on_flashlight)
    }
}

0 个答案:

没有答案