Java生成二维码条形码,jquery.qrcode生成二维码

一、导入jar包

   

            com.google.zxing
            core
            3.3.3
 

            com.google.zxing
            javase
            3.3.3

二、编写工具类

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class CodeImageUtil {

    //生成二维码,content:生成内容,生成的二维码存储路径
    public static String createQrCode(String content,String filePath) {
        int width = 300;//可放在参数列表中,一般尺寸都是固定的
        int height = 300;//可放在参数列表中,一般尺寸都是固定的
        String format = "png";
        //定义二维码的参数
        HashMap map = new HashMap();
        //设置编码
        map.put(EncodeHintType.CHARACTER_SET, "utf-8");
        //设置纠错等级,M中等级别
        map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        map.put(EncodeHintType.MARGIN, 2);//外边距

        try {
            //int number = (int)(Math.random()*100)+1;
            String uuid = UUID.randomUUID().toString();
            //生成二维码
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
            File temp = new File(filePath + uuid + ".png");
            Path file = temp.toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
            System.out.println(temp.getPath());
            return temp.getPath();
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //读取二维码,返回二维码内容,filePath:二维码所在位置
    public static String readQrCode(String filePath){
        try {
            MultiFormatReader multiFormatReader = new MultiFormatReader();
            File file = new File(filePath);
            BufferedImage image = ImageIO.read(file);
            //定义二维码参数
            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET,"utf-8");

            //获取读取二维码结果
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            Result result = multiFormatReader.decode(binaryBitmap, hints);

            System.out.println("读取二维码: " + result.toString());
            System.out.println("二维码格式: " + result.getBarcodeFormat());
            System.out.println("二维码内容: " + result.getText());
            return result.getText();
        } catch (NotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //生成条形码  contents:13位数
    public static String createBrCode(String contents,  String imgPath) {

        int width = 300;//可放在参数列表中,一般尺寸都是固定的
        int height = 100;//可放在参数列表中,一般尺寸都是固定的
        int codeWidth = 3 + // start guard
                (7 * 6) + // left bars
                5 + // middle guard
                (7 * 6) + // right bars
                3; // end guard
        codeWidth = Math.max(codeWidth, width);
        String uuid = UUID.randomUUID().toString().replaceAll("-","").substring(0,12);
        File temp = new File(imgPath + uuid + ".png");
        try {
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                    BarcodeFormat.EAN_13, codeWidth, height, null);

            MatrixToImageWriter.writeToStream(bitMatrix, "png",
                    new FileOutputStream(temp));
            return temp.getPath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //解析条形码
    public static String readBrcode(String imgPath) {
        BufferedImage image = null;
        Result result = null;
        try {
            image = ImageIO.read(new File(imgPath));
            if (image == null) {
                System.out.println("the decode image may be not exit.");
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

            result = new MultiFormatReader().decode(bitmap, null);
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

三、测试

    public class CodeTest {
    public static void main(String[] args) {

        //生成二维码,返回二维码存储路径
        String qrpath = QrCodeUtil.createQrCode("https://www.baidu.com","D:/QrCode/");
        //解析二维码
        System.out.println("二维码内容:"+QrCodeUtil.readQrCode(qrpath));
        //生成条形码,返回条形码存储路径
        String brpath =  QrCodeUtil.createBrCode("6925788300026","D:/QrCode/");
       //解析条形码
        System.out.println("条形码内容"+QrCodeUtil.readBrcode(brpath));
    }
}

放一些条形码供测试

Java生成二维码条形码,jquery.qrcode生成二维码_第1张图片

 

四、测试结果

Java生成二维码条形码,jquery.qrcode生成二维码_第2张图片

Java生成二维码条形码,jquery.qrcode生成二维码_第3张图片

生成二维码更简单的方案,利用jquery.qrcode.min.js库生成更简单

Java生成二维码条形码,jquery.qrcode生成二维码_第4张图片

做了个小Demo放在百度云上,有需要可下载

链接:https://pan.baidu.com/s/1NpamAzhAeKo57tjJ-QUi7w

提取码:omf2

你可能感兴趣的:(Java生成二维码条形码,jquery.qrcode生成二维码)