Class BarcodeGenerator
BarcodeGenerator for backend barcode images generation.
supported symbologies: 1D: Codabar, Code11, Code128, Code39, Code39FullASCII Code93Standard, Code93Extended, EAN13, EAN8, Interleaved2of5, MSI, Standard2of5, UPCA, UPCE, ISBN, GS1Code128, Postnet, Planet EAN14, SCC14, SSCC18, ITF14, SingaporePost ... 2D: Aztec, DataMatrix, PDf417, QR code ...
This sample shows how to create and save a barcode image.BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_128); generator.setCodeText("123ABC"); generator.save("code128.png");
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an instance of BarcodeGenerator.BarcodeGenerator(BaseEncodeType type, String codeText) Creates an instance of BarcodeGenerator. -
Method Summary
Modifier and TypeMethodDescriptionvoiddispose()Clean up any resources being used.booleanexportToXml(OutputStream xml) Exports BarCode properties to the xml-stream specifiedbooleanexportToXml(String xmlFile) Exports BarCode properties to the xml-file specifiedandroid.graphics.BitmapGenerate the barcode image under current settings.Barcode symbology type.Text to be encoded.Generation parameters.static BarcodeGeneratorimportFromXml(InputStream xml) Imports BarCode properties from the xml-stream specified and creates BarcodeGenerator instance.static BarcodeGeneratorimportFromXml(String xmlFile) Imports BarCode properties from the xml-file specified and creates BarcodeGenerator instance.voidsave(OutputStream stream, BarCodeImageFormat format) Save BarCodeImage to stream in specific format.voidSave barcode image to specific file.voidsave(String filename, BarCodeImageFormat format) Save barcode image to specific file in specific format.voidsetBarcodeType(BaseEncodeType value) Barcode symbology type.voidsetCodeText(byte[] codeBytes) Set codetext as sequence of bytes.voidsetCodeText(String value) Text to be encoded.voidsetCodeText(String codeText, Charset encoding) voidsetCodeText(String codeText, Charset encoding, boolean insertBOM)
-
Constructor Details
-
BarcodeGenerator
Creates an instance of BarcodeGenerator.
- Parameters:
type- Barcode symbology type. UseEncodeTypesclass to setup a symbology.
-
BarcodeGenerator
Creates an instance of BarcodeGenerator.
- Parameters:
type- Barcode symbology type. UseEncodeTypesclass to setup a symbology.codeText- Text to be encoded.
-
-
Method Details
-
getParameters
Generation parameters.
-
getBarcodeType
Barcode symbology type.
-
setBarcodeType
Barcode symbology type.
-
getCodeText
Text to be encoded.
-
setCodeText
Text to be encoded.
-
setCodeText
public void setCodeText(byte[] codeBytes) Set codetext as sequence of bytes.
- Parameters:
codeBytes- Bytes of codetext
-
setCodeText
Encodes the Unicode
<b>codeText</b>into a byte sequence using the specified<b>encoding</b>. UTF-8 is the most commonly used encoding. If the encoding supports it, the function automatically inserts a<a href="https://en.wikipedia.org/wiki/Byte_order_mark#Byte-order_marks_by_encoding">byte order mark (BOM)</a>.This function is intended for use with 2D barcodes only (e.g., Aztec, QR, DataMatrix, PDF417, MaxiCode, DotCode, HanXin, RectMicroQR, etc.). It enables manual encoding of Unicode text using national or special encodings; however, this method is considered obsolete in modern applications. For modern use cases,
<a href="https://en.wikipedia.org/wiki/Extended_Channel_Interpretation">ECI</a>encoding is recommended for Unicode data.Using this function with 1D barcodes, GS1-compliant barcodes (including 2D), or HIBC barcodes (including 2D) is not supported by the corresponding barcode standards and may lead to unpredictable results.
This example shows how to use
SetCodeTextsetting Unicode-encoded text for 2D barcodes using different encodings://Encode QR Code text using UTF-8 with BOM BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR); gen.setCodeText("車種名", StandardCharsets.UTF_8; gen.save("barcode.png", BarCodeImageFormat.PNG); BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.QR); for(BarCodeResult result : reader.readBarCodes()) System.out.println("BarCode CodeText: " + result.getCodeText()); //Encode DataMatrix text using Shift-JIS (Japanese encoding) BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.DATA_MATRIX); gen.setCodeText("車種名", Charset.forName("932")); gen.save("barcode.png", BarCodeImageFormat.PNG); BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.DATA_MATRIX); for(BarCodeResult result : reader.readBarCodes()) System.out.println("BarCode CodeText: " + result.getCodeText(Charset.forName("932")));- Parameters:
codeText- CodeText stringencoding- Applied encoding
-
setCodeText
Encodes the Unicode
<b>codeText</b>into a byte sequence using the specified<b>encoding</b>. UTF-8 is the most commonly used encoding. If the encoding supports it and<b>insertBOM</b>is set totrue, the function includes a<a href="https://en.wikipedia.org/wiki/Byte_order_mark#Byte-order_marks_by_encoding">byte order mark (BOM)</a>.This function is intended for use with 2D barcodes only (e.g., Aztec, QR, DataMatrix, PDF417, MaxiCode, DotCode, HanXin, RectMicroQR, etc.). It enables manual encoding of Unicode text using national or special encodings; however, this method is considered obsolete in modern applications. For modern use cases,
<a href="https://en.wikipedia.org/wiki/Extended_Channel_Interpretation">ECI</a>encoding is recommended for Unicode data.Using this function with 1D barcodes, GS1-compliant barcodes (including 2D), or HIBC barcodes (including 2D) is not supported by the corresponding barcode standards and may lead to unpredictable results.
This example shows how to use
SetCodeTextwith or without a BOM for 2D barcodes.//Encode codetext using UTF-8 with BOM BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR); gen.setCodeText("車種名", StandardCharsets.UTF_8, true); gen.save("barcode.png", BarCodeImageFormat.PNG); BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.QR); for(BarCodeResult result : reader.readBarCodes()) System.out.println("BarCode CodeText: " + result.getCodeText()); //Encode codetext using UTF-8 without BOM BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR); gen.setCodeText("車種名", StandardCharsets.UTF_8, false); gen.save("barcode.png", BarCodeImageFormat.PNG); BarCodeReader reader = new BarCodeReader("barcode.png", DecodeType.QR); for(BarCodeResult result : reader.readBarCodes()) System.out.println("BarCode CodeText: " + result.getCodeText());- Parameters:
codeText- CodeText stringencoding- Applied encodinginsertBOM- Indicates whether to insert a byte order mark (BOM) when the specified encoding supports it (e.g., UTF-8, UTF-16, UTF-32). If set totrue, the BOM is added; iffalse, the BOM is omitted even if the encoding normally uses one.
-
generateBarCodeImage
public android.graphics.Bitmap generateBarCodeImage()Generate the barcode image under current settings.
This sample shows how to create and save a barcode image.
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_128); File outputFile = new File("test.png"); OutputStream os = new FileOutputStream(outputFile); Bitmap generatedBitmap = generator.generateBarCodeImage(); generatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, os); os.flush(); os.close();- Returns:
- Barcode image. See
Bitmap.
-
save
Save BarCodeImage to stream in specific format.
- Throws:
IOException
-
save
Save barcode image to specific file in specific format.
- Parameters:
filename- Path to save to.format- Specifies the file format of the output image.- Throws:
IOException
-
save
Save barcode image to specific file.
- Parameters:
filename- Path to save to.- Throws:
IOException
-
exportToXml
Exports BarCode properties to the xml-file specified
- Parameters:
xmlFile- The name of the file- Returns:
- Whether or not export completed successfully.
Returns
<b>True</b>in case of success;<b>False</b>Otherwise
-
exportToXml
Exports BarCode properties to the xml-stream specified- Parameters:
xml- The xml-stream- Returns:
- Whether or not export completed successfully. Returns True in case of success; False Otherwise
- Throws:
IOException
-
importFromXml
Imports BarCode properties from the xml-file specified and creates BarcodeGenerator instance.
- Parameters:
xmlFile- The name of the file- Returns:
- BarcodeGenerator instance
-
importFromXml
Imports BarCode properties from the xml-stream specified and creates BarcodeGenerator instance.- Parameters:
xml- The xml-stream- Returns:
- BarcodeGenerator instance
-
dispose
public void dispose()Clean up any resources being used.
-