# Android Zxing Dependency ###### tags: `Android Dependency` [TOC] # Introduction Barcode/QRcode Dependency for Android. **Supported formats:** ![](https://i.imgur.com/qaRRxS6.png) # How to apply it to the project? **build.gradle(:project):** Add *mavenCentral()* in allprojects and buildscript: ```java= repositories { mavenCentral() google() jcenter() } ``` **manifest.xml:** Allow **hardwareAccelerated="true"** into application sector. ```java= <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:hardwareAccelerated="true" android:supportsRtl="true" android:theme="@style/Theme.AndroidDependencies"> ... </application> ``` **build.gradle(:app):** ```java= //zxing Dependency - Barcode QRCode def zxing_version = '3.3.0' implementation "com.google.zxing:core:${zxing_version}" ``` # Zxing implementation example (QRCODE) By using QRCodeWriter, we can obtain the base for QRCode production. This one by building one will return a BitMatrix object which will have an int array of 0 and 1. But when we do **(bitMatrix.get(x, y))**, this will return boolean and by using this, you will know which ones to make them become white/black. But by making the array is not enough, so you need to send the new array into the bitmap creation and like this the QRCode will be crated. ```java= public static Bitmap createQRCode(String text, int size) { try { Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); hints.put(EncodeHintType.MARGIN, 1); BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints); int[] pixels = new int[size * size]; for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { if (bitMatrix.get(x, y)) { pixels[y * size + x] = 0xff000000; } else { pixels[y * size + x] = 0xffffffff; } } } Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, size, 0, 0, size, size); return bitmap; } catch (WriterException e) { e.printStackTrace(); return null; } } ``` ### Reference [Bitmap Config](https://developer.android.com/reference/android/graphics/Bitmap.Config) **ARGB_8888:** Each pixel is stored on 4 bytes. **EncodeHintType** & **BarcodeFormat** are all from the xzing dependency. # Reference https://github.com/zxing/zxing