2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > ZXing生成二维码

ZXing生成二维码

时间:2019-02-21 01:23:16

相关推荐

ZXing生成二维码

pom.xml

<!-- /artifact/com.google.zxing/core --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.2</version> </dependency>​​ <!-- /artifact/com.google.zxing/javase --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.2</version> </dependency>

具体事务代码

package cn.silica.Utils;​import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.client.j2se.MatrixToImageWriter;import com.mon.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;​import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.OutputStream;import java.util.HashMap;import java.util.Map;​import static com.google.zxing.client.j2se.MatrixToImageConfig.BLACK;import static com.google.zxing.client.j2se.MatrixToImageConfig.WHITE;​/** * 二维码生成及解析工具 */public class QRCodeUtil {/** * 生成无中间log的二维码 * @param contexts 二位中内容 * @param qrWidth 二维码宽度 * @param qrHeight 二维码长度 * @param outputStream 输出流 */ public static void createNoLogQRCode(String contexts, int qrWidth, int qrHeight ,OutputStream outputStream){Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType, Object>(); //设置二维码校错级别 LMQH 7%,15%,25%,30%表示二维码被图形被污染时可自动复原的比例? //同时纠错级别越高,纠错信息占用的空间就越多,能存储的有用信息就越少 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); //设置编码格式 hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); //设置二维码边界大小1,2,3,4 默认为4,最大 hints.put(EncodeHintType.MARGIN,1); try {//MultiFormatWriter 二维码格式化输出//MultiFormatWriterBitMatrix bitMatrix = (new MultiFormatWriter()).encode(contexts, BarcodeFormat.QR_CODE,qrWidth,qrHeight,hints);//二维码转换为图像MatrixToImageWriter.writeToStream(bitMatrix,"png",outputStream);} catch (Exception e) {e.printStackTrace();} }​ /** * 生成带logo的二维码 * @param contexts 二维码中的内容 * @param qrWidth 二维码的宽度 * @param qrHeight 二维码的高度 * @param outputStream 二维码输出地 * @param imgWidth 二维码中logo的宽度 * @param imgHeight 二维码中logo的高度 * @param imgPath 二维码中logo的原地址 * @throws IOException */ public static void createLogQRCode(String contexts, int qrWidth, int qrHeight ,OutputStream outputStream,int imgWidth,int imgHeight,String imgPath) throws IOException {Map<EncodeHintType,Object> hints = new HashMap<EncodeHintType, Object>(); //设置二维码校错级别 LMQH 7%,15%,25%,30%表示二维码被图形被污染时可自动复原的比例? //同时纠错级别越高,纠错信息占用的空间就越多,能存储的有用信息就越少 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); //设置编码格式 hints.put(EncodeHintType.CHARACTER_SET,"utf-8"); //设置二维码边界大小1,2,3,4 默认为4,最大 hints.put(EncodeHintType.MARGIN,1); try {//MultiFormatWriter 二维码格式化输出BitMatrix bitMatrix = (new MultiFormatWriter()).encode(contexts, BarcodeFormat.QR_CODE,qrWidth,qrHeight,hints);//获取二维码高度qrWidth = bitMatrix.getWidth();qrHeight = bitMatrix.getHeight();​//创建缓冲流BufferedImage bufferedImage = new BufferedImage(qrWidth, qrHeight, BufferedImage.TYPE_INT_RGB);​//将二维码放入缓冲流for (int i = 0; i < qrWidth; i++) {for (int j = 0; j < qrHeight; j++) {// 循环将二维码内容写入图片bufferedImage.setRGB(i, j, bitMatrix.get(i, j) ? BLACK : WHITE); } }​//写入logo图像File logoFile = new File(imgPath);Graphics2D gs2 = bufferedImage.createGraphics();//该对象可以绘制BufferedImage对象BufferedImage logoImg = ImageIO.read(logoFile);//将图片写入到BufferedImage对象中//如果logo长宽超过二维码长宽的20%则设定其长宽为二维码的20%,如果小于20%则以logo实际长宽为准int logoWidth = logoImg.getWidth()>bufferedImage.getWidth()*2/10 ? bufferedImage.getWidth()*2/10 : logoImg.getWidth();int logoHeight = logoImg.getHeight()>bufferedImage.getHeight()*2/10 ? bufferedImage.getHeight()*2/10 : logoImg.getHeight();​//x,y为logo相对于二维码图片的位置int x = (qrWidth - logoWidth) / 2;int y = (qrHeight - logoHeight) / 2;​//开始绘制logo图片gs2.drawImage(logoImg,x,y,logoWidth,logoHeight,null);//生成一个可设置圆角的矩形gs2.drawRoundRect(x,y,logoWidth,logoHeight,0,0);//边框宽度gs2.setStroke(new BasicStroke(3));gs2.setColor(Color.green);//设置背景颜色//生成特定矩形的轮廓gs2.drawRect(x,y,logoWidth,logoHeight);gs2.dispose();//处理图像内容并释放资源,此后gs2将不能再被调用logoImg.flush();bufferedImage.flush();//将二维码返回到页面ImageIO.write(bufferedImage,"png",outputStream);​} catch (Exception e) {e.printStackTrace();}​ }}​

参考:

/rongku/article/details/51872156

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。