介绍
主要使用了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); } }
第三步、把文字写入二维码图片中:
/** * 给二维码图片加上文字 * @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"); }
第四步、在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); }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
更多利用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)丰富的标准库,提供优化过的数据结构和算法。

javaisnotirelyPlatemententduetojvmvariationsandnativecodinteintration,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 中文破解版
体积小,语法高亮,不支持代码提示功能

Dreamweaver CS6
视觉化网页开发工具

Atom编辑器mac版下载
最流行的的开源编辑器

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

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。