- 6 years ago
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' |
<?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>
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); } }
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); } } }
Hot Questions