介紹
主要使用了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中文網!

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允許Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,對象與偏見,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的頂級功能包括:1)面向對象編程,支持多態性,提升代碼的靈活性和可維護性;2)異常處理機制,通過try-catch-finally塊提高代碼的魯棒性;3)垃圾回收,簡化內存管理;4)泛型,增強類型安全性;5)ambda表達式和函數式編程,使代碼更簡潔和表達性強;6)豐富的標準庫,提供優化過的數據結構和算法。

javaisnotirelyplatemententedduetojvmvariationsandnativecodinteinteration,butitlargelyupholdsitsitsworapromise.1)javacompilestobytecoderunbythejvm

thejavavirtualmachine(JVM)IsanabtractComputingmachinecrucialforjavaexecutionasitrunsjavabytecode,使“ writeononce,runanywhere”能力

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Java的五大特色是多態性、Lambda表達式、StreamsAPI、泛型和異常處理。 1.多態性讓不同類的對象可以作為共同基類的對象使用。 2.Lambda表達式使代碼更簡潔,特別適合處理集合和流。 3.StreamsAPI高效處理大數據集,支持聲明式操作。 4.泛型提供類型安全和重用性,編譯時捕獲類型錯誤。 5.異常處理幫助優雅處理錯誤,編寫可靠軟件。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境