Here is my code to generate QR code. I want to generate QR with string and image logo as below image. I have try to use rendering hint also but the result can not help on this.
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE)
def generateQrCodePNG(QRCodeModel qrCode) {
int sizeW = 563
int sizeH = 644
try {
BitMatrix byteMatrix = createBitMatrixPNG(qrCode.QRText, sizeW,sizeH)
Integer CrunchifyWidth = byteMatrix.getWidth()
Integer CrunchifyHeight = byteMatrix.getHeight()
BufferedImage image = new BufferedImage(CrunchifyWidth, CrunchifyHeight,
BufferedImage.TYPE_INT_ARGB)
Graphics2D graphics = (Graphics2D) image.getGraphics()
graphics.setColor(Color.WHITE)
graphics.fillRect(0, 0, CrunchifyWidth, CrunchifyHeight)
//clear font
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment()
//Register Font
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("${servletContext.getRealPath("/")}/assets/fonts/KhmerOS_content.ttf")))
graphics.setFont(new Font("Khmer OS Content", Font.PLAIN, 28))
// add string title above qrCode
drawString(graphics ,
graphics.getFontMetrics().stringWidth(qrCode.storeName) ,
450,CrunchifyWidth , CrunchifyHeight,qrCode.storeName ,
28,
"Khmer OS Content",
Color.BLACK
)
Integer h1 = sizeH
Integer w1 = sizeW
Integer x1 = (Integer) ((sizeW) / 2 - w1 / 2)
Integer y1 = (Integer) ((sizeH) / 2 - h1 / 2)
float dash1 = 10.0f
graphics.setColor(Color.BLACK)
BasicStroke dashed =
new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash1 as float[], 0.0f)
graphics.setStroke(dashed)
graphics.draw(new RoundRectangle2D.Double(x1, y1,
CrunchifyWidth - 2 ,
CrunchifyHeight - 5 ,
1, 1))
graphics.setColor(Color.BLACK)
for (int i = 0; i < CrunchifyWidth; i++) {
for (int j = 0; j < CrunchifyHeight; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i, j, 1, 1)
}
}
}
if(qrCode.qr_type == Constant.QR_WITH_LOGO) {
// set white background color
graphics.setColor(Color.white)
Integer h = 60
Integer w = 60
Integer x = (Integer) (CrunchifyWidth / 2 - w / 2)
Integer y = (Integer) (CrunchifyHeight / 2 - h / 2)
graphics.fillRoundRect(x,y,w,h, 10, 10)
String logo = "${servletContext.getRealPath("/")}/assets/logo/AMK Mobile-Logo-high 1.png"
//add image into qrCode
File fileLogo = new File(logo)
Image logoImage1 = ImageIO.read(fileLogo)
graphics.setColor(Color.white)
Integer imgH = 55
Integer imgW = 55
Integer imgX = (Integer) (CrunchifyWidth / 2 - imgW / 2)
Integer imgY = (Integer) (CrunchifyHeight / 2 - imgH / 2)
graphics.drawImage(logoImage1, imgX, imgY, imgW, imgH, null)
}
//Register Font
graphics.setFont(new Font("Khmer OS Content", Font.PLAIN, 24))
// add string title above qrCode
drawString(graphics ,
graphics.getFontMetrics().stringWidth(qrCode.cashier) ,
-450,CrunchifyWidth , CrunchifyHeight,qrCode.cashier ,
24,
"Khmer OS Content"
)
//Register Font
graphics.setFont(new Font("Archive Regular", Font.PLAIN, 24))
// add string title above qrCode
drawString(graphics ,
graphics.getFontMetrics().stringWidth("${qrCode.account_id} | ${qrCode.currency}") ,
-550,CrunchifyWidth , CrunchifyHeight,"${qrCode.account_id} | ${qrCode.currency}" ,
24,
"Archive Regular"
)
graphics.dispose()
File myFile = new File(qrCode.file_path + qrCode.file_name)
ImageIO.write(image, "PNG", myFile)
} catch (WriterException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
System.out.println("\n\nYou have successfully created QR Code.")
}
Here is the result after generated, I got image png with bad quality text and logo when zoom the image: