二维码的生成与解析

1.导包(core-3.4.0.jar和javase-3.4.0.jar)

2.jsp页面(二维码的生成与解析)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>二维码的生成与解析</title>
  </head>
  <body>
<img alt="" src="/code.action"/>
<br>
<form action="decode.action" method="post" enctype="multipart/form-data">
  <input type="file" name="mf">
  <input type="submit" value="解析">
</form>
  </body>
</html>

3.servlet(二维码的生成)

@WebServlet("/code.action")
public class CodeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //生成图片
//		BufferedImage codeImage=ZXingCodeEncodeUtils.createZXingCodeNormal("smallzhs", 300, 300);
//        ServletOutputStream outputStream = resp.getOutputStream();
//        ImageIO.write(codeImage, "JPEG", outputStream);
//        outputStream.close();

        InputStream logoStream=this.getClass().getClassLoader().getResourceAsStream("logo.jpg");
        BufferedImage codeImage=ZXingCodeEncodeUtils.createZxingCodeUseLogo("smallzhs", 300, 300, logoStream);

        ServletOutputStream outputStream = resp.getOutputStream();
        ImageIO.write(codeImage, "JPEG", outputStream);
        outputStream.close();
    }
}

 4.utils工具类(二维码的生成【多种形式】)

/**
 * 生成二维码
 */
public class ZXingCodeEncodeUtils {
    // 二维码颜色
    private static final int BLACK = 0xFF000000;
    // 二维码背景颜色
    private static final int WHITE = 0xFFFFFFFF;

    // 二维码格式参数
    private static final EnumMap<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(
            EncodeHintType.class);

    static {
        /*
         * 二维码的纠错级别(排错率),4个级别: L (7%)、 M (15%)、 Q (25%)、 H (30%)(最高H)
         * 纠错信息同样存储在二维码中,纠错级别越高,纠错信息占用的空间越多,那么能存储的有用讯息就越少;共有四级; 选择M,扫描速度快。
         */
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 二维码边界空白大小 1,2,3,4 (4为默认,最大)
        hints.put(EncodeHintType.MARGIN, 1);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");// 设置放入的字符的编码
    }

    /**
     * 生成二维码保存到F盘
     */
    public static void createZXingCodeSaveToDisk(String content, int width, int height, String savePath,
                                                 String imageType) {
        try {
            BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            //得到二维码的宽度和高度
            int code_width = encode.getWidth();
            int code_height = encode.getHeight();
            //创建图片
            BufferedImage image=new BufferedImage(code_width, code_height, BufferedImage.TYPE_INT_BGR);

            // 把二维码里面的信息写到图片里面
            for (int i = 0; i < code_width; i++) {
                for (int j = 0; j < code_height; j++) {
                    image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
                }
            }
            // 保存图片到硬盘
            File file = new File(savePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            ImageIO.write(image, imageType, file);
            System.out.println("生成成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成二维码返回图片对象【页面显示】
     */
    public static BufferedImage createZXingCodeNormal(String content, int width, int height) {
        try {
            BitMatrix encode = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            // 得到二维码的宽度
            int code_width = encode.getWidth();
            int code_height = encode.getHeight();
            // 创建图片
            BufferedImage image = new BufferedImage(code_width, code_height, BufferedImage.TYPE_INT_RGB);
            // 把二维码里面的信息写到图片里面
            for (int i = 0; i < code_width; i++) {
                for (int j = 0; j < code_height; j++) {
                    image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE);
                }
            }
            return image;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 生成一张带logo的二维码
     * @param logoStream  logo的流对象
     *
     */
    public static void createZxingCodeUseLogoSaveToDisk(String content, int width, int height, String savePath,
                                                        String imageType,InputStream logoStream) {
        try {
            BufferedImage codeImage=createZxingCodeUseLogo(content, width, height, logoStream);
            // 保存图片到硬盘
            File file = new File(savePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            ImageIO.write(codeImage, imageType, file);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成一张带logo的二维码  返回BuffredeImage【页面显示】
     * @param logoStream  logo的流对象
     *
     */
    public static BufferedImage createZxingCodeUseLogo(String content, int width, int height,InputStream logoStream) {

        try {
            //生成二维码图片
            BufferedImage codeNormal = createZXingCodeNormal(content, width, height);
            if(null!=codeNormal) {
                //判断logoStream是否为空
                if(null!=logoStream) {
                    //拿到可以操作当前图片的画笔
                    Graphics2D graphics = codeNormal.createGraphics();
                    //得到logo图片的对象
                    BufferedImage logoImage = ImageIO.read(logoStream);
                    //得到logo的原始宽高
                    int old_logo_width = logoImage.getWidth();
                    int old_logo_height = logoImage.getHeight();


                    //得到二维码的宽高
                    int code_width=codeNormal.getWidth();
                    int code_height=codeNormal.getHeight();

                    //算出logo在二维码里面能存在的最大值
                    int logo_max_width=code_width/5;
                    int logo_max_height=code_height/5;

                    //计算logo的可用宽高
                    int logo_width=logo_max_width<old_logo_width?logo_max_width:old_logo_width;
                    int logo_height=logo_max_height<old_logo_height?logo_max_height:old_logo_height;

                    //计算logo的开始点的坐标
                    int x=(code_width-logo_width)/2;
                    int y=(code_height-logo_height)/2;

                    /**
                     * logoImage logo图片对象
                     * x 开始画的x轴坐标
                     * y 开始画的y轴的坐
                     * logo_width 要画的x轴的长度
                     * logo_height 要画的y车的长度
                     * arg5  null
                     */
                    graphics.drawImage(logoImage, x, y, logo_width, logo_height, null);

                    graphics.setStroke(new BasicStroke(2));
                    graphics.setColor(Color.WHITE);
                    //画白色边框
                    graphics.drawRoundRect(x, y, logo_width, logo_height, 15, 15);
                    graphics.dispose();//让画上的去的内容生效
                    return codeNormal;
                }
            }else {
                System.out.println("生成失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("生成失败");
        }
        return null;
    }


    public static void main(String[] args) {
        //createZXingCodeSaveToDisk("smallzhs",400,400,"F:/upload/image/xiaozhu.gif","JPEG");
        InputStream logoStream=ZXingCodeEncodeUtils.class.getClassLoader().getResourceAsStream("logo.jpg");
        createZxingCodeUseLogoSaveToDisk("smallzhs", 400, 400, "F:/upload/image/xiaozhu2.gif", "JPEG", logoStream);
    }
}

 二维码解析:

servlet(解析):别忘记下面这个注解!!!!

@WebServlet("/decode.action")
@MultipartConfig
public class DeCodeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part part=req.getPart("mf");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String string = ZXingCodeDecodeUtils.decodeCodeStream(part.getInputStream());
        PrintWriter out = resp.getWriter();
        out.write(string);
        out.flush();
        out.close();
    }
}

 utils

/**
 * 解析二维码
 */
public class ZXingCodeDecodeUtils {
    // 二维码格式参数
    private static final EnumMap<DecodeHintType, Object> decodeHints = new EnumMap<DecodeHintType, Object>(
            DecodeHintType.class);
    static {
        decodeHints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
    }

    /**
     * 解析文件
     *
     * @param path
     */
    public static String decodeCodeFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            // 把文件转成图片对象
            try {
                String content = decodeCodeStream(new FileInputStream(file));
                return content;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

        } else {
            return null;
        }
    }

    /**
     * 解析流
     *
     * @param is
     */
    public static String decodeCodeStream(InputStream is) {
        if (null != is) {
            try {
                BufferedImage image = ImageIO.read(is);
                LuminanceSource source = new BufferedImageLuminanceSource(image);
                Binarizer binarizer = new HybridBinarizer(source);
                BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
                MultiFormatReader reader = new MultiFormatReader();
                Result result = reader.decode(binaryBitmap, decodeHints);
                String content = result.getText();
                return content;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    public static void main(String[] args) throws FileNotFoundException {
        String string = decodeCodeStream(new FileInputStream(new File("F:/upload/image/xiaozhu2.gif")));
        System.out.println(string);
    }
}

ok! 最后运行即可!

posted @ 2020-04-07 17:15  lemmon_water  阅读(382)  评论(0)    收藏  举报