字符串转二维码,一维码

//字符串转二维码   (参数给定两个字符串)

public static void strToQRcode(String str, String flowno) throws Exception {

//定义一个宽度

int width = 200;

//定义一个高度

int height = 200;

//定义一个黑颜色

int BLACK = 0xFF000000;

//定义一个白颜色

int WHITE = 0xFFFFFFFF;

//定义一个格式

String format = "png";

//新建hashtable集合  线程安全       

Hashtable hints= new Hashtable();

//使用EncodeHintType.MARGIN设置 (google.zxing包,用于生成二维码) 二维码空白区域大小

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

//Matrix(矩阵)  用于生成二维码

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

//新建图片缓冲

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

//双重循环,生成二维码

for (int x = 0; x < width; x++) {

for (int y = 0; y < height; y++) {

image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);

}

}

//新建图片文件对象

File stream = new File("./", flowno + ".png");

if (!ImageIO.write(image, format, stream))

{ throw new IOException("Could not write an image of format " + format); }

}




//字符串转一维码

    public static void strToBarcode(String orderId) throws Exception {

//        deletePng();

//定义宽度

        int width = 300;

//定义高度

        int height = 50;

//定义黑色

        int BLACK = 0xFF000000;

//定义白色

        int WHITE = 0xFFFFFFFF;

//格式化

        String format = "png";

//新建hashtable集合 线程安全

//        Hashtable hints= new Hashtable();

//EncodeHintType设置二维码

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

//matrix(矩阵)  生成二维码  

        BitMatrix bitMatrix = new MultiFormatWriter().encode(orderId, BarcodeFormat.CODE_128, width, height);

//图片文件缓冲

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

//双重循环,循环打印

        for (int x = 0; x < width; x++) {

            for (int y = 0; y < height; y++) {

        //把条码设置在图片上

                image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);

            }

        }

        //文件对象

        File stream = new File("./", orderId + ".png");

//图片io 写入

        if (!ImageIO.write(image, format, stream)) {

            throw new IOException("Could not write an image1 of format " + format);

        }

    }

}

你可能感兴趣的:(字符串转二维码,一维码)