What is QR Code?
QR is an acronym for Quick Response. It’s a smarter version of the barcode, and it was originally developed in Japan for the automotive industry. Machines can read QR codes more quickly than barcodes, and QR codes can store more data in less space too.
Implementing QR Scanner by using ZXING Library- it provides easiest way to implement a QR code Scanner for developers.
following steps to use this library.
- add the dependency to app level build.gradle
1compile 'com.dlazaro66.qrcodereaderview:qrcodereaderview:2.0.3'
-
- Add a “QRCodeReaderView” in the layout editor like you actually do with a button for example.
- In your onCreate method, you can find the view, as usual, using findViewById() function.
- Create an Activity which implements,
onQRCodeReadListener
and let implements required methods or set aonQRCodeReadListener
to the QRCodeReaderView object.
1234<com.dlazaro66.qrcodereaderview.QRCodeReaderViewandroid:id="@+id/qrdecoderview"android:layout_width="match_parent"android:layout_height="match_parent" />
- Make sure you have camera permissions in order to use the library.
- Start & Stop camera preview in onPause() and onResume() overriden methods.
- You can place widgets or views over QRDecoderView.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
public class QRDecoderActivity extends Activity implements OnQRCodeReadListener { private QRCodeReaderView qrCodeReaderView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_decoder); qrCodeReaderView = (QRCodeReaderView) findViewById(R.id.qrdecoderview); qrCodeReaderView.setOnQRCodeReadListener(this); // Use this function to enable/disable decoding qrCodeReaderView.setQRDecodingEnabled(true); // Use this function to change the autofocus interval (default is 5 secs) qrCodeReaderView.setAutofocusInterval(2000L); // Use this function to enable/disable Torch qrCodeReaderView.setTorchEnabled(true); // Use this function to set front camera preview qrCodeReaderView.setFrontCamera(); // Use this function to set back camera preview qrCodeReaderView.setBackCamera(); } // Called when a QR is decoded // "text" : the text encoded in QR // "points" : points where QR control points are placed in View @Override public void onQRCodeRead(String text, PointF[] points) { // add here your source code with scanning code } @Override protected void onResume() { super.onResume(); qrCodeReaderView.startCamera(); } @Override protected void onPause() { super.onPause(); qrCodeReaderView.stopCamera(); } } |
References: https://github.com/dlazaro66/QRCodeReaderView