四时宝库

程序员的知识宝库

工具类:Java版本二维码生成以及解码

有时候我们经常会想要实现二维码批量生成的功能,这里提供一个二维码生成以及解析的工具类,包括可以上传logo,代码如下:

/**
 * 二维码工具类
 * @date 20198年10月31日
 * @version 1.0
 */
public class QRCodeUtils {
    // 二维码大小
    private static final int QRCODE_SIZE = 300;  
    // 二维码中间logo的尺寸
    private static final int WIDTH  = 60;
    private static final int HEIGHT = 60;
    public static void encode(String content, String logoPath, String imgPath)  throws Exception {  
        encode(content, logoPath, imgPath, QRCODE_SIZE, 1);  
    }
    public static void encode(String content, String logoPath, String imgPath, int size, int margin)  throws Exception {  
        BufferedImage image = createImage(content, logoPath, size, margin);  
        ImageIO.write(image, "png", new File(imgPath));  
    }
    public static void encode(String content, String logoPath, OutputStream output) throws Exception {  
        encode(content, logoPath, output, QRCODE_SIZE, 1);
    }
    public static void encode(String content, String logoPath, OutputStream output, int size, int margin) throws Exception {  
        BufferedImage image = createImage(content, logoPath, size, margin);  
        ImageIO.write(image, "png", output);  
    }
    private static BufferedImage createImage(String content, String imgPath, int size, int margin) throws Exception {  
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();  
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);  
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");  
        hints.put(EncodeHintType.MARGIN, margin); 
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, size, size, hints); 
        int[] rec = bitMatrix.getEnclosingRectangle();  
        int resWidth  = rec[2] + 1;  
        int resHeight = rec[3] + 1;  
        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);  
        resMatrix.clear();  
        for (int i = 0; i < resWidth; i++) {  
            for (int j = 0; j < resHeight; j++) {  
                if (bitMatrix.get(i + rec[0], j + rec[1])) { 
                     resMatrix.set(i, j); 
                } 
            }  
        }  
        int width  = bitMatrix.getWidth();  
        int height = bitMatrix.getHeight();  
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        for (int x = 0; x < width; x++) {  
            for (int y = 0; y < height; y++) {  
                image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);  
            }  
        }  
        if (imgPath == null || "".equals(imgPath)) {  
            return image;  
        }  
        // 插入图片  
        insertImage(image, imgPath, size);  
        return image;  
    }  
    private static void insertImage(BufferedImage source, String imgPath, int size) throws Exception {  
        File file = new File(imgPath);  
        if (!file.exists()) {  
            System.out.println(""+imgPath+"   该文件不存在!");  
            return;  
        }  
        Image src  = ImageIO.read(new File(imgPath));  
        int width  = src.getWidth(null);  
        int height = src.getHeight(null);
        // 压缩LOGO
        if (width > WIDTH) {  
            width = WIDTH;  
        }  
        if (height > HEIGHT) {  
            height = HEIGHT;  
        }  
        Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);  
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
        Graphics g = tag.getGraphics();  
        g.drawImage(image, 0, 0, null); // 绘制缩小后的图  
        g.dispose();  
        src = image;  
        // 插入LOGO  
        Graphics2D graph = source.createGraphics();  
        int x = (size - width) / 2;  
        int y = (size - height) / 2;  
        graph.drawImage(src, x, y, width, height, null);  
        Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);  
        graph.setStroke(new BasicStroke(3f));  
        graph.draw(shape);  
        graph.dispose();  
    }  
    public static String decode(File file) throws Exception {  
        BufferedImage image;  
        image = ImageIO.read(file);  
        if (image == null) {  
            return null;  
        }  
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);  
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));  
        Result result;  
        Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();  
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");  
        result = new MultiFormatReader().decode(bitmap, hints);  
        return result != null ? result.getText() : null;  
    }  
    public static void main(String[] args) throws Exception {
        QRCodeUtils.encode("https://www.suibibk.com/blog/579412311547052032/582955445098905600/639208466002477056", "e:/logo.jpg", "e:/qrcode.png");
        // 解析二维码存储的内容并打印
        System.out.println(decode(new File("e:/qrcode.png")));
    }
}

那些超好用并且在Chrome商店超过10万用户的JSON格式化插件

原文链接:

https://www.lskyf.com/post/51

JSON Formatter

下载地址

http://cj.lskyf.com/mhimpmpmffogbmmkmajibklelopddmjf.html

用户数:989,391 4.5星 推荐理由:没有跟踪,没有广告,没有别的邪恶。

你能搞定二维码吗?2分钟包教包会 | #全民求知季

本文有小虾同学VX公众号xxtx2018发布。我们是一群一线信息技术教师,学无止境,怕只怕不愿意接受和学习新鲜事物,小虾童鞋愿与你一起学习、一起成长,关注读书、职场以及信息技术的巧妙应用,从细节处提升生活的幸福感,从点滴处促进工作的高效能!


二维码在线生成器如何使用?长连接转化成短连接如何实现?

二维码在线生成器如何使用?长连接转化成短连接如何实现

随着互联网的发展,人们使用网络的方式也越来越多样化。其中,二维码成为了人们连接网络的一种重要方式。下面,我们将介绍二维码在线生成器以及如何将长连接转化成短连接。

二维码生成解析用ZXing.NET就够了,不要再引一堆生成和解析库了

ZXing.NET 是一个开源的、功能强大的二维码处理库,它能够对二维码进行解码(读取信息)和编码(生成二维码)。ZXing 是 "Zebra Crossing" 的缩写,是一个跨平台的、用于解码和生成条形码和二维码的库。以下是一些 ZXing.Net 的主要功能通过实例讲解。

二维码生成器如何使用?免费二维码生成器有哪些?

二维码生成器是一种能够自动生成二维码的软件工具,通常用于将信息或图像编码在二维码中,以便在扫描时快速访问或查看。在这篇文章中,我们将介绍如何使用免费的二维码生成器来创建自己的二维码,以及有哪些常见的免费二维码生成器可供选择。

二维码防伪代码(二维码防伪代码怎么看)

网址生成二维码如何使用?怎么把图片转换成二维码?

网址生成二维码如何使用?怎么把图片转换成二维码?

随着互联网的普及,二维码被越来越广泛地应用于各种领域,例如商品营销、信息传递、身份验证等等。那么,网址生成二维码和图片转换成二维码的原理和步骤是什么呢?下面将详细介绍。

怎么从二维码中提取链接?二维码链接访问内容怎么弄?

怎么将二维码转链接使用?现在二维码的广泛使用,极大的提升了现在人们获取内容的便捷性。但是在一些特定的情况下,无法通过扫描二维码的方式获取内容,那么可以通过访问二维码链接来查看内容。那么如何获取二维码链接呢?

.NET C#利用ZXing生成、识别二维码/条形码

ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。这篇文章主要给大家介绍了.NET C#利用ZXing生成、识别二维码/条形码的方法,文中给出了详细的示例代码,有需要的朋友们可以参考借鉴

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接