1. 程式人生 > >java 二維碼

java 二維碼

output 圖片 eat utf 寬度 bar java 位置 def

生成二維碼用的谷歌的jar包

<!-- 谷歌二維碼 -->
    <dependency>  
       <groupId>com.google.zxing</groupId>  
       <artifactId>core</artifactId>  
       <version>3.0.0</version>  
       </dependency>  
       <dependency>  
       <groupId>com.google.zxing</
groupId> <artifactId>javase</artifactId> <version>3.0.0</version> </dependency>
/**
     * 生成二維碼 base64格式圖片二維碼
     * @param code  信息鏈接
     * @param logPath log地址
     * @return
     * @throws WriterException 
     * @throws IOException 
     */
    public
static String qrCode(String code, String logPath) throws WriterException, IOException{ MultiFormatWriter writer = new MultiFormatWriter(); Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>(); //設置字符編碼 hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); BitMatrix matrix
= writer.encode(code, BarcodeFormat.QR_CODE, 300, 300, hintMap); BufferedImage bufferedImage = toBufferedImage(matrix); if(logPath != null && !"".equals(logPath)){ /** * 在二維碼中添加log */ URL url = new URL(Const.IMAGE_PATH + logPath); DataInputStream dataInputStream = new DataInputStream(url.openStream()); BufferedImage logo = ImageIO.read(dataInputStream); Graphics2D g = bufferedImage.createGraphics(); //考慮到LOGO照片貼到二維碼中,建議大小不要超過二維碼的1/5; int width = bufferedImage.getWidth() / Const.CODE_SIZE; int height = bufferedImage.getHeight() / Const.CODE_SIZE; //LOGO起始位置,此目的是為LOGO居中顯示 int x = (bufferedImage.getWidth() - width) / 2; int y = (bufferedImage.getHeight() - height) / 2; //繪制圖 g.drawImage(logo, x, y, width, height, null); //給LOGO畫邊框 //構造一個具有指定線條寬度以及 cap 和 join 風格的默認值的實心 BasicStroke g.setStroke(new BasicStroke()); g.setColor(Color.WHITE); g.drawRect(x, y, width, height); } ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, Const.FILE_TYPE.substring(1, Const.FILE_TYPE.length()) , out); //轉二進制 byte[] bytes = out.toByteArray(); return Const.BASE64 + Base64.encode(bytes); }
/**
     * BitMatrix 轉成  BufferedImage
     * @param matrix BitMatrix
     * @return
     */
    private static BufferedImage toBufferedImage(BitMatrix matrix) {
        Integer width = matrix.getWidth();
        Integer 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) ? Const.BLACK : Const.WHITE);
            }
        }
        return image;
    }
package com.yuanxinxinxi.yuanxinbuluo.common;

/**
 * 
 * 文件處理長量
 */
public class Const {
   /**
     * 壓縮圖片的後綴
     */
    public final static String FILE_TYPE = ".PNG";
    /**
     * 壓縮圖片的最低像素
     */
    public final static Integer BASE = 1280;
    /**
     * 白色
     */
    public static final Integer BLACK = 0xFF000000;
    /**
     * 黑色
     */
    public static final Integer WHITE = 0xFFFFFFFF;
    /**
     * base64前綴
     */
    public static final String BASE64 = "data:image/png;base64,";/**
     * 二維碼的大小
     */
    public static final Integer CODE_SIZE = 5;
}

java 二維碼