package com.xjh.demo.qrcode; 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; import java.io.File; import java.nio.file.Path; import java.util.HashMap; public class CreateQRCode { public static void main(String[] args){ int heigth=300; int width=300; String format="png"; String content="你好!"; HashMap hints=new HashMap(); hints.put(EncodeHintType.CHARACTER_SET,"GBK"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.MARGIN,2); try{ content=new String(content.getBytes("UTF-8"),"ISO-8859-1"); BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,heigth); Path file=new File("D:/img.png").toPath(); MatrixToImageWriter.writeToPath(bitMatrix,format,file); }catch (Exception e){ } } }
java生成二维码QRCode
最新推荐文章于 2024-04-03 16:39:55 发布