public static String createQrcodeImg(String content,String filename,String logoPath,int imgHeight,int imgWidth){
String retstr = "";
File file = new File(filename);
MultiFormatWriter multiFormatWriter = null;
BitMatrix bm = null;
BufferedImage image = null;
try {
multiFormatWriter = new MultiFormatWriter();
// 用于设置QR二维码參数
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
// 设置QR二维码的纠错级别(H为最高级别)详细级别信息
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
// 设置编码方式
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 0);
//hints.put(EncodeHintType.MAX_SIZE, 350);
//hints.put(EncodeHintType.MIN_SIZE, 100);
// 參数顺序分别为:编码内容。编码类型,生成图片宽度,生成图片高度,设置參数
bm = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, imgWidth, imgHeight,hints);
int w = bm.getWidth();
int h = bm.getHeight();
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
// 開始利用二维码数据创建Bitmap图片,分别设为黑白两色
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
image.setRGB(x, y, bm.get(x, y) ?Color.BLACK.getRGB() : Color.WHITE.getRGB());
}
}
//this.addLogo_QRCode(image, new File(zxingconfig.getLogoPath()), zxingconfig.getLogoConfig());
// 对象流传输
BufferedImage imagelogo = image;
Graphics2D g = imagelogo.createGraphics();
// 读取Logo图片
File logofile = new File(logoPath);
BufferedImage logo = ImageIO.read(logofile);
// 设置logo的大小,本人设置为二维码图片的20%,由于过大会盖掉二维码
int widthLogo = logo.getWidth(null) > imagelogo.getWidth() * 2 / 10 ? (imagelogo.getWidth() * 2 / 10) : logo.getWidth(null), heightLogo = logo
.getHeight(null) > imagelogo.getHeight() * 2 / 10 ?
(imagelogo.getHeight() * 2 / 10) : logo.getWidth(null);
// 计算图片放置位置
// logo放在中心
int x = (imagelogo.getWidth() - widthLogo) / 2;
int y = (imagelogo.getHeight() - heightLogo) / 2;
// 開始绘制图片
g.drawImage(logo, x, y, widthLogo, heightLogo, null);
g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15);
//g.setStroke(new BasicStroke(2));
//g.setColor(Color.WHITE);
g.drawRect(x, y, widthLogo, heightLogo);
g.dispose();
logo.flush();
imagelogo.flush();
ImageIO.write(image, "png", file); // 图片写出
Thread.sleep(500);// 缓冲
//zp.parseQR_CODEImage(bim);
retstr = SysUtil.RET_SUCCESS;
} catch (WriterException e) {
log.error("error",e);
retstr = SysUtil.RET_FAIL;
}
return retstr;
}
二维码带log
猜你喜欢
转载自blog.csdn.net/ZQ200720/article/details/130622520
今日推荐
周排行