|
|
@ -1,6 +1,7 @@ |
|
|
|
package com.hfkj.common; |
|
|
|
package com.hfkj.common; |
|
|
|
|
|
|
|
|
|
|
|
import com.google.zxing.BarcodeFormat; |
|
|
|
import com.google.zxing.BarcodeFormat; |
|
|
|
|
|
|
|
import com.google.zxing.EncodeHintType; |
|
|
|
import com.google.zxing.WriterException; |
|
|
|
import com.google.zxing.WriterException; |
|
|
|
import com.google.zxing.client.j2se.MatrixToImageWriter; |
|
|
|
import com.google.zxing.client.j2se.MatrixToImageWriter; |
|
|
|
import com.google.zxing.common.BitMatrix; |
|
|
|
import com.google.zxing.common.BitMatrix; |
|
|
@ -12,6 +13,8 @@ import java.awt.*; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @Auther: 胡锐 |
|
|
|
* @Auther: 胡锐 |
|
|
@ -29,8 +32,9 @@ public class QRCodeGenerator { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException { |
|
|
|
public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException { |
|
|
|
QRCodeWriter qrCodeWriter = new QRCodeWriter(); |
|
|
|
QRCodeWriter qrCodeWriter = new QRCodeWriter(); |
|
|
|
|
|
|
|
Map<EncodeHintType,Object> hints = new HashMap<>(); |
|
|
|
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); |
|
|
|
hints.put(EncodeHintType.MARGIN, 0); |
|
|
|
|
|
|
|
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints); |
|
|
|
|
|
|
|
|
|
|
|
File file = new File(filePath); |
|
|
|
File file = new File(filePath); |
|
|
|
if(!file.exists()){ |
|
|
|
if(!file.exists()){ |
|
|
@ -56,6 +60,48 @@ public class QRCodeGenerator { |
|
|
|
ImageIO.write(read , "png" , new File(CommonSysConst.getSysConfig().getFilesystem() + "/" + path)); |
|
|
|
ImageIO.write(read , "png" , new File(CommonSysConst.getSysConfig().getFilesystem() + "/" + path)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String overlapImage(String path ,String number) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
// 创建BufferedImage对象
|
|
|
|
|
|
|
|
int width = 100; |
|
|
|
|
|
|
|
int height = 45; |
|
|
|
|
|
|
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取Graphics2D对象
|
|
|
|
|
|
|
|
Graphics2D g2d = image.createGraphics(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置背景颜色并填充
|
|
|
|
|
|
|
|
g2d.setComposite(AlphaComposite.Clear); |
|
|
|
|
|
|
|
g2d.fillRect(0, 0, width, height); |
|
|
|
|
|
|
|
g2d.setComposite(AlphaComposite.SrcOver); |
|
|
|
|
|
|
|
// 设置背景颜色并填充
|
|
|
|
|
|
|
|
//g2d.setColor(Color.BLACK);
|
|
|
|
|
|
|
|
//g2d.fillRect(0, 0, width, height);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置字体和颜色
|
|
|
|
|
|
|
|
Font font = new Font("Arial", Font.BOLD, 30); |
|
|
|
|
|
|
|
g2d.setFont(font); |
|
|
|
|
|
|
|
g2d.setColor(Color.BLACK); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 绘制数字到图片上
|
|
|
|
|
|
|
|
int x = (width - g2d.getFontMetrics().stringWidth(number)) / 2; |
|
|
|
|
|
|
|
int y = (height + g2d.getFontMetrics().getHeight()) / 2; |
|
|
|
|
|
|
|
g2d.drawString(number, x, y); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 释放Graphics2D对象的资源
|
|
|
|
|
|
|
|
g2d.dispose(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 将BufferedImage保存为图片文件
|
|
|
|
|
|
|
|
File outputFile = new File(path + number + ".png"); |
|
|
|
|
|
|
|
ImageIO.write(image, "png", outputFile); |
|
|
|
|
|
|
|
return outputFile.getName(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
public static void main(String[] args) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
generateQRCodeImage("This is my first QR Code", 350, 350, "D:\\/ss/qr1.png"); |
|
|
|
generateQRCodeImage("This is my first QR Code", 350, 350, "D:\\/ss/qr1.png"); |
|
|
|