通过Java生成二维码

准备工作

网站1下载zxing包,实际过程中有可能出现错误,是因为少了一个包,可以网站2下载

代码

public class Create {

public static void main(String[]args){

int width =300;//二维码宽

int height =300;//二维码高

String format ="png";//二维码格式

String content ="helloWorld";//二维码内容,可以是中文

//定义二维码参数

HashMap hints =new HashMap();

hints.put(EncodeHintType.CHARACTER_SET,"utf-8");

hints.put(EncodeHintType.MARGIN,2);

hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);

//ErrorCorrectionLevel纠错等级,L、M、Q、H等级一次递增,等级越高存储容量越小

try{

BitMatrix bitMatrix =new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,width,height,hints);

Path file =new File("E:/hello/img.png").toPath();

MatrixToImageWriter.writeToPath(bitMatrix,format,file);

}catch (Exception e){

e.printStackTrace();

}

}

}

结尾

Java小白之路,共勉

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容