`

二维码生产与解析

阅读更多

 

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
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.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCodeUtils {
    private static final String CODE = "utf-8";
    private static final String IMAGE_TYPE = "png";
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
    private static final int LOGO_SIZE = 60;  

     /** 
     * 生成RQ二维码    默认生成二维码
     *  
     * @author wfhe 
     * @param content 
     *            内容 
     * @param height 
     *            高度(px) 
     * @param width 
     *            高度(px)
     *  
     */  
    public static BufferedImage getRQ(String content, Integer height, Integer width, 
             String logoPath, boolean needCompress) {
        if(height == null || height < 100) {
            height = 200;
        }
        if(width == null || width < 50) {
            width = height;
        }

        try {
            Hashtable<EncodeHintType, Object> h = new Hashtable<EncodeHintType, Object>();
            h.put(EncodeHintType.CHARACTER_SET, CODE);
            h.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            h.put(EncodeHintType.MARGIN, 1);

            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, h);

            return toBufferedImage(bitMatrix, logoPath, needCompress);

        } catch (WriterException e) {
            e.printStackTrace();
        }

        return null;
    }

     /** 
     * 转换成图片 
     *  
     * @author wfhe 
     * @param bitMatrix 
     * @param logoPath logo文件路径
     * @param needCompress logo是否要压缩 
     * @return 
     */  
    private static BufferedImage toBufferedImage(BitMatrix bitMatrix, String logoPath, boolean needCompress ){
        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); 
        for(int x = 0; x < width; x++) {
            for(int y = 0; y < height; y ++) {
                image.setRGB(x, y, bitMatrix.get(x, y)? BLACK : WHITE);
            }
        }

        if(logoPath != null && !"".equals(logoPath)) wlogo(image, logoPath, needCompress);
        return image;
    }

    /**
     *  生成一、二维码,写到文件中
     *  
     * @author wfhe
     * @param content 内容
     * @param height 高度
     * @param width 宽度
     * @param filePath 文件路径
     * @throws Exception 
     */
    public static void wQRFile(String content, Integer height, Integer width, String filePath,
             String logoPath, boolean needCompress
            ) throws Exception {
        if(filePath == null || "".equals(filePath)) throw new Exception("filePath erorr : " + filePath);
        File file = new File(filePath);
        if(file == null || file.exists() == false) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                throw new Exception("filePath create fail : " + filePath);
            }
        }


        BufferedImage image  = getRQ(content, height, width, logoPath, needCompress);
        ImageIO.write(image, IMAGE_TYPE, file);
    }

    /**
     * @author wfhe
     * @param filePath 需要解析的二维码图片路径
     * @return
     */
    public static String resolve(String filePath) {
        try {
            if(filePath == null || "".equals(filePath)) throw new Exception("filePath erorr : " + filePath);
            File file = new File(filePath);
            if(file == null || file.exists() == false) throw new Exception("File Not Found : " + filePath);

            BufferedImage imge = ImageIO.read(file);

            LuminanceSource source = new BufferedImageLuminanceSource(imge);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

            // 解码设置编码方式为:utf-8
            Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();  
            hints.put(DecodeHintType.CHARACTER_SET, CODE);  

            Result result = new MultiFormatReader().decode(bitmap, hints);

            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @author wfhe
     * @param bufferedImage 图片缓存
     * @param logoPath logo 图片文件的路径
     * @param needCompress 是否要压缩logo
     */
    private static void wlogo(BufferedImage bufferedImage, String logoPath, boolean needCompress) { 
        try {
            File file = new File(logoPath);  
            if (!file.exists()) {  
                    throw new Exception("File Not Found : " + logoPath);
            }
            Image logo = ImageIO.read(file);
            int width = logo.getWidth(null);
            int height = logo.getHeight(null);
            if(needCompress){// 压缩LOGO
                width = LOGO_SIZE;
                height = LOGO_SIZE;

                logo = logo.getScaledInstance(width, height, Image.SCALE_SMOOTH);
                BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
                Graphics g = tag.getGraphics();  
                g.drawImage(logo, 0, 0, null); // 绘制缩小后的图  
                g.dispose();  
            }
            // 插入LOGO  
            Graphics2D grap = bufferedImage.createGraphics();
            int x = (bufferedImage.getWidth() - width) >> 1;
            int y = (bufferedImage.getHeight() - height) >> 1;
            grap.drawImage(logo, x, y, width, height, null);
            Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
            grap.setStroke(new BasicStroke(3f));
            grap.draw(shape);
            grap.dispose();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        String filePath = "d://1.png";
        String logoPath = "d://111.jpg";

        QRCodeUtils.wQRFile("http://www.baidu.com", null, null, filePath, logoPath, true);

        System.out.println("-----成生成功----");

        String s = QRCodeUtils.resolve(filePath);

        System.out.println("-----解析成功----");
        System.out.println(s);

    }

}

 

 

分享到:
评论

相关推荐

    二维码生产和解析

    winform版和web版两种生产二维码方式

    QRCODE二维码生产

    二维码生成与解析,有源代码。能根据输入的信息生成相应的二维码,还能把二维码解析为具体的信息-Two-dimensional code generated with the analytical

    qrcode解析二维码

    在QRcode的压缩包里面包含两个文件夹,分别对应着生产二维码的demo和解析二维码的demo,用户可以根据自己的实际情况来选择使用或者改写插件。

    JAVA 生成二维码并设置失效机制

    1.通过QRCode.jar包生成二维码,可设置二维码图片格式,二维码图片存放路径,二维码尺寸,二维码颜色 2.二维码扫描内容分为两种,1种为...4.提供通过QRCode.jar生成二维码的全部生产线上代码,可直接运行,含有关键注释

    二维码生成以及扫一扫解析二维码原理实例

    是java后台的二维码生成以及扫一扫解析二维码原理的实例,包含二维码生产,以及解析原理,代码实现不易。

    利用zixng方式生产及解析二维码

    该资源利用zxing lib 生成及解析二维码,相对QRCodeDecoder 方式来说代码更简洁,解析时候的错误也少,遗憾的地方就是调用前设值的地方比较多,建议封装下

    java实现生成二维码(包括必要jar)

    java中二维码生成和解析的必须jar包 以及完整的java代码 可以直接使用

    Java生成二维码jar包及全部教程和源码

    那么大家想不想知道如何生成二维码,以及如何去解析二维码呢?本文将为大家介绍Java中三种二维码的实现方式,分为使用ZXing、QRCode以及jquery-qrcode。 二维码的分类:线性堆叠式二维码、矩阵式二维码、邮政码。 ...

    二维码小助手-crx插件

    解析页面上某个二维码 QR Helper QR code encoder/decoder 二维码小助手 1. 根据当前页面URL生成二维码 2. 解析页面上某个二维码 3. 本地生成、本地解码支持离线使用 支持语言:English,中文 (简体)

    QRcode的jar包.rar

    基于QRCoder的二维码生产和解析工具的jar包,使用方法:https://blog.csdn.net/weixin_44774463/article/details/115750010

    非均匀光照下的二维码识别算法优化研究

    针对某污水膜生产企业使用的包含产品信息的QR码(二维码的一种)在非均匀光照下产生的阴影区域问题,提出一种基于大津法的改进算法。利用图像的局部区域阈值及窗口逐像素滑动计算来消除光照对图像造成的影响,从而...

    极速二维码-crx插件

    一款现代、极简风格的二维码生成和解析插件,诸多细节可能会让你爱不释手。 1. 页面链接点击可编辑并实时生成二维码 2. 二维码可以缓存在本地,并且可以跨Tab展示 3. 二维码标题及链接可以修改及保存 4. 二维码可以...

    苏州智能工厂建设指南.doc

    2.1.3 质量控制可追溯 建立数据采集与监视控制系统(SCADA),通过条形码、二维码或无线射频识别(R FID)卡等识别技术,可查看每个产品生产过程的订单信息、报工信息、批次号、工作中 心、设备信息、人员信息,...

    dwz短网址 3.0 build 20131107.zip

    1.二维码与短网址双重生成 2.模版自由修改 3.淘宝客智能劫持系统 4.独创中间页模式 5.统计系统 6.完善的会员系统  7.可盈利的会员套餐系统  8.短网址扩展 9.短网址安全防护 10.平台数据安全 系统...

    开源mes系统:Java springboot + layui + mysql,看板和后端独立

    1、 产品和原材料双向溯源 (支持二维码扫描输入后的自动解析,设备自动上传产品数据), 2、工艺流程定义, 3、生产计划, 4、工作过程监控, 5、工作进度监控, 6、设备管理, 7、班组管理, 8、质量管理...

    基于国家物联网标识管理公共服务平台的智慧能源云服务平台.pptx

    积成电子每年生产的智能电表、智能水表、智能燃气表 、智能热计量表数百万计,对这些表计的信息化管理尤为重要,通过扫描二维码获取表计的生命历程信息,从产品生产下线开始,经过测试检验,出厂物流、安装调试、...

    条形码标准整理(Code39/EAN-13/QRCode)

    常见一维码和二维码文档整理。 条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符。常见的条形码是由反射率相差很大的黑条(简称条)和白条(简称空)排成的平行...

    google zxing jar文件,版本是3.5.1

    这是一个java打包文件,可以使用来生产不同的barcode。ZXing(Zebra Crossing)是Google开发的一个二维码解析和生成的开源库

    码上闪条形码比价3.3.1

    【软件简介】 码上闪智能手机必备条形码比价、网上商城比价、手机淘宝购物、二维码应用软件。可以识别条形码、二维码(QR码、DM码等),是一个不折不扣的“码”上通。...3.修复了解析某些特殊条码不能解析

Global site tag (gtag.js) - Google Analytics