public static BufferedImage toSingleQrCodeBufferedImage(String s, ErrorCorrectionLevel ec, int scaleFactor) throws WriterException { QRCode qrCode = new QRCode(); Encoder.encode(s, ec, qrCode); BufferedImage bufferedImage=MatrixToImageWriter.toBufferedImage(qrCode.getMatrix()); if (scaleFactor!=1) { int newWidth=bufferedImage.getWidth()*scaleFactor; int newHeight=bufferedImage.getHeight()*scaleFactor; Image image=bufferedImage.getScaledInstance(newWidth, newHeight, Image.SCALE_FAST); bufferedImage=ImageIoUtils.toBufferedImage(image); } return(bufferedImage); }
/** * Generates a QR-Code-Image from given String * * @param url * @param width * @param height * @throws WriterException */ public static Bitmap getQRImage(String url, int size) { Bitmap mBitmap; BitMatrix bitMatrix; @SuppressWarnings("unused") QRCode qr = new QRCode(); QRCodeWriter writer = new QRCodeWriter(); mBitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888); try { bitMatrix = writer.encode(url, BarcodeFormat.QR_CODE, size, size); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { mBitmap.setPixel(i, j, bitMatrix.get(i, j) ? Color.BLACK : Color.WHITE); } } } catch (WriterException e) { e.printStackTrace(); } return mBitmap; }
/** * Generate a QR code with the payment information of a given size, optionally, with branded * @param paymentString A SPAYD string with payment information * @return An image with the payment QR code * @throws SpaydQRException */ public BufferedImage createQRCode(String paymentString) { notEmpty(paymentString); final BitMatrix matrix; final int barsize; final Writer writer = new MultiFormatWriter(); try { final Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, "ISO-8859-1"); final QRCode code = Encoder.encode(paymentString, ErrorCorrectionLevel.M, hints); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); barsize = size / (code.getMatrix().getWidth() + 8); matrix = writer.encode(paymentString, BarcodeFormat.QR_CODE, size, size, hints); } catch (WriterException e) { throw new SpaydQRException("Unable to create QR code", e); } final BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix); return isBranded() ? brandImage(image, barsize) : image; }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case TAKE_PICTURE: try { mOriginal = BitmapFactory.decodeStream(getAssets().open("image/000.jpg")); mOriginalIv.setImageBitmap(mOriginal); QRCode qrcode = encodeQrcode(null); Bitmap composedBmp = makeFaceQRCodeBt(mOriginal, null); mPreviewIv.setImageBitmap(composedBmp); } catch (Exception e) { e.printStackTrace(); } break; } } }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case TAKE_PICTURE: mOriginal = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/test/1000.jpg"); mOriginalIv.setImageBitmap(mOriginal); QRCode qrcode = encodeQrcode(null); Bitmap composedBmp = composeBinarization(null, qrcode); mPreviewIv.setImageBitmap(composedBmp); break; } } }
/** * @param contents Contents to encode. * @param errorCorrectionLevel ErrorCorrectionLevel * @return QR code object. * @throws WriterException Refer to the messages below. */ private static QRCode getProtoQRCode(String contents, ErrorCorrectionLevel errorCorrectionLevel) throws WriterException { if (contents.isEmpty()) { throw new IllegalArgumentException("Found empty contents"); } Hashtable<EncodeHintType, Object> hintMap = new Hashtable<>(); hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hintMap.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel); return Encoder.encode(contents, errorCorrectionLevel, hintMap); }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException { if (contents.isEmpty()) { throw new IllegalArgumentException("Found empty contents"); } if (format != BarcodeFormat.QR_CODE) { throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format); } if (width < 0 || height < 0) { throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height); } ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; int quietZone = QUIET_ZONE_SIZE; if (hints != null) { if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) { errorCorrectionLevel = ErrorCorrectionLevel.valueOf(hints.get(EncodeHintType.ERROR_CORRECTION).toString()); } if (hints.containsKey(EncodeHintType.MARGIN)) { quietZone = Integer.parseInt(hints.get(EncodeHintType.MARGIN).toString()); } } QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints); return renderResult(code, width, height, quietZone); }
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) { ByteMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = inputWidth + (quietZone * 2); int qrHeight = inputHeight + (quietZone * 2); int outputWidth = Math.max(width, qrWidth); int outputHeight = Math.max(height, qrHeight); int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight); // Padding includes both the quiet zone and the extra white pixels to accommodate the requested // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will // handle all the padding from 100x100 (the actual QR) up to 200x160. int leftPadding = (outputWidth - (inputWidth * multiple)) / 2; int topPadding = (outputHeight - (inputHeight * multiple)) / 2; BitMatrix output = new BitMatrix(outputWidth, outputHeight); for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) { // Write the contents of this row of the barcode for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) { if (input.get(inputX, inputY) == 1) { output.setRegion(outputX, outputY, multiple, multiple); } } } return output; }
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) { ByteMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = inputWidth + (quietZone << 1); int qrHeight = inputHeight + (quietZone << 1); int outputWidth = Math.max(width, qrWidth); int outputHeight = Math.max(height, qrHeight); int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight); int leftPadding = (outputWidth - (inputWidth * multiple)) / 2; int topPadding = (outputHeight - (inputHeight * multiple)) / 2; BitMatrix output = new BitMatrix(outputWidth, outputHeight); int inputY = 0; int outputY = topPadding; while (inputY < inputHeight) { int inputX = 0; int outputX = leftPadding; while (inputX < inputWidth) { if (input.get(inputX, inputY) == (byte) 1) { output.setRegion(outputX, outputY, multiple, multiple); } inputX++; outputX += multiple; } inputY++; outputY += multiple; } return output; }
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) { ByteMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = inputWidth + (quietZone * 2); int qrHeight = inputHeight + (quietZone * 2); int outputWidth = Math.max(width, qrWidth); int outputHeight = Math.max(height, qrHeight); int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight); int leftPadding = (outputWidth - (inputWidth * multiple)) / 2; int topPadding = (outputHeight - (inputHeight * multiple)) / 2; BitMatrix output = new BitMatrix(outputWidth, outputHeight); int inputY = 0; int outputY = topPadding; while (inputY < inputHeight) { int inputX = 0; int outputX = leftPadding; while (inputX < inputWidth) { if (input.get(inputX, inputY) == (byte) 1) { output.setRegion(outputX, outputY, multiple, multiple); } inputX++; outputX += multiple; } inputY++; outputY += multiple; } return output; }
public static void main(String[] args) throws Exception { final BufferedImage background = ImageIO.read(Main.class.getResource("nyan_sakamoto.png")); Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.QR_VERSION, 5); QRCode qrCode = Encoder.encode("Hello world, UniqR!", ErrorCorrectionLevel.H, hints); UniqR<BufferedImage> uniqR = new UniqR<>(new JavaSEPlatform(), background, new QrCodeData(qrCode)); uniqR.setQrPatternColor(0xFF003366); uniqR.setScale(3); uniqR.setPadding(100); showImage(uniqR.build().produceResult(), "Image"); }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException { if (contents.isEmpty()) { throw new IllegalArgumentException("Found empty contents"); } if (format != BarcodeFormat.QR_CODE) { throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format); } if (width < 0 || height < 0) { throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height); } ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; int quietZone = QUIET_ZONE_SIZE; if (hints != null) { ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION); if (requestedECLevel != null) { errorCorrectionLevel = requestedECLevel; } Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN); if (quietZoneInt != null) { quietZone = quietZoneInt; } } QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints); return renderResult(code, width, height, quietZone); }
@Override public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException { if (contents.length() == 0) { throw new IllegalArgumentException("Found empty contents"); } if (format != BarcodeFormat.QR_CODE) { throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format); } if (width < 0 || height < 0) { throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + height); } ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; int quietZone = QUIET_ZONE_SIZE; if (hints != null) { ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION); if (requestedECLevel != null) { errorCorrectionLevel = requestedECLevel; } Integer quietZoneInt = (Integer) hints.get(EncodeHintType.MARGIN); if (quietZoneInt != null) { quietZone = quietZoneInt; } } QRCode code = Encoder.encode(contents, errorCorrectionLevel, hints); return renderResult(code, width, height, quietZone); }
private static BitMatrix renderResult(QRCode code, int width, int height, int quietZone) { ByteMatrix input = code.getMatrix(); if (input == null) { throw new IllegalStateException(); } int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = inputWidth + (quietZone << 1); int qrHeight = inputHeight + (quietZone << 1); int outputWidth = Math.max(width, qrWidth); int outputHeight = Math.max(height, qrHeight); int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight); // Padding includes both the quiet zone and the extra white pixels to accommodate the requested // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will // handle all the padding from 100x100 (the actual QR) up to 200x160. int leftPadding = (outputWidth - (inputWidth * multiple)) / 2; int topPadding = (outputHeight - (inputHeight * multiple)) / 2; BitMatrix output = new BitMatrix(outputWidth, outputHeight); for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) { // Write the contents of this row of the barcode for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) { if (input.get(inputX, inputY) == 1) { output.setRegion(outputX, outputY, multiple, multiple); } } } return output; }