利用Java產生帶有文字的二維碼
介紹
主要使用了goole的zxing包,下面給出了示例代碼,很方便大家的理解和學習,代碼都屬於初步框架,功能有了,需要根據實際使用情況完善優化。
第一步、maven導入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 扫描二维码的内容 * @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(); //设置UTF-8, 防止中文乱码 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置二维码四周白色区域的大小 hints.put(EncodeHintType.MARGIN,1); //设置二维码的容错性 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); } }
第三步、把文字寫入文字中:中寫真第四步、在main方法中測試一下,一個中間帶文字的二維碼就生成了
/** * 给二维码图片加上文字 * @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); //设置画笔的颜色 g.setColor(color); //设置字体 Font font = new Font("宋体", fontStyle, fontSize); FontMetrics metrics = g.getFontMetrics(font); //文字在图片中的坐标 这里设置在中间 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"); }
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
更多利用Java來產生帶有文字的二維碼相關文章請關注PHP中文網!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本啟動Spring...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

Redis緩存方案如何實現產品排行榜列表的需求?在開發過程中,我們常常需要處理排行榜的需求,例如展示一個�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...
