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
| int magnify = 1; //The resolution of the QRCode
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix;
// The QR should be multiplied up to fit, with extra padding if necessary
int size = 180;
matrix = writer.encode("Un p'tit bonjour de Tours !", BarcodeFormat.QR_CODE, size, size, null);
//Make the BufferedImage that are to hold the QRCode
BufferedImage im = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
im.createGraphics();
Graphics2D g = (Graphics2D) im.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, size, size);
//paint the image using the ByteMatrik
for (int h = 0; h < matrix.getHeight(); h++) {
for (int w = 0; w < matrix.getWidth(); w++) {
//Find the colour of the dot
if (matrix.get(h, w) == false) {
g.setColor(Color.WHITE);
} else {
g.setColor(Color.BLACK);
}
//Paint the dot
g.fillRect(h * magnify, w * magnify, magnify, magnify);
}
}
ImageIO.write(im, "png", new File("testQRCodeWriter.png")); |
Partager