前言:
目前所分享的技术栈为Javaweb之后学运用到的
有喜欢我分享的一些demo可以多多交流
生成二维码前提:
-
1.需要引入谷歌所推荐使用的jar包
-
2.此jar包名称叫做zxing,目前我还没找到能所下载的jar包
-
3.我这里有自己制作好的jar包可以云盘下载
-
链接:https://pan.baidu.com/s/1GoM-DuUbL4_gU0jaDgx3xw
-
提取码:9dgg
话不多说直接上代码:
直接复制即可,制作不易,来个三连
package com.wskj.sjz.demo;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class Codel {/*** 参数是* context 文本内容,url路径可以直接解析成网页* path 你要下载的文件地址* @param context* @param path* @return*/@SuppressWarnings("unchecked")public boolean codel(String context,String path){//设置二维码宽int width = 300;//设置二维码高int height = 300;//设置二维码的后缀名称String format = "png";//设置map集合要往二维码内添加的参数@SuppressWarnings("rawtypes")Map map = new HashMap();//设置二维码的级别map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);//设置二维码中文本的编码格式map.put(EncodeHintType.CHARACTER_SET, "utf-8");//设置二维码的外边框map.put(EncodeHintType.MARGIN, 10);try {//创建生成二维码对象,调用方法将所需要的参数放入BitMatrix bm = new MultiFormatWriter().encode(context, BarcodeFormat.QR_CODE, width, height,map);//创建path对象将物理地址放到file文件内,然后生成path对象Path paths = new File(path).toPath();//使用writeToPath方法调用下载,里面的参数是下载的对象照片,下载的后缀名称,下载的物理路径地址MatrixToImageWriter.writeToPath(bm, format, paths);} catch (WriterException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} return true;}public static void main(String[] args) {Codel code = new Codel();//第一个参数是你所需要的文本,第二个参数是你要保存二维码所需要的路径地址code.codel("北梦创造:csdn","C:\\Users\\H\\Desktop\\csdn.png");}}
运行之后二维码图片,永久保存,WeChat,QQ直接扫码就行
项目结构截图:
-
项目结构所用到的是JavaWeb项目结构
-
jar包需要在此模块中引入
-
可以直接运行在jvm虚拟机上,不需要Tomcat服务器
制作不易记得三连,下期再见,下期出如何使用Java代码发送邮件