首先引入maven依赖
依赖1
< dependency >
< groupId>com.google.zxing< /groupId>
< artifactId>javase< /artifactId>
< version>3.3.1< /version>
< /dependency> .
依赖2
< dependency>
< groupId>com.google.zxing< /groupId>
< artifactId>core< /artifactId>
< version>3.3.1< /version>
< /dependency>
上传fastDFS
public String creatQrCode(String path, String name) {
String qrPath = "";
try {
//获取二维码图片字节流
byte[] bytes = getQrCodeByte(path, name);
InputStream inputStream = new ByteArrayInputStream(bytes);
//通过fastDFS保存最终图片
Map qrMap = fastDFSConfig.uploadFilePng(inputStream);
qrPath = qrMap.get("filePath").toString();
if (ObjectUtils.isEmpty(qrPath)) {
throw BizException.warp(ResultCode.VALIDATE_FAILED.build("生成二维码失败"));
}
} catch (Exception e) {
throw BizException.warp(ResultCode.VALIDATE_FAILED.build("生成二维码失败"));
}
return qrPath;
}
获取二维码流
private byte[] getQrCodeByte(String path, String name) {
byte[] bytes = null;
try {
BufferedImage image = createQrCode(path, 300);
BufferedImage bufferedImage = addNote(image, name);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(byteArrayOutputStream);
ImageIO.write(bufferedImage, "jpg", imageOutputStream);
bytes = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
throw BizException.warp(ResultCode.VALIDATE_FAILED.build("生成二维码失败"));
}
return bytes;
}
创建二维码
private BufferedImage createQrCode(String content, int qrCodeSize) {
try {
//设置二维码纠错级别
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QrCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width - 65, height - 65, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
graphics2D.setColor(Color.white);
graphics2D.fillRect(0, 0, width, height);
graphics2D.setColor(Color.BLACK);
//保存图像
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
if (bitMatrix.get(i, j)) {
graphics2D.fillRect(i - 33, j - 33, 2, 2);
}
}
}
return image;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
底部添加文字说明
private BufferedImage addNote(BufferedImage image, String note) {
int a = 0;
String reg1 = "[\u4e00-\u9fa5]";
String reg2 = "[A-Z]";
String reg3 = "[a-z]";
String reg4 = "[0-9]";
char[] charArr = note.toCharArray();
String[] strArr = new String[charArr.length];
for (int i = 0; i < charArr.length; i++) {
strArr[i] = String.valueOf(charArr[i]);
if (strArr[i].matches(reg1)) {
a = a + 19;
} else if (strArr[i].matches(reg4)) {
a = a + 11;
} else if (strArr[i].matches(reg2) || strArr[i].matches(reg3)) {
a = a + 9;
} else {
a = a + 5;
}
}
Image src = image.getScaledInstance(300, 300, Image.SCALE_DEFAULT);
BufferedImage tag;
//最下层黑边
if (a < 300) {
tag = new BufferedImage(300, 322, BufferedImage.TYPE_INT_RGB);
} else {
tag = new BufferedImage(300, 345, BufferedImage.TYPE_INT_RGB);
}
Graphics g1 = tag.getGraphics();
g1.setColor(Color.white);
Graphics2D g2 = tag.createGraphics();//设置文字
Font font = new Font("Default", Font.BOLD, 18);
g2.setFont(font);
g2.setColor(Color.BLACK);
if (a < 300) {
g1.fillRect(0, 300, 300, 22);
g2.drawString(note, (300 - a) / 2 - 4 < 0 ? 0 : (300 - a) / 2 - 4, 300 + font.getSize());
} else {
g1.fillRect(0, 300, 300, 45);
g2.drawString(note, (300 - a) / 2 - 4 < 0 ? 0 : (300 - a) / 2 - 4, 300 + font.getSize());
g2.drawString(note.substring(16, note.length()), (300 - (a - 300)) / 2, 300 + font.getSize() * 2 + 4);
}
g1.drawImage(src, 0, 0, null);
g1.dispose();
g2.dispose();
image = tag;
image.flush();
return image;
}
底部文字中文乱码问题:
文章来源:https://www.toymoban.com/news/detail-402803.html
解决思路:~~~~~~~~~~~~~~~~~~~
1.看一下代码
2.部署服务器的编码问题
(1)查看java是否存在 java -version
(2) which java
进入到bin的目录下
cd /usr/bin
ll
看jre下面是否有fonts文件夹,没有的话,新建一个
cd /etc/alternatives/jre/lib/fonts/fallback/simsun.ttc
将ttf文件导入到linux系统中,如上路径
simsun.ttc (文字包)↓
可在本地电脑C盘 Windows\Fonts中获取
这里是一个ttc文件,需要修改文件后缀,改为ttf,才可以扔到linux中文章来源地址https://www.toymoban.com/news/detail-402803.html
到了这里,关于【二维码的创建、底部添加文字以及文字乱码】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!