LoginSignup
0
2

More than 3 years have passed since last update.

JAVA:多種類のバーコードの生成とスキャンを実現します

Last updated at Posted at 2019-05-15

バーコードは可視化され、機械で読み取り可能なデータであり、これらのデータは通常そのバーコードを運ぶものに関する情報を記述している。バーコードは商品流通、図書管理、郵便管理、銀行システムなどの分野で広く使われています。この記事では、一般的な一次元と二次元バーコードの生成と走査について説明します。

必要なツール:

以下は無料バージョンでサポートされているバーコードタイプのリストです。
9.png
:top: P.s. もっとバーコードの種類がほしいです。Spire.Barcode for Java商業版を参考にしてください。

:one: Barcode生成:

バーコードの生成は二つの重要な種類に及んでいます。一つはBarceode Settingsで、もう一つはBarceode Generatorです。Barch odeSettingsはバーコードをカスタマイズするための特定のタイプで、データ、サイズ、色などです。Barceode Generatorは、BarcedeSettingsをベースに画像データを作成します。上の表にサポートされている部分バーコードの生成は以下の通りです。

:black_small_square:Codebar:


import com.spire.barcode.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class CODABAR {
    public static void main(String[] args) throws Exception {
    //Create the BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
    //Set data
        settings.setData("2030405060");
    //Set the Symbology property
        settings.setType(BarCodeType.CODABAR);
    //Set Show Text location on bottom
        settings.setShowTextOnBottom(true);
    //Set border is visible
        settings.hasBorder(true);
    //Set the CodabarStartChar and CodebarStopChar
        settings.setCodabarStartChar(CodabarChar.B);
        settings.setCodabarStopChar(CodabarChar.D);
    //Create the BarcodeGenerator
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
    //Get image from the BarcodeGenerator
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
    //Save the image
        ImageIO.write(bufferedImage,"png",new File("CODABAR.png"));
    }
}

実行効果:
1.png

:black_small_square:Code11:

import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class CODE_11 {
    public static void main(String[] args) throws IOException {

    //Create the BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
        //Set Data
        settings.setData("12345-67890"); 
    //Set the Symbology property
        settings.setType(BarCodeType.CODE_11);
    //Set ShowText location on bottom
        settings.setShowTextOnBottom(true);
    //Set Border is visible
        settings.hasBorder(true);
    //Create the BarCodeGenerator
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
    //Get image from the BarCodeGenerator
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
    //Save the image
        ImageIO.write(bufferedImage,"png",new File("CODE_11.png"));

    }
}

実行効果:
2.png
:black_small_square:Code 39:

import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class CODE_39 {
    public static void main(String[] args) throws IOException {
        //Create the BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
        //Set Data
        settings.setData("Bar 987654321");
        //Set the Symbology property
        settings.setType(BarCodeType.CODE_39);
        //Set ShowText location on bottom
        settings.setShowTextOnBottom(true);
        //Set Border is visible
        settings.hasBorder(true);
        //Create the BarCodeGenerator
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
        //Get image from the BarCodeGenerator
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
        //Save the image
        ImageIO.write(bufferedImage,"png",new File("CODE_39.png"));

    }
}

実行効果:
3.png

:black_small_square:Code 128:

import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class CODE_128 {
    public static void main(String[] args) throws IOException {
        //Create the BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
        //Set Data
        settings.setData("ABCD 12345 abcd");
        //Set the Symbology property
        settings.setType(BarCodeType.CODE_128);
        //Set ShowText location on bottom
        settings.setShowTextOnBottom(true);
        //Set Border is visible
        settings.hasBorder(true);
        //Create the BarCodeGenerator
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
        //Get image from the BarCodeGenerator
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
        //Save the image
        ImageIO.write(bufferedImage,"png",new File("CODE_128.png"));

    }
}

実行効果:
4.png
:black_small_square:QR_Code:

import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class QR_CODE {
    public static void main(String[] args) throws IOException {
     //Create the BarcodeSettings
        BarcodeSettings settings = new BarcodeSettings();
     //Set Data
        settings.setData("ABC 123456789");
     //Set the Symbology property
        settings.setType(BarCodeType.QR_CODE);
     //Set the QR_CODE size
        settings.setX(2);
     //Set ShowText location on bottom
        settings.setShowTextOnBottom(true);
     //Set Border is visible
        settings.hasBorder(true);
     //Create the BarCodeGenerator
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
         //Get image from the BarCodeGenerator
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
     //Save the image
        ImageIO.write(bufferedImage,"png",new File("QR_CODE.png"));

    }
}

実行効果:
5.png
:two:Barcodeスキャン:

いくつかのバーコードの集合を一緒にスキャンし、Barcode Scanner類のscan()方法で複数のバーコードデータを読み取る試みです。画像とコードは以下の通りです。
6.png

            public class Scan {
            public static void main(String[] args) throws Exception {

                  //Get code information by scanning the image   
             String[] s=BarcodeScanner.scan("C:\\Users\\Administrator\\Desktop\\Barcode.png");
                  for (int i=0;i< s.length ;i++){
                 System.out.println(s[i]);
                     }
                   }
                 }

実行効果:
7.png

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2