问题描述
这是我生成QR码的代码。我想生成带有字符串和图像徽标的QR,如下图所示。我也尝试过使用渲染提示,但是结果对此无济于事。
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,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,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)
}
}
}
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",24))
// add string title above qrCode
drawString(graphics,graphics.getFontMetrics().stringWidth(qrCode.cashier),-450,qrCode.cashier,24,"Khmer OS Content"
)
//Register Font
graphics.setFont(new Font("Archive Regular",graphics.getFontMetrics().stringWidth("${qrCode.account_id} | ${qrCode.currency}"),-550,"${qrCode.account_id} | ${qrCode.currency}","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.")
}
这是生成后的结果,缩放图像时,我得到的图像png具有错误的文字和徽标:
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)