广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java生成读取条形码和二维码的简单示例
  • 869
分享到

Java生成读取条形码和二维码的简单示例

2024-04-02 19:04:59 869人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

条形码 将宽度不等的多个黑条和白条,按照一定的编码规则排序,用以表达一组信息的图像标识符 通常代表一串数字 / 字母,每一位有特殊含义 一般数据容量30个数字 / 字母 二维码

条形码

将宽度不等的多个黑条和白条,按照一定的编码规则排序,用以表达一组信息的图像标识符

通常代表一串数字 / 字母,每一位有特殊含义

一般数据容量30个数字 / 字母

二维码

用某种特定几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息

比一维条形码能存储更多信息,表示更多数据类型

能够存储数字 / 字母 / 汉字 / 图片等信息

可存储几百到几十KB字符

Zxing

Zxing主要是Google出品的,用于识别一维码和二维码的第三方库

主要类:

  • BitMatrix位图矩阵
  • MultiFORMatWriter位图编写器
  • MatrixToImageWriter写入图片

Maven导入Zxing


<dependencies>
        <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.0.0</version>
        </dependency>
</dependencies>

生成一维码java


public static void main(String[] args) {
    generateCode(new File("1dcode.png"), "1390351289", 500, 250);
}

public static void generateCode(File file, String code, int width, int height){
    // 定义位图矩阵BitMatrix
    BitMatrix matrix = null;
    try {
        // 使用code_128格式进行编码生成100*25的条形码
        MultiFormatWriter writer = new MultiFormatWriter();

        matrix = writer.encode(code, BarcodeFormat.CODE_128, width, height, null);
    } catch (WriterException e) {
        e.printStackTrace();
    }

    // 将位图矩阵BitMatrix保存为图片
    try {
        FileOutputStream outputStream = new FileOutputStream(file);
        ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "png", outputStream);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

注意:一维码只能存储数字和字母,其他数据会报Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project MavenDemo: Command execution failed.错误java

读取一维码


public static void main(String[] args) {
    readCode(new File("1dcode.png"));
}

public static void readCode(File readImage) {
    try {
        BufferedImage image = ImageIO.read(readImage);
        if (image == null) {
            return;
        }
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

        Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, "gbk");
        hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
        hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

        Result result = new MultiFormatReader().decode(bitmap, hints);
        System.out.println(result.getText());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

注意:当使用String类进行转码时,要使用Java.lang包的,Maven导包的时候会导入第三方Apache的String类

生成二维码



private final static int WIDTH = 300;

private final static int HEIGHT = 300;

private final static String FORMAT = "png";


public static void generateQRCode(File file, String content) {
    // 定义二维码参数
    Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
    // 设置编码
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    // 设置容错等级
    hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
    // 设置边距,默认为5
    hints.put(EncodeHintType.MARGIN, 2);

    try {
        BitMatrix bitMatrix = new MultiFormatWriter()
                .encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
        Path path = file.toPath();
        // 保存到项目跟目录中
        MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) {
    generateQRCode(new File("smt.png"), "淑玫唐家居网");
}

读取二维码



public static void readQRCode(File file) {
    MultiFormatReader reader = new MultiFormatReader();
    try {
        BufferedImage image = ImageIO.read(file);
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
        Map<DecodeHintType, Object> hints = new HashMap<>();
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result result = reader.decode(binaryBitmap, hints);
        System.out.println("解析结果: " + new String(result.toString().getBytes("GBK"), "GBK"));
        System.out.println("二维码格式: " + result.getBarcodeFormat());
        System.out.println("二维码文本内容: " + new String(result.getText().getBytes("GBK"), "GBK"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) {
    readQRCode(new File("smt.png"));
}

注意: Maven打印的控制台中会出现中文乱码,在idea Setting->maven->runner VMoptions:-Dfile.encoding=GB2312;即可解决

总结

到此这篇关于Java生成读取条形码和二维码的文章就介绍到这了,更多相关Java生成读取条形码二维码内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java生成读取条形码和二维码的简单示例

本文链接: https://www.lsjlt.com/news/130189.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作