利用Zxing生产二维码

ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。

所需架包:
zxing 3.3.0.jar

生成二维码:

public class QCode {

    private book b1=null;
    int width=300;
    int height =300;

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

    String content="二维码生成成功";
public QCode()
{





}
public void does()
{
    // 来定义二维码的参数
    HashMap img=new HashMap();
    img.put(EncodeHintType.CHARACTER_SET,"utf-8");//设置编译的字节集
    img.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);
    img.put(EncodeHintType.MARGIN, 2);//设置边距


    try{
        BitMatrix b=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height,img);//1.是内容 2.是二维码类型  5是二维码

        Path file=new File("C://Users/luo/Desktop/image/1.png").toPath();

        MatrixToImageWriter.writeToPath(b, format, file);
    }catch(Exception e)
    {
        e.printStackTrace();
    }   
}


}

启动类

import org.junit.Test;

import DataExchange.QCode;

public class zxingTest {


    @Test
    public void test()
    {

        QCode q =new QCode();
        q.does();
    }
}

查看结果:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40990818/article/details/81841704