public class Qrcode4jUtil {
/**
* 将字符串生成二维码
*/
@SuppressWarnings("deprecation")
public static String createQrcode(String text) {
String root = ServletActionContext.getServletContext().getRealPath("/systemfile/picture");
// 从配置文件中读取宽高(二维码宽高相等)
int width1 = Integer.parseInt(MyProperties.getByKey("width"));
int width2 = Integer.parseInt(MyProperties.getByKey("width2"));
String format = "jpg";
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
String newFileName = NewIdUtil.getNewIdUtil().getNewId() + ".jpg";// 给二维码图片命名
String path = root + "/" + newFileName;
String path1 = root + "1/" + newFileName;
String path2 = root + "2/" + newFileName;
// System.out.println(path);
BitMatrix bitMatrix;
BitMatrix bitMatrix1;
try {
bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width2, width2, hints);
bitMatrix1 = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width1, width1, hints);
File outputFile = new File(path);
File outputFile1 = new File(path1);
File outputFile2 = new File(path2);
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);// 大个的二维码
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile2);// 也是大个的二维码
MatrixToImageWriter.writeToFile(bitMatrix1, format, outputFile1);// 经过缩略的二维码
return newFileName;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 将字符串生成二维码
*/
@SuppressWarnings("deprecation")
public static String createQrcode(String text,String root) {
//String root = ServletActionContext.getServletContext().getRealPath("/systemfile/picture");
// 从配置文件中读取宽高(二维码宽高相等)
int width1 = Integer.parseInt(MyProperties.getByKey("width"));
int width2 = Integer.parseInt(MyProperties.getByKey("width2"));
String format = "jpg";
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
String newFileName = NewIdUtil.getNewIdUtil().getNewId() + ".jpg";// 给二维码图片命名
String path = root + "/" + newFileName;
String path1 = root + "1/" + newFileName;
String path2 = root + "2/" + newFileName;
// System.out.println(path);
BitMatrix bitMatrix;
BitMatrix bitMatrix1;
try {
bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width2, width2, hints);
bitMatrix1 = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width1, width1, hints);
File outputFile = new File(path);
File outputFile1 = new File(path1);
File outputFile2 = new File(path2);
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);// 大个的二维码
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile2);// 也是大个的二维码
MatrixToImageWriter.writeToFile(bitMatrix1, format, outputFile1);// 经过缩略的二维码
return newFileName;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public static void pressText(String pressText, String newImg, String targetImg, int fontStyle, Color color, int fontSize, int width, int height) {
//计算文字开始的位置
//x开始的位置:(图片宽度-字体大小*字的个数)/2
int startX = (width-(fontSize*pressText.length()))/2;
//y开始的位置:图片高度-(图片高度-图片宽度)/2
int startY = height-(height-width)/2;
try {
File file = new File(targetImg);
Image src = ImageIO.read(file);
int imageW = src.getWidth(null);
int imageH = src.getHeight(null);
BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, imageW, imageH, null);
g.setColor(color);
g.setFont(new Font(null, fontStyle, fontSize));
g.drawString(pressText, startX, startY);
g.dispose();
FileOutputStream out = new FileOutputStream(newImg);
ImageIO.write(image, "JPEG", out);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
System.out.println("image press success");
} catch (Exception e) {
System.out.println(e);
}
}
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
Map<String, String> datas = new HashMap<String, String>();
datas.put("test", "test");
datas.put("test1", "test1");
createImgWithText(datas);
}
public static void createImgWithText(Map<String, String> datas) throws IOException, WriterException{
// String text = "你好";
Iterator<Map.Entry<String, String>> it = datas.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
int width = 300;
int height = 300;
String format = "jpg";
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(entry.getKey(), BarcodeFormat.QR_CODE, width, height, hints);
File outputFile = new File("D:\\"+entry.getKey()+".jpg");
MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
int font = 18; //字体大小
int fontStyle = 1; //字体风格
//用来存放带有logo+文字的二维码图片
String newImageWithText = "D:/imageWithText"+entry.getKey()+".jpg";
//带有logo的二维码图片
String targetImage = "D:/"+entry.getKey()+".jpg";
//附加在图片上的文字信息
// String content = "文字测试一二三四五六123";
//在二维码下方添加文字(文字居中)
pressText(entry.getValue(), newImageWithText, targetImage, fontStyle, Color.BLUE, font, width, height) ;
}
}
}
结果就可以在D盘查看了