|
|
@ -80,15 +80,16 @@ public class CreateQrCodeUtil { |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 用于创建code_128类型的条形码 |
|
|
* 用于创建code_128类型的条形码 |
|
|
* @param content 条形码存储的数据 |
|
|
* @param code 条形码的数据 |
|
|
|
|
|
* @param name 条形码上方的名称 |
|
|
* @param height 高度 |
|
|
* @param height 高度 |
|
|
* @param width 宽度 |
|
|
* @param width 宽度 |
|
|
* @return base64格式的字符串 |
|
|
* @return base64格式的字符串 |
|
|
*/ |
|
|
*/ |
|
|
public static String CreateBarCode128(String content, int height, int width){ |
|
|
public static String CreateBarCode128(String code,String name, int height, int width){ |
|
|
String resultImage = ""; |
|
|
String resultImage = ""; |
|
|
if (!StringUtils.isEmpty(content)) { |
|
|
if (!StringUtils.isEmpty(code)) { |
|
|
BufferedImage image = insertWords(getBarCode(content,width,height), content,width,height,80); |
|
|
BufferedImage image = insertWords(getBarCode(code,width,height), code,name,width,height,120); |
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
|
try { |
|
|
try { |
|
|
ImageIO.write(image, "png", os); |
|
|
ImageIO.write(image, "png", os); |
|
|
@ -149,34 +150,40 @@ public class CreateQrCodeUtil { |
|
|
* 把带logo的二维码下面加上文字 |
|
|
* 把带logo的二维码下面加上文字 |
|
|
* |
|
|
* |
|
|
* @param image 条形码图片 |
|
|
* @param image 条形码图片 |
|
|
* @param words 文字 |
|
|
* @param code 文字 |
|
|
* @return 返回BufferedImage |
|
|
* @return 返回BufferedImage |
|
|
* @author fxbin |
|
|
* @author fxbin |
|
|
*/ |
|
|
*/ |
|
|
public static BufferedImage insertWords(BufferedImage image, String words, int WIDTH, int HEIGHT, int WORD_HEIGHT) { |
|
|
public static BufferedImage insertWords(BufferedImage image, String code,String name, int WIDTH, int HEIGHT, int WORD_HEIGHT) { |
|
|
// 新的图片,把带logo的二维码下面加上文字
|
|
|
// 新的图片,把带logo的二维码下面加上文字
|
|
|
if (org.apache.commons.lang.StringUtils.isNotEmpty(words)) { |
|
|
if (org.apache.commons.lang.StringUtils.isNotEmpty(code)) { |
|
|
BufferedImage outImage = new BufferedImage(WIDTH, WORD_HEIGHT, BufferedImage.TYPE_INT_RGB); |
|
|
BufferedImage outImage = new BufferedImage(WIDTH, WORD_HEIGHT, BufferedImage.TYPE_INT_RGB); |
|
|
Graphics2D g2d = outImage.createGraphics(); |
|
|
Graphics2D g2d = outImage.createGraphics(); |
|
|
// 抗锯齿
|
|
|
// 抗锯齿
|
|
|
setGraphics2D(g2d); |
|
|
setGraphics2D(g2d); |
|
|
// 设置白色
|
|
|
// 设置白色
|
|
|
setColorWhite(g2d); |
|
|
setColorWhite(g2d); |
|
|
// 画条形码到新的面板
|
|
|
// 画条形码到新的面板(并居中)
|
|
|
g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); |
|
|
g2d.drawImage(image, 0, WORD_HEIGHT / 2 -image.getHeight() / 2, image.getWidth(), image.getHeight(), null); |
|
|
// 画文字到新的面板
|
|
|
// 画文字到新的面板
|
|
|
Color color = new Color(0, 0, 0); |
|
|
Color color = new Color(0, 0, 0); |
|
|
g2d.setColor(color); |
|
|
g2d.setColor(color); |
|
|
// 字体、字型、字号
|
|
|
// 字体、字型、字号
|
|
|
g2d.setFont(new Font("微软雅黑", Font.PLAIN, 16)); |
|
|
g2d.setFont(new Font("微软雅黑", Font.PLAIN, 16)); |
|
|
//文字长度
|
|
|
//文字长度
|
|
|
int strWidth = g2d.getFontMetrics().stringWidth(words); |
|
|
int strWidth = g2d.getFontMetrics().stringWidth(code); |
|
|
//总长度减去文字长度的一半 (居中显示)
|
|
|
//总长度减去文字长度的一半 (居中显示)
|
|
|
int wordStartX = (WIDTH - strWidth) / 2; |
|
|
int wordStartX = (WIDTH - strWidth) / 2; |
|
|
//height + (outImage.getHeight() - height) / 2 + 12
|
|
|
//height + (outImage.getHeight() - height) / 2 + 12
|
|
|
int wordStartY = HEIGHT + 20; |
|
|
int wordStartY = WORD_HEIGHT / 2 + image.getHeight() /2 + 16; |
|
|
|
|
|
// 画文字
|
|
|
|
|
|
g2d.drawString(code, wordStartX, wordStartY); |
|
|
|
|
|
|
|
|
|
|
|
strWidth = g2d.getFontMetrics().stringWidth(name); |
|
|
|
|
|
wordStartX = (WIDTH - strWidth) / 2; |
|
|
|
|
|
wordStartY = g2d.getFont().getSize(); |
|
|
// 画文字
|
|
|
// 画文字
|
|
|
g2d.drawString(words, wordStartX, wordStartY); |
|
|
g2d.drawString(name, wordStartX, wordStartY); |
|
|
g2d.dispose(); |
|
|
g2d.dispose(); |
|
|
outImage.flush(); |
|
|
outImage.flush(); |
|
|
return outImage; |
|
|
return outImage; |
|
|
@ -207,7 +214,7 @@ public class CreateQrCodeUtil { |
|
|
public static void setColorWhite(Graphics2D g2d) { |
|
|
public static void setColorWhite(Graphics2D g2d) { |
|
|
g2d.setColor(Color.WHITE); |
|
|
g2d.setColor(Color.WHITE); |
|
|
//填充整个屏幕
|
|
|
//填充整个屏幕
|
|
|
g2d.fillRect(0, 0, 302, 113); |
|
|
g2d.fillRect(0, 0, 302, 120); |
|
|
//设置笔刷
|
|
|
//设置笔刷
|
|
|
g2d.setColor(Color.BLACK); |
|
|
g2d.setColor(Color.BLACK); |
|
|
} |
|
|
} |
|
|
|