public void GenerateQRCodeLabel(HttpServletResponse response, Map<String, String> params) {
try {
int width = 360; // 图片宽
int height = 220;// 图片高
String content = params.get("ID");//二维码内容
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
content=new String(content.getBytes("UTF-8"),"ISO-8859-1");
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, 135, 135, hintMap);
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth-20, matrixWidth-20, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++){
for (int j = 0; j < matrixWidth; j++){
if (byteMatrix.get(i, j)){
graphics.fillRect(i-10, j-10,1, 1);
}
}
}
image.flush();
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
bi.createGraphics();
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setColor(Color.WHITE); // 设置背景颜色
g2.fillRect(0, 0, width, height);// 填充整张图片
g2.setColor(Color.black);// 设置字体颜色
g2.setStroke(new BasicStroke(2.0f)); // 边框加粗
g2.drawRect(1, 1, width - 2, height - 2); // 画边框就是黑边框
g2.setStroke(new BasicStroke(0.0f)); // 边框不需要加粗
g2.drawLine(120, 40, 400, 40);//第二条横线
g2.drawLine(120, 80, 400, 80);//第三条横线
g2.drawLine(0, 120, 400, 120);//第四条横线
g2.drawLine(0, 170, 400, 170);//第五条横线
g2.drawLine(120, 0, 120, 250); // 从左到右第一个竖线
g2.drawLine(200, 40, 200, 120); // 从左到右第二个竖线
// 设置标题的字体,字号,大小
Font titleFont = new Font("宋体", Font.BOLD, 14);
g2.setFont(titleFont);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿
String QYMC = params.get("QYMC");
g2.drawString(QYMC, 130, 27);
String PKMC = params.get("PKMC");
g2.drawString("排口名称 "+PKMC, 130, 65);
String PKBH = params.get("PKBH");
g2.drawString("排口编号 "+PKBH, 130, 105);
g2.drawString("排口位置", 20, 150);
g2.drawString("经纬度信息", 20, 200);
g2.drawString(StringUtil.isEmpty(params.get("PKWZ"))?"-":params.get("PKWZ"), 130, 150);
g2.drawString(StringUtil.isEmpty(params.get("PKJD"))?"-":params.get("PKJD"), 130, 190);
g2.drawString(StringUtil.isEmpty(params.get("PKWD"))?"-":params.get("PKWD"), 130, 210);
// 画文字到新的面板
g2.drawImage(image, 2, 2,image.getWidth(), image.getHeight(), null);
g2.dispose(); // 释放对象
String fileName = URLEncoder.encode(PKMC + PKBH +"-QRCode.jpg", "UTF-8");
response.setHeader("Content-Type","image/png");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", bos);
response.getOutputStream().write(bos.toByteArray());
bos.flush();
bos.close();
}catch (Exception e){
e.printStackTrace();
}
}
Java后台生成二维码标签(依赖com.google.zxing)
最新推荐文章于 2024-06-03 17:03:28 发布