本篇文章将介绍java中如何生成二维码,二维码的展示主要包括两各方面:1.直接生成图片(直接生成图片不需要web程序,maven工程即可)生成二维码的功能主要是依赖Google的Zxing包。
- 添加需要的jar包和pom依赖
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.1</version> </dependency>
去下面给出的地址下载QRCode.jar包,此jar包已经包括 生成与解析 。
官网下载到的jar包是没有解析的
https://files.cnblogs.com/files/bigroc/QRCode.zip 注:需要jdk环境1.8
-
创建两个工具类
package edu.zzu.eams.teacher.info.demo; import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import jp.sourceforge.qrcode.util.Color; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; public class ZXingUtil { public static void encodeImg(String imgPath,String format,String content,int width,int height,String logo) throws Exception { Hashtable<EncodeHintType,Object > hints = new Hashtable<EncodeHintType,Object>() ; //排错率 L<M<Q<H hints.put( EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H) ; //编码 hints.put( EncodeHintType.CHARACTER_SET, "utf-8") ; //外边距:margin hints.put( EncodeHintType.MARGIN, 1) ; /* * content : 需要加密的 文字 * BarcodeFormat.QR_CODE:要解析的类型(二维码) * hints:加密涉及的一些参数:编码、排错率 */ BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height, hints); //内存中的一张图片:此时需要的图片 是二维码-> 需要一个boolean[][] ->BitMatrix 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,(bitMatrix.get(x,y)? Color.BLACK:Color.WHITE) ); } } //画logo image = LogoUtil.logoMatrix(image, logo) ; //string->file File file = new File(imgPath); //生成图片 System.out.println(file); ImageIO.write(image, format, file); } //解密:二维码->文字 public static void decodeImg(File file) throws Exception { if(!file.exists()) return ; //file->内存中的一张图片 BufferedImage imge = ImageIO.read(file) ; MultiFormatReader formatReader = new MultiFormatReader() ; LuminanceSource source = new BufferedImageLuminanceSource(imge); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); //图片 ->result Map map = new HashMap(); map.put(EncodeHintType.CHARACTER_SET, "utf-8") ; Result result = formatReader.decode(binaryBitmap ,map ) ; System.out.println("解析结果:"+ result.toString()); } }
package edu.zzu.eams.teacher.info.demo; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class LogoUtil { //传入logo、二维码 ->带logo的二维码 public static BufferedImage logoMatrix( BufferedImage matrixImage,String logo ) throws IOException { //在二维码上画logo:产生一个 二维码画板 Graphics2D g2 = matrixImage.createGraphics(); System.out.println(logo); //画logo: String->BufferedImage(内存) BufferedImage logoImg = ImageIO.read(new File(logo)); int height = matrixImage.getHeight(); int width = matrixImage.getWidth(); //纯logo图片 g2.drawImage(logoImg, width * 2 / 5, height * 2 / 5, width * 1 / 5, height * 1 / 5, null); //产生一个 画 白色圆角正方形的 画笔 BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); //将画板-画笔 关联 g2.setStroke(stroke); //创建一个正方形 RoundRectangle2D.Float round = new RoundRectangle2D.Float(width * 2 / 5, height * 2 / 5, width * 1 / 5, height * 1 / 5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2.setColor(Color.WHITE); g2.draw(round); //灰色边框 BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2.setStroke(stroke2); //创建一个正方形 RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(width * 2 / 5 + 2, height * 2 / 5 + 2, width * 1 / 5 - 4, height * 1 / 5 - 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); // Color color = new Color(128,128,128) ; g2.setColor(Color.GRAY); g2.draw(round2); g2.dispose(); matrixImage.flush(); return matrixImage; } }
-
package edu.zzu.eams.teacher.info.demo; import java.io.File; public class Test { public static void main(String[] args) throws Exception { String imgPath = "src/二维码12.png"; String content = "https://jw.v.zzu.edu.cn/eams/login.action"; String logo = "src/logo.png"; //加密:文字信息->二维码 ZXingUtil.encodeImg(imgPath, "png",content, 430, 430,logo); //解密:二维码 ->文字信息 ZXingUtil.decodeImg(new File(imgPath)); } }
注 :src路径下面需要方丈logo.png照片 ,生成的二维码图片在src下面
来源:CSDN
作者:WangPanJu
链接:https://blog.csdn.net/qq_37627493/article/details/104651224