zoukankan      html  css  js  c++  java
  • 【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包。

  • 相关阅读:
    4、2 核心组件
    promise
    Content-Type
    $routeProvider
    广告
    $apply() $digest()
    异常
    switch
    autoprefixer
    $resource
  • 原文地址:https://www.cnblogs.com/lllini/p/11955253.html
Copyright © 2011-2022 走看看