package org.ripple.power.qr; import java.awt.image.BufferedImage; import java.math.BigDecimal; import com.google.zxing.WriterException; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import com.google.zxing.qrcode.encoder.ByteMatrix; import com.google.zxing.qrcode.encoder.Encoder; import com.google.zxing.qrcode.encoder.QRCode; public class SendCodeGenerator { private static final int QUIET_ZONE_SIZE = 4; private static int QR_CODE_ELEMENT_MULTIPLE = 2; private QRCode code; public SendCodeGenerator() { code = new QRCode(); } public BufferedImage generateQRcode(String address, String amount, String label) { return generateQRcode(address, amount, label, 1); } public BufferedImage generateQRcode(String address, String amount, String label, int scaleFactor) { String bitcoinURI = ""; ByteMatrix matrix; try { bitcoinURI = RippleURI.convertToBitcoinURI(address, new BigDecimal( amount), label, null); matrix = encode(bitcoinURI); } catch (com.google.zxing.WriterException e) { return null; } catch (IllegalArgumentException e) { return null; } int matrixWidth = matrix.getWidth(); int matrixHeight = matrix.getHeight(); int swatchWidth = matrixWidth * scaleFactor; int swatchHeight = matrixHeight * scaleFactor; BufferedImage image = new BufferedImage(swatchWidth, swatchHeight, BufferedImage.TYPE_INT_RGB); for (int y = 0; y < matrixHeight; y++) { for (int x = 0; x < matrixWidth; x++) { byte imageValue = matrix.get(x, y); for (int scaleX = 0; scaleX < scaleFactor; scaleX++) { for (int scaleY = 0; scaleY < scaleFactor; scaleY++) { image.setRGB(x * scaleFactor + scaleX, y * scaleFactor + scaleY, imageValue); } } } } return image; } /** * This object renders a QR Code as a ByteMatrix 2D array of greyscale * values. * * @author dswitkin@google.com (Daniel Switkin) */ public ByteMatrix encode(String contents) throws WriterException { if (contents == null || contents.length() == 0) { throw new IllegalArgumentException("Found empty contents"); } Encoder.encode(contents, ErrorCorrectionLevel.L); return renderResult(code, QR_CODE_ELEMENT_MULTIPLE); } // Note that the input matrix uses 0 == white, 1 == black, while the output // matrix uses // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap). private static ByteMatrix renderResult(QRCode code, int multiple) { ByteMatrix input = code.getMatrix(); int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int qrWidth = multiple * inputWidth + (QUIET_ZONE_SIZE << 1); int qrHeight = multiple * inputHeight + (QUIET_ZONE_SIZE << 1); ByteMatrix output = new ByteMatrix(qrWidth, qrHeight); byte[][] outputArray = output.getArray(); // We could be tricky and use the first row in each set of multiple as // the temporary storage, // instead of allocating this separate array. byte[] row = new byte[qrWidth]; // 1. Write the white lines at the top for (int y = 0; y < QUIET_ZONE_SIZE; y++) { setRowColor(outputArray[y], (byte) 255); } // 2. Expand the QR image to the multiple byte[][] inputArray = input.getArray(); for (int y = 0; y < inputHeight; y++) { // a. Write the white pixels at the left of each row for (int x = 0; x < QUIET_ZONE_SIZE; x++) { row[x] = (byte) 255; } // b. Write the contents of this row of the barcode int offset = QUIET_ZONE_SIZE; for (int x = 0; x < inputWidth; x++) { byte value = (inputArray[y][x] == 1) ? 0 : (byte) 255; for (int z = 0; z < multiple; z++) { row[offset + z] = value; } offset += multiple; } // c. Write the white pixels at the right of each row offset = QUIET_ZONE_SIZE + (inputWidth * multiple); for (int x = offset; x < qrWidth; x++) { row[x] = (byte) 255; } // d. Write the completed row multiple times offset = QUIET_ZONE_SIZE + (y * multiple); for (int z = 0; z < multiple; z++) { System.arraycopy(row, 0, outputArray[offset + z], 0, qrWidth); } } // 3. Write the white lines at the bottom int offset = QUIET_ZONE_SIZE + (inputHeight * multiple); for (int y = offset; y < qrHeight; y++) { setRowColor(outputArray[y], (byte) 255); } return output; } private static void setRowColor(byte[] row, byte value) { for (int x = 0; x < row.length; x++) { row[x] = value; } } }