google-zxing

使用到两个jar包: zxing-core , zxing-javase

maven下载地址:https://mvnrepository.com/

刚接触后续更新。

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 javax.imageio.ImageReader;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

class Scratch {
    public static void main(String [] args)throws Exception {
        readQR();
    }

    public static void readQR()throws Exception{
        String s2 = "D:\\资料\\199_e7412ebeb7b4ec020824975d6a99e6ea.png";
        File file = new File(s2);
        BufferedImage bufferedImage = ImageIO.read(file);
        HybridBinarizer hybridBinarizer = new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage));
        BinaryBitmap binaryBitmap = new BinaryBitmap(hybridBinarizer);
        MultiFormatReader multiFormatReader = new MultiFormatReader();

        HashMap hashMap = new HashMap();
        hashMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
        hashMap.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        hashMap.put(DecodeHintType.CHARACTER_SET,"utf-8");

        Result result = multiFormatReader.decode(binaryBitmap,hashMap);
        System.out.println("解析结果:"+result.toString());
}

    public void createQR(){
        // 定义二维码的参数
        int width = 300; // 图片宽度
        int height = 300; // 图片高度
        String format = "jpg"; // 图片格式  如果是png类型,logo图变成黑白的,
        String content = "user_id:100001,";// 二维码内容

        // 1.定义HashMap hints
        HashMap hints = new HashMap();
        // 2.hints调用put函数设置字符集、间距以及纠错度为M
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//纠错等级【L,M,Q,H】
        hints.put(EncodeHintType.MARGIN, 2);
        // 生成二维码
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            // 3.最后用MultiformatWriter函数类调用echoed函数并返回一个值 然后写入文件
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            // 这里路径后面的img.png不可省略,前面是自己选取生成的图片地址
            Path file = new File("D:/资料/QR.png").toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, format, file);
            //*************添加logo*****************
            //读取二维码图片
            BufferedImage bufferedImage = ImageIO.read(new File(file.toString()));
            //获取画笔
            Graphics2D graphics = bufferedImage.createGraphics();
            //读取logo图片
            BufferedImage logo = ImageIO.read(new File("C:/Users/Jreeylke/Pictures/Saved Pictures/月.jpg"));
            //设置二维码大小,太大了会覆盖二维码,此处为20%
            int logoWidth = logo.getWidth() > bufferedImage.getWidth()*2 /10 ? (bufferedImage.getWidth()*2 /10) : logo.getWidth();
            int logoHeight = logo.getHeight() > bufferedImage.getHeight()*2 /10 ? (bufferedImage.getHeight()*2 /10) : logo.getHeight();
            //设置logo图片放置的位置,中心
            int x = (bufferedImage.getWidth() - logoWidth) / 2;
            int y = (bufferedImage.getHeight() - logoHeight) / 2;
            //开始合并并绘制图片
            graphics.drawImage(logo,x,y,logoWidth,logoHeight,null);
            graphics.drawRoundRect(x,y,logoWidth,logoHeight,15,15);
            //logob边框大小
            graphics.setStroke(new BasicStroke(2));
            //logo边框颜色
            graphics.setColor(Color.WHITE);
            graphics.drawRect(x,y,logoWidth,logoHeight);
            graphics.dispose();
            logo.flush();
            bufferedImage.flush();
            ImageIO.write(bufferedImage, format ,new File("D:/资料/QR2.png"));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

posted @   LoveDonkey  阅读(307)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示