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
Bypass Android's Hidden API Restrictions
App Crash After Activity Has Been Killed in Background
Using Asynctask to Speed Up Android App Launch Time
Equivalent of Listview.Setemptyview in Recyclerview
Android How to Identify Item in Listview with Checkbox
Android Browser Textarea Scrolls All Over The Place, Is Unusable
How to Implement Search in Customlistview Based on Class Item of Pojo Class in Android
Modifying The Color of an Android Drawable
How Many Activities Vs Fragments
<Select> Not Working in Phonegap App on Android 2.3.3
Android Emulator Failed to Allocate Memory 8
How to Create Custom Spinner Like Border Around The Spinner with Down Triangle on The Right Side
How to Add Apache Commons Collections in Android Studio (Gradle)
Phonegap & Android - How to Use The New Select List Style