Question Detail

How to generate QR Code and read in android?

6 years ago Views 1941 Visit Post Reply

I have an Encrypted value which is very lengthy, Every time user should enter that key that is not user-friendly so we decide to create QR code and Scan it for input. I have tried many things but not able to Generate QR Code of Simple String. 
How can I generate QR Code and read it from Scanner?
Image result for mobile qr code


Thread Reply

Correct Answer

Bili Greed

- 6 years ago

Add ZXING library

We need to add ZXING library in our project, so open build.gradle(Module:app) file and add following code:

repositories{
		 maven{
			 url "https://jitpack.io" }
			}
			dependencies{
			.....
			compile 'com.github.kenglxn.QRGen:android:2.2.0'
 

compile 'com.journeyapps:zxing-android-embedded:3.5.0'

  .....

}


XML for Generate QR Code:- (activity_qrcode_show.xml)
 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="in.milkywayservices.vbagetech.fortunepay.QRCodeShowActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>



Activity to Generate QR Code:- 

public class QRCodeShowActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode_show);
        Bitmap myBitmap= QRCode.from("http://qna.vbagetech.com").bitmap();
        ImageView myImage=(ImageView) findViewById(R.id.imageView);
        myImage.setImageBitmap(myBitmap);
    }
}




Activity To scan QR Code :- 

public class QRScanActivity extends AppCompatActivity {
    //qr code scanner object
    private IntentIntegrator qrScan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrscan);
        //intializing scan object
        qrScan = new IntentIntegrator(this);
//initiating the qr code scan
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
        integrator.setPrompt("Scan a Barcode");  // Use a specific camera of the device
        integrator.setBeepEnabled(true);
        integrator.initiateScan();
    }
    //Getting the scan results
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            //if qrcode has nothing in it
            if (result.getContents() == null) {
                Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
            } else {
               Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}