生成二维码图片,并以流传输到前端页面显示

特别声明---该文章只为备忘

jar包下载链接:

http://maven.aliyun.com/nexus/#welcome

本项目所需要的jar包:

zxing-javase.jar   QRUtil依赖包---二维码图片

nutz-1.r.65.jar    nutz框架的依赖


生成二维码图片的Util

public  class QRUtil {
                public static String image_url ="";


            public static String testEncoder(String str) {
                    String nstr="";
                    String text = str;   
                    System.out.println(text);
                    int width = 200;   
                    int height = 200;   
                    String format = "jpg";  


                    Hashtable hints = new Hashtable();  
                    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
                    BitMatrix bitMatrix = null;
                    try {
                        
                        bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
                    } catch (WriterException e1) { 
                        e1.printStackTrace();
                    }    
                    String imgid = UUID.randomUUID().toString();
                    
                    //将图片动态存入项目目录
                    String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
                    int num=t.indexOf(".metadata");
                   String path=t.substring(1,num).replace('/', '\\')+"imageTest\\WebContent\\image\\";
                    File outputFile = new File(path+imgid+".jpg");  
                    image_url =outputFile.getPath();
                    try {
                        MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
                    } catch (IOException e) { 
                        e.printStackTrace();
                    }
                                return nstr;
        
            }


        }

Nutz框架代码

@At("/span")
@Ok("jsp:show")
    public void login(@Param("username")String name, @Param("city")String city, HttpSession session,HttpServletRequest req,HttpServletResponse response) throws Exception {
                String Userlogin = "http://ttts.besttoptoday.com/lc/login" + "?username="+ name+"&city=" +city;
                System.out.println(Userlogin);
                QRUtil.testEncoder(Userlogin);
    }
        
        @At("/imageShow")
          public void imageGet(HttpSession session,HttpServletRequest req,HttpServletResponse response) throws Exception{
              
                    String image_url =  QRUtil.image_url;
                    System.out.println(image_url);
                        
                    File filePic = new File(image_url);  
                if(filePic.exists()){  
                    FileInputStream is = new FileInputStream(filePic);  
                    int i = is.available(); // 得到文件大小    
                    byte data[] = new byte[i];    
                    is.read(data); // 读数据    
                    is.close();    
                    response.setContentType("image/*"); // 设置返回的文件类型    
                    response.setHeader("Pragma", "no-cache");
                    response.setHeader("Cache-Control", "no-cache");
                    response.setDateHeader("Expires", 0);
                    OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象    
                    toClient.write(data); // 输出数据    
                    toClient.close();    
                }
      }
      

页面则在img标签中src直接访问项目请求

你可能感兴趣的:(java相关备忘录)