java二维码生成

时间:2023-03-09 04:10:44
java二维码生成
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.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; //生成二维码
public class CreateQRCode { public static void main(String[] args) {
int width=300;
int height=300;
String format="jpg";
String content="http://www.zjjansens.com";
//定义二维码参数
HashMap hashMap=new HashMap();
hashMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hashMap.put(EncodeHintType.MARGIN,2);
try {
BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,hashMap);
Path file=new File("d:/img.jpg").toPath();
MatrixToImageWriter.writeToPath(bitMatrix,format, file);;
} catch (Exception e) {
e.printStackTrace();
}
}
}