JAVA generate barcode scanning

Disclaimer: learning reproduced for personal collection, if infringement, please contact me to delete, reprint address: https: //www.cnblogs.com/MariaWang/p/10837641.html

 

  A barcode is a visual, machine-readable data, such data generally describes the information carried in the barcode of the article. Bar code has been widely used in the field of commodity circulation, library management, postal management and banking systems. In this article, we describe how to generate and scan some of the common one-dimensional and two-dimensional bar code.

Tools required:

  • Free Spire.Barcode for Java 1.3 (free version)
  • Intellij IDEA 

Jar package file import:

1 the STEP : After downloading the package to extract Jar, enter "Project Structure" interface. (There are three ways to quickly open the Project Structure IDEA in the interface, to select the desired mode)

2 STEP : import according to the following procedure.

① Select "Modules" - "Dependencies", add external jar package; ② into the "Attach File or Directories" interface to select the jar file path, and then click "OK"; ③ Check the jar path options, and click "OK" / "Apply "; ④ import is complete. As shown below:

Here is the free version supports barcode type list:

PS More barcode types, please refer to the use Spire.Barcode for Java Business Edition. 

 

Barcode生成:

生成条形码涉及到两个重要的类,一个是BarcodeSettings,另一个是BarcodeCenerator。BarcodeSettings是用来定制条形码的特定类型,数据,大小,颜色等。BarcodeCenerator以BarcodeSettings为基础,用来创建图像数据。上表所支持的部分条形码的生成如下所示:

Codebar:

public class CODABAR {

    public static void main(String[] args) throws Exception {
        //创建BarcodeSettings实例
        BarcodeSettings settings = new BarcodeSettings();
        //设置条形码数据
        settings.setData("2030405060");
        //设置符号属性
        settings.setType(BarCodeType.CODABAR);
        //设置底部显示文本
        settings.setShowTextOnBottom(true);
        //设置边框可见
        settings.hasBorder(true);
        //设置CodabarStartChar和CodabarStopChar
        settings.setCodabarStartChar(CodabarChar.B);
        settings.setCodabarStopChar(CodabarChar.D);
        //创建条形码生成器对象
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
        //从条形码生成器中获取图像
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
        //保存图像
        ImageIO.write(bufferedImage,"png",new File("CODABAR.png"));
    }
}

  运行效果:

 

 

Code11:

public class CODE_11 {

    public static void main(String[] args) throws IOException {
        //创建BarcodeSettings实例
        BarcodeSettings settings = new BarcodeSettings();
        //设置条形码数据
        settings.setData("12345-67890");
        //设置符号属性
        settings.setType(BarCodeType.CODE_11);
        //设置底部文本
        settings.setShowTextOnBottom(true);
        //设置边框可见
        settings.hasBorder(true);
        //创建条码生成器对象
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
        //从条码生成期中获取图像
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
        //保存图像
        ImageIO.write(bufferedImage,"png",new File("CODE_11.png"));
    }
}

  运行效果:

 

 

Code 39:

public static void main(String[] args) throws IOException {
    //创建BarcodeSettings
    BarcodeSettings settings = new BarcodeSettings();
    //设置条形码数据
    settings.setData("ABC 123456789");
    //设置符号属性
    settings.setType(BarCodeType.CODE_39);
    //在底部设置显示文本位置
    settings.setShowTextOnBottom(true);
    //设置边框可见
    settings.hasBorder(true);
    //创建条形码生成器实例
    BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
    //从条形码生成器中获取图像
    BufferedImage bufferedImage = barCodeGenerator.generateImage();
    //保存图像
    ImageIO.write(bufferedImage,"png",new File("CODE_39.png"));
}

  运行效果:

 

Code 128:

public class CODE_128 {

    public static void main(String[] args) throws IOException {
        //创建BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
        //设置条形码数据
        settings.setData("ABCD 12345 abcd");
        //设置符号属性
        settings.setType(BarCodeType.CODE_128);
        //在底部设置显示文本位置 
       settings.setShowTextOnBottom(true);
        //设置边界可见
        settings.hasBorder(true);
        //创建条形码生成器实例
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
        //从条形码生成器获取图像
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
        //保存图像
        ImageIO.write(bufferedImage,"png",new File("CODE_128.png"));
    }

  运行效果

 

QR_Code:

public class QR_CODE {
    public static void main(String[] args) throws IOException {
        //创建BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
        //设置条形码数据
        settings.setData("ABC 123456789");
        //设置符号属性
        settings.setType(BarCodeType.QR_CODE);
        //在底部设置ShowText位置
        settings.setShowTextOnBottom(true);
        //设置边框是可见的
        settings.hasBorder(true);
        //创建BarCodeGenerator实例
      BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
        //从条形码生成器获取图像
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
        //保存图像
        ImageIO.write(bufferedImage,"png",new File("QR_CODE.png"));
    }
}

  运行效果:

 

Barcode扫描:

下面我们将尝试将将几个条形码的合集放在一起扫描,使用BarcodeScanner 类的scan()方法读取多个条形码数据。图像和代码如下所示:

public class Scan {
    public static void main(String[] args) throws Exception {
       //从要扫描的图像中获取信息
       String[] s= BarcodeScanner.scan("C:\\Users\\Administrator\\Desktop\\Barcode.png");
        for (int i=0;i< s.length ;i++){
            System.out.println(s[i]);
        }
    }
}

  运行效果:

 

 

Guess you like

Origin www.cnblogs.com/Big-Boss/p/10951274.html
Recommended