QRCodeUtils { public static Bitmap generateQRBitMap(String data, int sideLength) throws IOException, WriterException { final long time = System.currentTimeMillis(); String compressedData = CompressionUtils.compress(data); if (compressedData.length() > 4000) { throw new IOException(Collect.getInstance().getString(R.string.encoding_max_limit)); } Map<EncodeHintType, ErrorCorrectionLevel> hints = new HashMap<>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix bitMatrix = qrCodeWriter.encode(compressedData, BarcodeFormat.QR_CODE, sideLength, sideLength, hints); Bitmap bmp = Bitmap.createBitmap(sideLength, sideLength, Bitmap.Config.RGB_565); for (int x = 0; x < sideLength; x++) { for (int y = 0; y < sideLength; y++) { bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } Timber.i("QR Code generation took : %d ms", System.currentTimeMillis() - time); return bmp; } private  QRCodeUtils(); static String decodeFromBitmap(Bitmap bitmap); static Bitmap generateQRBitMap(String data, int sideLength); static Observable<Bitmap> getQRCodeGeneratorObservable(Collection<String> selectedPasswordKeys); static final String QR_CODE_FILEPATH; }
@Test public void generateAndDecodeQRCode() throws IOException, WriterException, DataFormatException, ChecksumException, NotFoundException, FormatException { String data = "Some random text"; Bitmap generatedQRBitMap = QRCodeUtils.generateQRBitMap(data, 100); assertQRContains(generatedQRBitMap, data); }
@Test public void readQRCodeFromDiskIfCacheExists() throws NoSuchAlgorithmException, IOException, WriterException { String expectedData = "{\"general\":{},\"admin\":{}}"; new File(Collect.SETTINGS).mkdirs(); FileUtils.saveBitmapToFile(QRCodeUtils.generateQRBitMap(expectedData, 100), QR_CODE_FILEPATH); FileUtils.write(md5File, getDigest(expectedData.getBytes())); assertTrue(savedQrCodeImage.exists()); assertTrue(md5File.exists()); final long lastModifiedQRCode = savedQrCodeImage.lastModified(); final long lastModifiedCache = md5File.lastModified(); final GenerationResults generationResults = new GenerationResults(); generateQrCode(generationResults); assertEquals(lastModifiedCache, md5File.lastModified()); assertEquals(lastModifiedQRCode, savedQrCodeImage.lastModified()); verifyCachedMd5Data(expectedData); }