Giter Club home page Giter Club logo

zxing's Introduction

Nepxion Zxing

Total lines License Maven Central Javadocs Build Status Codacy Badge

Nepxion Zxing是一款基于Google Zxing的二维码/条形码生成组件。支持二维码/条形码创建和扫描,支持创建成本地图片和字节数组两种格式

请联系我

微信和公众号

Alt text Alt text

简介

/**
 * 相关参数说明
 * text              二维码/条形码内容。二维码可以是文字,也可以是URL,条形码必须是数字
 * format            二维码/条形码图片格式,例如jpg,png
 * encoding          二维码/条形码内容编码,例如UTF-8
 * correctionLevel   二维码/条形码容错等级,例如ErrorCorrectionLevel.H(30%纠正率),ErrorCorrectionLevel.Q(25%纠正率),ErrorCorrectionLevel.M(15%纠正率),ErrorCorrectionLevel.L(7%纠正率)。纠正率越高,扫描速度越慢
 * width             二维码/条形码图片宽度
 * height            二维码/条形码图片高度
 * margin            二维码/条形码图片白边大小,取值范围0~4
 * foregroundColor   二维码/条形码图片前景色。格式如0xFF000000
 * backgroundColor   二维码/条形码图片背景色。格式如0xFFFFFFFF
 * deleteWhiteBorder 二维码图片白边去除。当图片面积较小时候,可以利用该方法扩大二维码/条形码的显示面积
 * logoFile          二维码Logo图片的文件,File对象。显示在二维码中间的Logo图片,其在二维码中的尺寸最大为100x100左右,否则会覆盖二维码导致最后不能被识别
 * outputFile        二维码/条形码图片的导出文件,File对象
 */

依赖

<dependency>
  <groupId>com.nepxion</groupId>
  <artifactId>zxing</artifactId>
  <version>${zxing.version}</version>
</dependency>

示例

创建二维码图片并扫描的调用入口

public static void executeForQRFile() {
    // 二维码内容
    String text = "https://github.com/Nepxion/";
    // 二维码图片导出路径
    File file = new File("E:/二维码.jpg");

    // 二维码参数的构造对象,很多参数赋予了默认值,可自行通过set方法更改
    ZxingEntity entity = new ZxingEntity();
    entity.setBarcodeFormat(BarcodeFormat.QR_CODE);
    entity.setLogoFile(new File("src/test/resources/logo.png"));
    entity.setText(text);
    entity.setOutputFile(file);
    entity.setWidth(300);
    entity.setHeight(300);

    // 以文件格式读取并导出,该方式适合本地调用
    ZxingEncoder encoder = new ZxingEncoder();
    encoder.encodeForFile(entity);

    // 以文件格式扫描并解析
    ZxingDecoder decoder = new ZxingDecoder();
    Result result = decoder.decodeByFile(file, entity.getEncoding());

    System.out.println("扫描结果 - [Text] : " + result.getText() + " [Timestamp] : " + result.getTimestamp() + " [BarcodeFormat] : " + result.getBarcodeFormat() + " [NumBits] : " + result.getNumBits());
}

创建二维码图片字节数组(用于网络传递)并扫描的调用入口

public static void executeForQRBytes() throws IOException {
    // 二维码内容
    String text = "https://github.com/Nepxion/";
    // 二维码图片导出路径
    File file = new File("E:/二维码.jpg");

    // 二维码参数的构造对象,很多参数赋予了默认值,可自行通过set方法更改
    ZxingEntity entity = new ZxingEntity();
    entity.setBarcodeFormat(BarcodeFormat.QR_CODE);
    entity.setLogoFile(new File("src/test/resources/logo.png"));
    entity.setText(text);
    entity.setOutputFile(file);
    entity.setWidth(300);
    entity.setHeight(300);

    // 以字节数组格式读取并导出,该方式适合服务端传输给客户端调用
    ZxingEncoder encoder = new ZxingEncoder();
    byte[] bytes = encoder.encodeForBytes(entity);

    ZxingUtils.createFile(bytes, file);

    // 以字节数组格式扫描并解析
    ZxingDecoder decoder = new ZxingDecoder();
    Result result = decoder.decodeByBytes(bytes, entity.getEncoding());

    System.out.println("扫描结果 - [Text] : " + result.getText() + " [Timestamp] : " + result.getTimestamp() + " [BarcodeFormat] : " + result.getBarcodeFormat() + " [NumBits] : " + result.getNumBits());
}

创建条形码图片并扫描的调用入口

public static void executeForEANFile() {
    // 条形码内容
    String text = "6943620593115";
    // 条形码图片导出路径
    File file = new File("E:/条形码.jpg");

    // 条形码参数的构造对象,很多参数赋予了默认值,可自行通过set方法更改
    ZxingEntity entity = new ZxingEntity();
    entity.setBarcodeFormat(BarcodeFormat.EAN_13);
    entity.setText(text);
    entity.setOutputFile(file);
    entity.setWidth(560);
    entity.setHeight(200);

    // 以文件格式读取并导出,该方式适合本地调用
    ZxingEncoder encoder = new ZxingEncoder();
    encoder.encodeForFile(entity);

    // 以文件格式扫描并解析
    ZxingDecoder decoder = new ZxingDecoder();
    Result result = decoder.decodeByFile(file, entity.getEncoding());

    System.out.println("扫描结果 - [Text] : " + result.getText() + " [Timestamp] : " + result.getTimestamp() + " [BarcodeFormat] : " + result.getBarcodeFormat() + " [NumBits] : " + result.getNumBits());
}

创建条形码图片字节数组(用于网络传递)并扫描的调用入口

public static void executeForEANBytes() throws IOException {
    // 条形码内容
    String text = "6943620593115";
    // 条形码图片导出路径
    File file = new File("E:/条形码.jpg");

    // 条形码参数的构造对象,很多参数赋予了默认值,可自行通过set方法更改
    ZxingEntity entity = new ZxingEntity();
    entity.setBarcodeFormat(BarcodeFormat.EAN_13);
    entity.setText(text);
    entity.setOutputFile(file);
    entity.setWidth(560);
    entity.setHeight(200);

    // 以字节数组格式读取并导出,该方式适合服务端传输给客户端调用
    ZxingEncoder encoder = new ZxingEncoder();
    byte[] bytes = encoder.encodeForBytes(entity);

    ZxingUtils.createFile(bytes, file);

    // 以字节数组格式扫描并解析
    ZxingDecoder decoder = new ZxingDecoder();
    Result result = decoder.decodeByBytes(bytes, entity.getEncoding());

    System.out.println("扫描结果 - [Text] : " + result.getText() + " [Timestamp] : " + result.getTimestamp() + " [BarcodeFormat] : " + result.getBarcodeFormat() + " [NumBits] : " + result.getNumBits());
}

运行结果

二维码示例图片

Alt text

条形码示例图片

Alt text

zxing's People

Contributors

haojunren avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.