Android, How to Read Qr Code in My Application

Android, How to read QR code in my application?

try {

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); // "PRODUCT_MODE for bar codes

startActivityForResult(intent, 0);

} catch (Exception e) {

Uri marketUri = Uri.parse("market://details?id=com.google.zxing.client.android");
Intent marketIntent = new Intent(Intent.ACTION_VIEW,marketUri);
startActivity(marketIntent);

}

and in onActivityResult():

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0) {

if (resultCode == RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
}
if(resultCode == RESULT_CANCELED){
//handle cancel
}
}
}

Android start application from QR Code with params

Create QR CODE with this text : myApp://extraString and read it with any qr code reader. Or even you can integrate your own qr code reader using Zxing's open source. And you can get the extraString as @Sean Owen mentioned using getIntent().getDataString(). And don't forget to add this in your manifest file :

<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="myApp"/>
</intent-filter>

That should work.

How to read or decode QR Code in Android without the use of any 3rd party app?

Google has a barcode class included in the Google Play services under the namespace com.google.android.gms.vision.barcode. I'm using it myself in a production app and its just great! Its fast, robust and handles all from blurry to damaged codes.

Check out Android QR Code Reader Made Easy. This should get you up and running in no time! You can easily continue on the code base provided or equally easily integrate it in your existing project.

How to launch implicit intent from a QR Code result?

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.



Related Topics



Leave a reply



Submit