• 【JAVA】纯JSP文件二维码生成代码分享


    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    
    <%@ page import="java.io.FileInputStream" %>
    <%@ page import="java.io.ByteArrayOutputStream" %>
    <%@ page import="java.io.IOException" %>
    <%@ page import="java.io.InputStream" %>
    <%@ page import="java.awt.image.BufferedImage" %>
    <%@ page import="javax.imageio.ImageIO" %>
    <%@ page import="java.io.File" %>
    <%@ page import="java.util.Hashtable" %>
    
    <%@ page import="com.google.zxing.BarcodeFormat" %>
    <%@ page import="com.google.zxing.EncodeHintType" %>
    <%@ page import="com.google.zxing.MultiFormatWriter" %>
    <%@ page import="com.google.zxing.WriterException" %>
    <%@ page import="com.google.zxing.common.BitMatrix" %>
    <%
    int BLACK = 0xFF000000;  
    
    int WHITE = 0xFFFFFFFF;  
    String text = "http://www.haiqiancun.com/application/index.jsp"; 
    String exts = "/file/zxing"; 
    File f = new File(getServletContext().getRealPath("/")+exts);
            int width = 300;  
            int height = 300;  
            //二维码的图片格式  
            String format = "gif";  
            Hashtable hints = new Hashtable();  
            //内容所使用编码  
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");  
            BitMatrix bitMatrix = new MultiFormatWriter().encode(text,BarcodeFormat.QR_CODE, width, height, hints);  
            //生成二维码  
            File outputFile = new File(f+"/core.gif");  
    
            if(!f.exists()){
    
    f.mkdir();
    
    }
    
         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) ? BLACK : WHITE);  
           }  
         }  
         BufferedImage bimage =  image;
         if (!ImageIO.write(image, format, outputFile)) {  
           throw new IOException("Could not write an image of format " + format + " to " + outputFile);  
         } 
         out.println("<div style='text-align:center;'><img src='"+request.getContextPath()+exts+"/core.gif' /><br/>"+text+"<br/>文件生成成功:"+f+"</div>");
    %>

    重点在于要引用谷歌开发的zxing包。

  • 相关阅读:
    LoliProfile Native内存分析工具
    谈谈法线贴图
    逐顶点光照与逐像素光照
    标准光照模型
    UE4之TaskGraph系统
    Centos 安装.NET CORE 3.1
    ImportError: No module named 通用解决方法
    [公告] 本博客停止更新,新文献将发布于掘金
    jdk8的常用的功能
    etcd 与 redis使用场景
  • 原文地址:https://www.cnblogs.com/lllini/p/11955253.html
Copyright © 2020-2023  润新知