Qrcode Generator and Reader Android Example: Zxing
Qrcode Generator and Reader Android Example: Zxing
EXAMPLE
MAY 26, 2018
Hello guy’s, today we gonna look out how to work with QRCode. Recently, I’ve been working on my
startup. My startup is based on Wallet App. So, I had to implement QRCode generator and reader on
the client side. When creating the app I had faced a lot difficulty to find the right solution. So, I thought
So, let’s see how can we generate and read QRCode in Android app with the custom object. In this
example, we’re going to use Zxing library for reading and generating QRCode.
First, add the following dependency in your app level build.gradle file.
READING QRCODE
1. <me.dm7.barcodescanner.zxing.ZXingScannerView
2. android:id="@+id/qrCodeScanner"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent">
5.
6. </me.dm7.barcodescanner.zxing.ZXingScannerView>
Now get the ZxingScannerView reference in the activity class.
1. /**
2. * Set Qr code scanner basic properties.
3. */
4.
5. private fun setScannerProperties() {
6. qrCodeScanner.setFormats(listOf(BarcodeFormat.QR_CODE))
7. qrCodeScanner.setAutoFocus(true)
8. qrCodeScanner.setLaserColor(R.color.colorAccent)
9. qrCodeScanner.setMaskColor(R.color.colorAccent)
10. if (Build.MANUFACTURER.equals(HUAWEI, ignoreCase = true))
11. qrCodeScanner.setAspectTolerance(0.5f)
12. }
Now we need to start the scanner. It’s a good approach that we start scanner in onResume and stop the
scanner in on onPause.
You see the setResultHandler is accepting the RequestHandler type interface and bypassing “this”
The following shows how to stop scanner in onStop or in the onPause method.
1. /**
2. * stop the qr code camera scanner when activity is in the onPause state.
3. */
4. override fun onPause() {
5. super.onPause()
6. qrCodeScanner.stopCamera()
7. }
GENERATING QRCODE
Generating a QR code is very easy. You guys must be remembered that I said, we’re going to
generate QRCode with Custom Object. By, custom object means we can store user info like name,
email,image_url etc in QRCode. So, in this example, we’re going to store user full name and age
in QRCode. So, when we scan the QRCode, we get user info in the Result object.
Now, this is another activity for generating the QR code. Below is code for the xml file.
Now in this xml, we have one EditText for the full name and one for user age. We have
an ImageView where we show the QR code and one Button for generating QR code.
I’m gonna show you quickly how to generate QR code except for the prep work.
1. generateQrCodeButton.setOnClickListener {
2. if (checkEditText()) {
3. val user = UserObject(fullName = fullNameEditText.text.toString(), age = Integer.parseInt(ageEditText.text.toString()))
4. val serializeString = Gson().toJson(user)
5. val bitmap = QRCodeHelper
6. .newInstance(this)
7. .setContent(encryptedString)
8. .setErrorCorrectionLevel(ErrorCorrectionLevel.Q)
9. .setMargin(2)
10. .getQRCOde()
11. qrCodeImageView.setImageBitmap(bitmap)
12. }
13. }
You see in click listener we’re creating a User object for QR code. We can only create QR
code with String. So, we have to serialize the User object into a string with Gson. After that, we need
to set QR code setting for generating the code. The QRCodeHelper is my custom class for the
generating QR code.
Note: You see we’re serializing the user object when generating the code. So, we’ve to deserialize the
string into User object when the scanner successfully scanned the QR code. One more important thing
to say here please do not create a huge object for creating QR code. If you create a huge object your
That’s it guy’s, this is my demonstration about generating and reading QR code with the custom
object. If you guy’s want to see the code of the example, that I’ve shown you in the above see it
on GitHub.
I hope you guys learn how can we generate and read QR code. If you have any queries please do
comment below.