二维码ZXing

package zxing;

import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import jp.sourceforge.qrcode.util.Color;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 需要的jar包
 * 1. zxing-javase-3.1.0.jar
 * 2.zxing-3.1.0.jar
 */
public class ZXingUtil {

    /**
     * 生成二维码
     * @param content 内容
     * @param imgPath 输出图片路径
     * @param logoPath logo路径
     * @param format 二维码类型
     */
    public static void encodeImg(String content,String imgPath,String logoPath,String format,int width,int height) throws IOException, WriterException {
        //设置二维码相关属性
        HashMap<EncodeHintType, Object> hints = new HashMap<>();
        //设置排错率 7% L<M<Q<H 30%
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        //编码
        hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
        //外边距
        hints.put(EncodeHintType.MARGIN,1);
        /*
            BitMatrix 可以将内容 -> boolean[][]
         * content : 需要加密的 文字
         * BarcodeFormat.QR_CODE:要解析的类型(二维码)
         * hints:加密涉及的一些参数:编码、排错率
         */
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);

        //产生内存中的图片
        //BufferedImage.TYPE_INT_RGB :图片产生的颜色(绘图基础颜色)
        BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        for (int i = 0; i < width; i++) {
            for(int j = 0; j < height; j++){
                bufferedImage.setRGB(i,j,(bitMatrix.get(i,j)? Color.BLACK : Color.WHITE));
            }
        }
        //画logo
        bufferedImage = drawLogo(bufferedImage,logoPath);
        //String->file
        File file = new File(imgPath);
        //生成图片
        ImageIO.write(bufferedImage,format,file);
    }

    public static void decodeImg(File file) throws IOException, NotFoundException {
        if(!file.exists()) return;

        //file->内存中的一张图片
        BufferedImage imge = ImageIO.read(file)  ;
        LuminanceSource source = new BufferedImageLuminanceSource(imge);
        HybridBinarizer hybridBinarizer = new HybridBinarizer(source);
        BinaryBitmap binaryBitmap = new BinaryBitmap(hybridBinarizer);
        MultiFormatReader formatReader = new MultiFormatReader() ;
        //图片 ->result
        Map map = new HashMap();
        map.put(EncodeHintType.CHARACTER_SET, "utf-8") ;
        Result result = formatReader.decode(binaryBitmap  ,map ) ;
        System.out.println("解析结果:"+ result.toString());
    }
    private static BufferedImage drawLogo(BufferedImage bufferedImage, String logoPath) throws IOException {
        //创建一个画板
        Graphics2D gs = bufferedImage.createGraphics();
        //读取硬盘中的logo到内存
        BufferedImage logo = ImageIO.read(new File(logoPath));
        int width = bufferedImage.getWidth();
        int height = bufferedImage.getHeight();
        //纯logo
        gs.drawImage(logo,width*2/5,height*2/5,width/5,height/5,null);

        //产生一个 画 白色圆角正方形的 画笔
        //画笔粗细
        BasicStroke basicStroke = new BasicStroke(5,BasicStroke.CAP_ROUND ,BasicStroke.JOIN_ROUND);
        //将画笔画板关联
        gs.setStroke(basicStroke);
        //创建一个正方形
        RoundRectangle2D.Float round = new RoundRectangle2D.Float(width * 2 / 5, height * 2 / 5, width / 5, height / 5, BasicStroke.CAP_ROUND, BasicStroke
                .JOIN_ROUND);
        gs.setColor(java.awt.Color.white);
        gs.draw(round);
        RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(width*2/5+3,height*2/5+3,width/5-6,height/5-6,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
        gs.setColor(java.awt.Color.gray);
        gs.draw(round2);

        //释放空间
        gs.dispose();
        //清理缓冲区
        bufferedImage.flush();
        return bufferedImage;
    }

    public static void main(String[] args) throws IOException, WriterException, NotFoundException {
        String imgPath = "E:\\temp\\二维码2.png";
        String content = "http://www.baidu.com";
        String logoPath = "E:\\temp\\previewFix.jpg";
        ZXingUtil.encodeImg(content,imgPath,logoPath,"png",430,430);
        ZXingUtil.decodeImg(new File(imgPath));

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值