1. 程式人生 > >二維碼生成與讀取

二維碼生成與讀取

一、通過Zxing生成與讀取:

  • 生成二維碼:

       int width=300;
       int height=300;
       String format="png";
       String content="www.link.com";
       
       HashMap hint=new HashMap();
       hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
       hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
       hint.put(EncodeHintType.MARGIN, 2);
       
       BitMatrix bitMatrix=new MultiFormatWriter().
       encode(content, BarcodeFormat.QR_CODE, width, height,hint);  
       Path file=new File("D:/img.png").toPath();
       MatrixToImageWriter.writeToPath(bitMatrix, format, file);
    

注:此處生成掃描是"www.link.com",新增為連結時轉到連結

  • 讀取二維碼:

      MultiFormatReader formatReader=new MultiFormatReader(); 
       File file=new File("D:/img.png");  
       BufferedImage image=ImageIO.read(file); 
       BinaryBitmap binaryBitmap=new BinaryBitmap(
         new HybridBinarizer(new BufferedImageLuminanceSource(image)));
       
       
       HashMap hint=new HashMap();
       hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
       Result result=formatReader.decode(binaryBitmap,hint);
       
       System.out.println(result.toString());
       System.out.println(result.getBarcodeFormat());
       System.out.println(result.getText());
    

    二、QRcode生成二維碼:

    • 生成二維碼:

       Qrcode x=new Qrcode();
       x.setQrcodeErrorCorrect('M');
       x.setQrcodeEncodeMode('B');
          x.setQrcodeVersion(7);
          String qrData="www.imooc.com";
          int width=67+12*(7-1);
          int height=67+12*(7-1);
          int pixoff=2;
      
       BufferedImage bufferedImage=
       new BufferedImage
       (300, 300, BufferedImage.TYPE_INT_RGB);  
        Graphics2D gs=bufferedImage.createGraphics();
       gs.setBackground(Color.WHITE);
       gs.setColor(Color.BLACK);
       gs.clearRect(0, 0, width, height);
         
         byte[] d=qrData.getBytes("gb2312");
         if(d.length>0&&d.length<120){
               boolean[][] s=x.calQrcode(d); 
              for(int i=0;i<s.length;i++){
                  for(int j=0;j<s.length;j++){
                       if(s[j][i]){
                        gs.fillRect(j*3+pixoff, i*3+pixoff, 3, 3);
                      }
                }
            }
      }
      gs.dispose();
      bufferedImage.flush();
      ImageIO.write(bufferedImage, "png", new File("D:/qrcode.png"));
      
    • 讀取二維碼:

         public class ReadQRcode {
         public static void main(String[] args) throws IOException {
         	File file=new File("D:/qrcode.png"); 
         	BufferedImage bufferedImage=ImageIO.read(file); 
         	QRCodeDecoder codeDecoder=new QRCodeDecoder();
        	 String  result=
        	 new String(codeDecoder.decode(
        	 new QRcodeImage(bufferedImage)),"gb2312");
        	 System.out.println(result); 
       	 }
       }
       class QRcodeImage implements QRCodeImage{
       	BufferedImage bufferedImage;
       	 public QRcodeImage(BufferedImage bufferedImage) {
       		  this.bufferedImage=bufferedImage;
       	 }
        @Override
        public int getHeight() {
         return bufferedImage.getHeight();
        }
        @Override
      public int getPixel(int arg0, int arg1) {
       	  return bufferedImage.getRGB(arg0, arg1);
      }
       @Override
       public int getWidth() {
       	return bufferedImage.getWidth();
       }}
      

    三、jquery.qrcode.min.js生成二維碼:

       在jsp頁面引入:
         	<script type="text/javascript" src="js/jquery.min.js"></script>
     		<script type="text/javascript" 
     		src="js/jquery.qrcode.min.js"></script> 
     			  
     		 生成二維碼:<br>
     		 <div id="qrcode"></div> 
     		 <script type="text/javascript">
     		  jQuery('#qrcode').qrcode("www.imooc.com");
     		 </script>