立即注册 登录
About云-梭伦科技 返回首页

luojiandong的个人空间 https://www.aboutyun.com/?57249 [收藏] [复制] [分享] [RSS]

日志

生成带有logo二维码

已有 1096 次阅读2018-8-14 10:25 |个人分类:java 二维码|系统分类:其它

1.使用google的zxing来生成二维码.pom依赖
    <dependency>  
                <groupId>com.google.zxing</groupId>  
                <artifactId>javase</artifactId>  
                <version>3.3.0</version>  
    </dependency>

2.创建二维码配置信息类。(ZXingConfig)
import java.util.Map;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;

/**
 * 二维码配置信息
 * 
 * @author X-rapido
 * 
 */
public class ZXingConfig {
    private boolean logoFlg = false;    // 是否添加Log图片
    private String content; // 二维码编码内容
    private BarcodeFormat barcodeformat = BarcodeFormat.QR_CODE;    //编码类型
    private int width = 200;    // 生成图片宽度
    private int height = 200;   //  生成图片高度
    private Map<EncodeHintType, ?> hints;   // 设置参数
    private String logoPath;    // Logo图片路径
    private String putPath; // 图片输出路径
    private LogoConfig LogoConfig;  // logo图片参数

    /**
     * 获取 是否添加Log图片
     * 
     * @return logoFlg 是否添加Log图片
     */
    public boolean isLogoFlg() {
        return logoFlg;
    }

    /**
     * 设置 是否添加Log图片
     * 
     * @param logoFlg 是否添加Log图片
     */
    public void setLogoFlg(boolean logoFlg) {
        this.logoFlg = logoFlg;
    }

    /**
     * 获取 二维码编码内容
     * 
     * @return content 二维码编码内容
     */
    public String getContent() {
        return content;
    }

    /**
     * 设置 二维码编码内容
     * 
     * @param content 二维码编码内容
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 获取 编码类型
     * 
     * @return barcodeformat 编码类型
     */
    public BarcodeFormat getBarcodeformat() {
        return barcodeformat;
    }

    /**
     * 设置 编码类型
     * 
     * @param barcodeformat 编码类型
     */
    public void setBarcodeformat(BarcodeFormat barcodeformat) {
        this.barcodeformat = barcodeformat;
    }

    /**
     * 获取 生成图片宽度
     * 
     * @return width 生成图片宽度
     */
    public int getWidth() {
        return width;
    }

    /**
     * 设置 生成图片宽度
     * 
     * @param width 生成图片宽度
     */
    public void setWidth(int width) {
        this.width = width;
    }

    /**
     * 获取 生成图片高度
     * 
     * @return height 生成图片高度
     */
    public int getHeight() {
        return height;
    }

    /**
     * 设置 生成图片高度
     * 
     * @param height 生成图片高度
     */
    public void setHeight(int height) {
        this.height = height;
    }

    /**
     * 获取 设置参数
     * 
     * @return hints 设置参数
     */
    public Map<EncodeHintType, ?> getHints() {
        return hints;
    }

    /**
     * 设置 设置参数
     * 
     * @param hints 设置参数
     */
    public void setHints(Map<EncodeHintType, ?> hints) {
        this.hints = hints;
    }

    /**
     * 获取 Logo图片路径
     * 
     * @return logoPath Logo图片路径
     */
    public String getLogoPath() {
        return logoPath;
    }

    /**
     * 设置 Logo图片路径
     * 
     * @param logoPath Logo图片路径
     */
    public void setLogoPath(String logoPath) {
        this.logoPath = logoPath;
    }

    /**
     * 获取 图片输出路劲
     * 
     * @return putPath 图片输出路劲
     */
    public String getPutPath() {
        return putPath;
    }

    /**
     * 设置 图片输出路劲
     * 
     * @param putPath 图片输出路劲
     */
    public void setPutPath(String putPath) {
        this.putPath = putPath;
    }

    /**
     * 获取 logoConfig
     * 
     * @return logoConfig logoConfig
     */
    public LogoConfig getLogoConfig() {
        return LogoConfig;
    }

    /**
     * 设置 logoConfig
     * 
     * @param logoConfig logoConfig
     */
    public void setLogoConfig(LogoConfig logoConfig) {
        LogoConfig = logoConfig;
    }

}


3.二维码生成类(ZXingCodeUtil)

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * 二维码生成google zxing
 * 
 * @author X-rapido
 * 
 */
public class ZXingCodeUtil {
    /**
     * 二维码图片添加Logo
     * 
     * @param bim 图片流
     * @param logoPic Logo图片物理位置
     * @param logoConfig Logo图片设置参数
     * @throws Exception 异常上抛
     */
    private void addLogo_QRCode(BufferedImage bim, File logoPic, LogoConfig logoConfig) throws Exception {
        try {
            // 对象流传输
            BufferedImage image = bim;
            Graphics2D g = image.createGraphics();

            // 读取Logo图片
            BufferedImage logo = ImageIO.read(logoPic);

            // 设置logo的大小,本人设置为二维码图片的20%,因为过大会盖掉二维码
            int widthLogo = logo.getWidth(null) > image.getWidth() * 2 / 10 ? (image.getWidth() * 2 / 10) : logo.getWidth(null), heightLogo = logo
                    .getHeight(null) > image.getHeight() * 2 / 10 ? (image.getHeight() * 2 / 10) : logo.getWidth(null);

            // 计算图片放置位置
            // logo放在中心
            int x = (image.getWidth() - widthLogo) / 2;
            int y = (image.getHeight() - heightLogo) / 2;
            // 开始绘制图片
            g.drawImage(logo, x, y, widthLogo, heightLogo, null);
            g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15);
            g.setStroke(new BasicStroke(logoConfig.getBorder()));
            g.setColor(logoConfig.getBorderColor());
            g.drawRect(x, y, widthLogo, heightLogo);
            g.dispose();
            logo.flush();
            image.flush();

        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 二维码的解析
     * 
     * @param image  图片文件流
     * @return 解析后的Result结果集
     * @throws Exception 错误异常上抛
     */
    @SuppressWarnings("unchecked")
    public Result parseQR_CODEImage(BufferedImage image) throws Exception {
        // 设置解析
        Result result = null;
        try {
            MultiFormatReader formatReader = new MultiFormatReader();

            LuminanceSource source = new BufferedImageLuminanceSource(image);
            Binarizer binarizer = new HybridBinarizer(source);
            BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);

            @SuppressWarnings("rawtypes")
            Map hints = new HashMap();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

            result = formatReader.decode(binaryBitmap, hints);

            System.out.println("resultFormat = " + result.getBarcodeFormat());
            System.out.println("resultText = " + result.getText());
        } catch (Exception e) {
            throw e;
        }
        return result;
    }

    /**
     * 生成二维码bufferedImage图片
     * @param zxingconfig 二维码配置信息
     * @return 生成后的 BufferedImage
     * @throws Exception 异常上抛
     */
    public BufferedImage getQR_CODEBufferedImage(ZXingConfig zxingconfig) throws Exception {
        // Google配置文件
        MultiFormatWriter multiFormatWriter = null;
        BitMatrix bm = null;
        BufferedImage image = null;
        try {
            multiFormatWriter = new MultiFormatWriter();

            // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
            bm = multiFormatWriter.encode(zxingconfig.getContent(), zxingconfig.getBarcodeformat(), zxingconfig.getWidth(), zxingconfig.getHeight(),
                    zxingconfig.getHints());

            int w = bm.getWidth();
            int h = bm.getHeight();
            image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

            // 开始利用二维码数据创建Bitmap图片,分别设为黑白两色
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    image.setRGB(x, y, bm.get(x, y) ? Color.lightGray.getRGB() : Color.white.getRGB());
                    
                }
            }

            // 是否设置Logo图片
            if (zxingconfig.isLogoFlg()) {
                this.addLogo_QRCode(image, new File(zxingconfig.getLogoPath()), zxingconfig.getLogoConfig());
            }
        } catch (WriterException e) {
            throw e;
        }
        return image;
    }

    /**
     * 设置二维码的格式参数
     * 
     * @return
     */
    public Map<EncodeHintType, Object> getDecodeHintType() {
        // 用于设置QR二维码参数
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        // 设置QR二维码的纠错级别(H为最高级别)具体级别信息
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 设置编码方式
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 0);
        hints.put(EncodeHintType.MAX_SIZE, 350);
        hints.put(EncodeHintType.MIN_SIZE, 100);

        return hints;
    }

    /**
     * 这个工具类 ZXingCodeUtil 是提供二维码图片生成的工具 首先使用的时候需要实例化
     * ZXingCodeUtil 然后实例化参数 ZXingConfig 和 LogoConfig 通过下面的演示可以详细看参数是按照什么循序进行设置 最后调用
     * ZXingCodeUtil 中方法 getQR_CODEBufferedImage来生成二维码
     */
    public static void main(String[] args) throws WriterException {
        String content = "食品安全BIOT成都研究院-大数据开发工程师:罗建东";
        System.out.println("inputParam:" + content);
        try {
            // 生成二维码
            File file = new File("E:/Users/yulong/git/myutils/test3.png");
            ZXingCodeUtil zp = new ZXingCodeUtil(); // 实例化二维码工具
            ZXingConfig zxingconfig = new ZXingConfig();    // 实例化二维码配置参数
            zxingconfig.setHints(zp.getDecodeHintType());   // 设置二维码的格式参数
            zxingconfig.setContent(content);// 设置二维码生成内容
            zxingconfig.setLogoPath("E:/Users/yulong/git/myutils/ljd1.png"); // 设置Logo图片
            zxingconfig.setLogoConfig(new LogoConfig());    // Logo图片参数设置   
            zxingconfig.setLogoFlg(true);   // 设置生成Logo图片
            BufferedImage bim = zp.getQR_CODEBufferedImage(zxingconfig);// 生成二维码
            ImageIO.write(bim, "png", file);    // 图片写出
            Thread.sleep(500);  // 缓冲

            zp.parseQR_CODEImage(bim);  // 解析调用
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}




4.测试通过



路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

关闭

推荐上一条 /2 下一条