1

I have built a QR scanning application (written in Kotlin, for Android). It scans for a QR code and then returns the URL after its scanned like so.

enter image description here

However, I wish to take it a step further and actually launch the return value of the QR code into a search engine of an internet application and have it display the results of what the QR code had intended. How would I get the application to get the returned URL and redirect the user to the intended place?

Here is my MainActivity.kt for reference:

class MainActivity : AppCompatActivity() {

    private lateinit var codeScanner: CodeScanner

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

        val scannerView = findViewById<CodeScannerView>(R.id.scanner_view)

        codeScanner = CodeScanner(this, scannerView)

        codeScanner.camera = CodeScanner.CAMERA_BACK
        codeScanner.formats = CodeScanner.ALL_FORMATS

        codeScanner.autoFocusMode = AutoFocusMode.SAFE
        codeScanner.scanMode = ScanMode.SINGLE

        codeScanner.isAutoFocusEnabled = true
        codeScanner.isFlashEnabled = false

        codeScanner.decodeCallback = DecodeCallback {
            runOnUiThread {
                Toast.makeText(this, "Scan result: ${it.text}", Toast.LENGTH_LONG).show()}
        }
        scannerView.setOnClickListener {
            codeScanner.startPreview()
        }
    }

    override fun onResume() {
        super.onResume()
        codeScanner.startPreview()
    }

    override fun onPause() {
        codeScanner.releaseResources()
        super.onPause()
    }
}
RunMildew
  • 67
  • 8
  • If I've understood correctly, you'll need to start an Activity with an implicit Intent as in this [example](https://stackoverflow.com/questions/3004515/sending-an-intent-to-browser-to-open-specific-url). I suggest looking at implicit Intents because they are very powerful. I can't answer your question specifically because I don't know Kotlin. – Nicola Fanelli Jan 04 '22 at 19:13
  • 1
    Old, but almost certainly still just as easy: https://stackoverflow.com/a/2051953/608312 – Jake Lee Jan 04 '22 at 20:40
  • @JakeLee While this is a solution that works, it's mainly rooted in the ZXing project library. In my case, I'm using [this library](https://github.com/yuriy-budiyev/code-scanner), albeit it is based on the ZXing library. Also, how exactly would I carry this out in Kotlin? – RunMildew Jan 05 '22 at 16:05

1 Answers1

0

I used this answer to build a searchWeb function within my MainActivity.kt class like so;

fun searchWeb(query: String) {

        val url = "http://www.google.com"
        val intent = Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url))
        startActivity(intent)
        
    }

I then called the function within my codeScanner.decodeCallback like so;

codeScanner.decodeCallback = DecodeCallback {
            runOnUiThread {
                Toast.makeText(this, "Scan result: ${it.text}", Toast.LENGTH_LONG).show()
            }
            searchWeb(it.text)
        }

After doing this, the implicit intent was launched and the QR application launched the internet browser.

RunMildew
  • 67
  • 8