利用Java生成帶有文字的二維碼
介紹
主要使用了goole的zxing包,下面給出了示例代碼,很方便大家的理解和學(xué)習(xí),代碼都屬于初步框架,功能有了,需要根據(jù)實際使用情況完善優(yōu)化。
第一步、maven導(dǎo)入zxing
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.2.1</version> </dependency>
第二步、開始生成二維碼:
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
/**
* 把生成的二維碼存入到圖片中
* @param matrix zxing包下的二維碼類
* @return
*/
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
* 生成二維碼并寫入文件
* @param content 掃描二維碼的內(nèi)容
* @param format 圖片格式 jpg
* @param file 文件
* @throws Exception
*/
public static void writeToFile(String content, String format, File file)
throws Exception {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
@SuppressWarnings("rawtypes")
Map hints = new HashMap();
//設(shè)置UTF-8, 防止中文亂碼
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//設(shè)置二維碼四周白色區(qū)域的大小
hints.put(EncodeHintType.MARGIN,1);
//設(shè)置二維碼的容錯性
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//畫二維碼
BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
BufferedImage image = toBufferedImage(bitMatrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
第三步、把文字寫入二維碼圖片中:
/**
* 給二維碼圖片加上文字
* @param pressText 文字
* @param qrFile 二維碼文件
* @param fontStyle
* @param color
* @param fontSize
*/
public static void pressText(String pressText, File qrFile, int fontStyle, Color color, int fontSize) throws Exception {
pressText = new String(pressText.getBytes(), "utf-8");
Image src = ImageIO.read(qrFile);
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);
//設(shè)置畫筆的顏色
g.setColor(color);
//設(shè)置字體
Font font = new Font("宋體", fontStyle, fontSize);
FontMetrics metrics = g.getFontMetrics(font);
//文字在圖片中的坐標 這里設(shè)置在中間
int startX = (WIDTH - metrics.stringWidth(pressText)) / 2;
int startY = HEIGHT/2;
g.setFont(font);
g.drawString(pressText, startX, startY);
g.dispose();
FileOutputStream out = new FileOutputStream(qrFile);
ImageIO.write(image, "JPEG", out);
out.close();
System.out.println("image press success");
}
第四步、在main方法中測試一下,一個中間帶文字的二維碼就生成了
public static void main(String[] args) throws Exception {
File qrcFile = new File("/Users/deweixu/","google.jpg");
writeToFile("www.google.com.hk", "jpg", qrcFile);
pressText("谷歌", qrcFile, 5, Color.RED, 32);
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
Java 異常的棧軌跡(Stack Trace)詳解及實例代碼
這篇文章主要介紹了Java 異常的棧軌跡(Stack Trace)詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
java生成字母數(shù)字組合的隨機數(shù)示例 java生成隨機數(shù)
這篇文章主要介紹了java生成字母數(shù)字組合的隨機數(shù)的示例,大家參考使用吧2014-01-01
MyBatis動態(tài)SQL中的trim標簽的使用方法
這篇文章主要介紹了MyBatis動態(tài)SQL中的trim標簽的使用方法,需要的朋友可以參考下2017-05-05
Java事務(wù)管理學(xué)習(xí)之Hibernate詳解
hibernate是jdbc輕量級的封裝,本身不具備事務(wù)管理的能力,在事物管理層面,一般是委托于底層的jdbc和jta來完成調(diào)度的。下面這篇文章主要給大家介紹了Java事務(wù)管理學(xué)習(xí)之Hibernate的相關(guān)資料,需要的朋友可以參考下。2017-03-03
Maven中exec插件執(zhí)行Java程序的實現(xiàn)
在Maven項目中,可以使用Maven的插件來執(zhí)行Java程序,本文主要介紹了Maven中exec插件執(zhí)行Java程序的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-12-12

