バーコードは可視化され、機械で読み取り可能なデータであり、これらのデータは通常そのバーコードを運ぶものに関する情報を記述している。バーコードは商品流通、図書管理、郵便管理、銀行システムなどの分野で広く使われています。この記事では、一般的な一次元と二次元バーコードの生成と走査について説明します。
必要なツール:
- Free Spire.Barcode for Java
- Intellij IDEA
以下は無料バージョンでサポートされているバーコードタイプのリストです。
P.s. もっとバーコードの種類がほしいです。Spire.Barcode for Java商業版を参考にしてください。
Barcode生成:
バーコードの生成は二つの重要な種類に及んでいます。一つはBarceode Settingsで、もう一つはBarceode Generatorです。Barch odeSettingsはバーコードをカスタマイズするための特定のタイプで、データ、サイズ、色などです。Barceode Generatorは、BarcedeSettingsをベースに画像データを作成します。上の表にサポートされている部分バーコードの生成は以下の通りです。
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"));
}
}
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"));
}
}
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"));
}
}
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"));
}
}
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"));
}
}
いくつかのバーコードの集合を一緒にスキャンし、Barcode Scanner類のscan()方法で複数のバーコードデータを読み取る試みです。画像とコードは以下の通りです。
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]);
}
}
}