Zxing手機掃碼跳轉

原文鏈接:https://www.jianshu.com/p/fc890265deb4

github

  • 你需要引入zxing的jar包,如果是maven工程.
<dependency>   
     <groupId>com.google.zxing</groupId>   
     <artifactId>core</artifactId>  
     <version>3.2.1</version>
</dependency>
  • 後臺:在你的controller中放入以下方法,訪問/qr即可
 //[測試] 二維碼url
    @RequestMapping("/qr")
    public String testQRCode(Model model) {    
          //這裏隨便給了一個微信支付相關的url
          model.addAttribute("code_url", "weixin://wxpay/bizpayurl?pr=PbcM89o");    
          //返回qr.html
          return "qr";
    }

    //顯示二維碼圖片, 這個地址配在img的src下
    @RequestMapping("/qr-img")
    @ResponseBody
    public void getQRCode(String code_url, HttpServletResponse response) {   
         encodeQrcode(code_url, response);
    }

    //不保存二維碼圖片,以流的形式返回
    private void encodeQrcode(String content, HttpServletResponse response) {    
        if (StringUtils.isEmpty(content))        return;    
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();    
        Map hints = new HashMap();    
        //設置字符集編碼類型  
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 
        //設置二維碼四周的白色邊框 ,默認是4,默認爲4的時候白色邊框實在是太粗了   
        hints.put(EncodeHintType.MARGIN, 0);
        BitMatrix bitMatrix = null;    
        try {        
                bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 190, 190, hints);      
                BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);   
                //輸出二維碼圖片流      
                try {          
                      ImageIO.write(image, "png", response.getOutputStream());     
                     } catch (IOException e) {       
                           // TODO Auto-generated catch block      
                            e.printStackTrace();    
                           }  
                     } catch (WriterException e1) {     
                           // TODO Auto-generated catch block       
                           e1.printStackTrace();   
                     }
  }
  • qr.html
<!DOCTYPE html>
<html lang="en">
<head>   
      <meta charset="UTF-8"/>    
      <title></title>
</head>
<body>
      <!--The thymeleaf syntax is used here,you need to write your own path-->
      <img src="/qr-img?code_url=${code_url}" style="width:300px;height:300px;"/>
</body>
</html>
  • 寫到這裏你會發現好像少了一個MatrixToImageWriter類,這個類是由google提供,直接copy一份即可
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
/** 
   * 二維碼的生成需要藉助MatrixToImageWriter類,該類是由Google提供的,可以將該類直接拷貝到源碼中使用 
   * Created by sam on 16/4/12. 
   */
public class MatrixToImageWriter {    
      private static final int BLACK = 0xFF000000;  
      private static final int WHITE = 0xFFFFFFFF;  

      private MatrixToImageWriter() {   
      }    

      public static BufferedImage toBufferedImage(BitMatrix matrix) {      
            int width = matrix.getWidth();     
            int height = matrix.getHeight();     
            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, matrix.get(x, y) ? BLACK : WHITE);       
                 }  
            }       
             return image;
      }    
  
      public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {   
             BufferedImage image = toBufferedImage(matrix);       
             if (!ImageIO.write(image, format, file)) {    
                   throw new IOException("Could not write an image of format " + format + " to " + file);
            }   
       } 
  
      public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {    
           BufferedImage image = toBufferedImage(matrix);    
           if (!ImageIO.write(image, format, stream)) { 
                 throw new IOException("Could not write an image of format " + format);       
           } 
     }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章