二維碼生成

package com.times.util; 
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix; 
    
 import javax.p_w_picpathio.ImageIO; 
import javax.servlet.http.HttpServletRequest;

 import java.io.File; 
import java.io.FileInputStream;
import java.io.InputStream;
 import java.io.OutputStream; 
 import java.io.IOException; 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.Random;
import java.awt.p_w_picpath.BufferedImage; 
    
 /**
  * 生成二維碼  
  * 
  *
  */
 public final class MatrixToImageWriter { 
    
   static HttpServletRequest rq = SessionUtil.getRequest();
   public static String rq_url = "https://"+ rq.getRemoteAddr()+":"+rq.getRemotePort()+"/";
   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 p_w_picpath = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
     for (int x = 0; x < width; x++) { 
       for (int y = 0; y < height; y++) { 
         p_w_picpath.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); 
       } 
     } 
     return p_w_picpath; 
   } 
    
      
   public static void writeToFile(BitMatrix matrix, String format, File file) 
       throws IOException { 
     BufferedImage p_w_picpath = toBufferedImage(matrix); 
     if (!ImageIO.write(p_w_picpath, format, file)) { 
       throw new IOException("Could not write an p_w_picpath of format " + format + " to " + file); 
     } 
   } 
    
      
   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) 
       throws IOException { 
     BufferedImage p_w_picpath = toBufferedImage(matrix); 
     if (!ImageIO.write(p_w_picpath, format, stream)) { 
       throw new IOException("Could not write an p_w_picpath of format " + format); 
     } 
   }
   
   /**
    * 獲取二維碼名稱生成二維碼
    * @return
    */
   public static String[] getQR_CODE(String f_appUrl,int width,int height) throws Exception{
       //圖片名稱
       String p_w_picpath_name = getRandomChar(10);
       //二維碼的圖片格式 
       String format = "jpg"; 
       //二維碼路徑
       String newImageName = SessionUtil.getSession().getServletContext().getRealPath("file")+"/"+p_w_picpath_name+".jpg";
       
       Hashtable hints = new Hashtable();
       //內容所使用編碼 
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 
       BitMatrix bitMatrix = new MultiFormatWriter().encode(f_appUrl, 
               BarcodeFormat.QR_CODE, width, height, hints);
       //生成二維碼 
       File outputFile = new File(newImageName);
       MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
       //上傳到圖片服務器
       File imgFile = new File(newImageName+".jpg");
       InputStream in = new FileInputStream(imgFile);
       String[] resString = TimesHttpClient.uploadImage2(in,imgFile.getName());
       imgFile.delete();
       return resString;
   }
   
   /**
    * 根據內容生成二維碼BufferedImage
    * @author 向龍飛
    * @date 2015-10-10
    * @param f_appUrl
    * @param width
    * @param height
    * @return
    * @throws WriterException
    */
   public static BufferedImage getQr_imgbuffer(String f_appUrl,int width,int height) throws WriterException{
       
       Hashtable hints = new Hashtable();
       //內容所使用編碼 
       hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
       BitMatrix bitMatrix = new MultiFormatWriter().encode(f_appUrl, 
               BarcodeFormat.QR_CODE, width, height, hints);
       //獲取BufferedImage
       BufferedImage p_w_picpath = toBufferedImage(bitMatrix);
       
       return p_w_picpath;
   }
   
   /**
     * 獲得0-9,a-z,A-Z範圍的隨機數 + 當前時間
     * 
     * 
     * @date 2014-12-04
     * @param length
     *            隨機數長度
     * @return String
     */
    public static String getRandomChar(int length) {
        char[] chr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
                'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        Random random = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            buffer.append(chr[random.nextInt(62)]);
        }
        // 獲取當前時間
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH-mm-ss");
        Date date = new Date();
        String low_date = sdf.format(date);
        buffer.append(low_date);
        return buffer.toString();
    }

    public String getRq_url() {
        return rq_url;
    }
    
    public void setRq_url(String rq_url) {
        this.rq_url = rq_url;
    }
   
    
 }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章