计科1602&电信1602团日活动java代码 - 追着光梦游

计科1602&电信1602团日活动java代码

imooc 2017-11-04

package com.zing2;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.datamatrix.encoder.ErrorCorrection;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

//生成二维码
public class CreateQRcode {

    public static void main(String[] args) {
        
    int width=300;
    int height=300;
    String format="png";
    double i=100*Math.random();
    String str_j = Double.toString(i);
    String content="优惠券码"+str_j+"        计科1602&电信1602团日活动";
    
    //定义二维码的参数
    HashMap hints= new HashMap();
    hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
    hints.put(EncodeHintType.MARGIN, 2);
    
    try {
        BitMatrix bitMatrix  ; 
        bitMatrix=new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);
        Path file = new File("D:/code/1.png").toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        System.out.println(i);
    
    } catch (Exception e) {
        
        e.printStackTrace();
    }
    }

}

本文由 guopengli 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。
  • 浏览次数: 437
  • 还不快抢沙发

    添加新评论