Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-barcode/e49c13ebe68efe7c059e32871b13a3ab to your computer and use it in GitHub Desktop.

Select an option

Save aspose-barcode/e49c13ebe68efe7c059e32871b13a3ab to your computer and use it in GitHub Desktop.
This Gist contains code snippets of examples of Aspose.BarCode for Java.
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(ApplyALicense.class) + "License/";
String licFilePath = dataDir + "Aspose.BarCode.lic";
// Apply a license to avoid the evaluation watermark in the barcode image.
License licBarcode = new License();
licBarcode.setLicense(licFilePath);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(ApplyingChecksumValidation.class) + "Barcode/AdvancedFeatures/";
//Generation
//Instantiate BarCodeBuilder object
com.aspose.barcode.BarCodeBuilder builder = new com.aspose.barcode.BarCodeBuilder();
//Set the Code text for the barcode
builder.setCodeText("1234567890");
//Set the symbology type to CodaBar
builder.setEncodeType(com.aspose.barcode.EncodeTypes.CODABAR);
//Set the EnableChecksum property to yes
builder.setEnableChecksum(com.aspose.barcode.EnableChecksum.Yes);
//Set the CodabarChecksumMode
builder.setCodabarChecksumMode(com.aspose.barcode.CodabarChecksumMode.Mod10);
//Save the image on the system
builder.save("Codabar_Mod10.png");
//Recognition
//Initialize reader object
BarCodeReader reader = new BarCodeReader("Codabar_Mod10.png", com.aspose.barcode.barcoderecognition.DecodeType.CODABAR);
//Set ChecksumValidation property of the reader to On
reader.setChecksumValidation(com.aspose.barcode.barcoderecognition.ChecksumValidation.On);
while (reader.read())
{
//Get code text
System.out.println(" codetext: " + reader.getCodeText());
//Get type of barcode
System.out.println(" type: " + reader.getCodeType());
//Get checksum value
System.out.println(" Checksum: " + reader.getCheckSum());
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(ApplyingChecksumValidation.class) + "Barcode/AdvancedFeatures/";
//Generation
//Instantiate BarCodeBuilder object
com.aspose.barcode.BarCodeBuilder builder = new com.aspose.barcode.BarCodeBuilder();
//Set the Code text for the barcode
builder.setCodeText("1234567890");
//Set the symbology type to CodaBar
builder.setEncodeType(com.aspose.barcode.EncodeTypes.CODABAR);
//Set the EnableChecksum property to yes
builder.setEnableChecksum(com.aspose.barcode.EnableChecksum.Yes);
//Set the CodabarChecksumMode
builder.setCodabarChecksumMode(com.aspose.barcode.CodabarChecksumMode.Mod10);
//Save the image on the system
builder.save("Codabar_Mod10.png");
//Recognition
//Initialize reader object
BarCodeReader reader = new BarCodeReader("Codabar_Mod10.png", com.aspose.barcode.barcoderecognition.DecodeType.CODABAR);
//Set ChecksumValidation property of the reader to On
reader.setChecksumValidation(com.aspose.barcode.barcoderecognition.ChecksumValidation.On);
while (reader.read())
{
//Get code text
System.out.println(" codetext: " + reader.getCodeText());
//Get type of barcode
System.out.println(" type: " + reader.getCodeType());
//Get checksum value
System.out.println(" Checksum: " + reader.getCheckSum());
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(ApplyingChecksumValidation.class) + "Barcode/AdvancedFeatures/";
// Create an instance of BarCodeReader class and load an existing
// oncecode barcode.
BarCodeReader r = new BarCodeReader(dataDir + "onecode.png", com.aspose.barcode.barcoderecognition.DecodeType.ONE_CODE);
// Set the ChecksumValidation property to Off.
r.setChecksumValidation(ChecksumValidation.Off);
while (r.read()) {
System.out.println(r.getCodeType() + ": " + r.getCodeText());
System.out.println("CheckSum: " + r.getCheckSum());
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(GenerateAustraliaPostBarcode.class) + "Barcode/AdvancedFeatures/";
//Instantiate BarCodeBuilder object
com.aspose.barcode.BarCodeBuilder builder = new com.aspose.barcode.BarCodeBuilder();
//Set the Code text for the barcode
builder.setCodeText("(01)98898765432106(3202)012345(15)991231");
//Set the symbology type to Code128
builder.setEncodeType(com.aspose.barcode.EncodeTypes.DATABAR_EXPANDED_STACKED);
//Set the cloumn property to define segments per row
builder.setColumns(6);
//Save the image
builder.save("6segmets.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(Checksum.class) + "Barcode/AdvancedFeatures/";
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText("1234567");
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_39_STANDARD);
//Make the checksum to be visible on the barcode
bb.setEnableChecksum(EnableChecksum.Default);
//Save the Barcode image to file
bb.save(dataDir + "checksum.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(GenerateAPatchCode.class) + "Barcode/AdvancedFeatures/";
BarCodeBuilder builder = new BarCodeBuilder();
// set Symbology type
builder.setEncodeType(com.aspose.barcode.EncodeTypes.PATCH_CODE);
builder.setCodeText("Patch T");
builder.save(dataDir + "patch.bmp");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder builder = new BarCodeBuilder("Patch T", com.aspose.barcode.EncodeTypes.PATCH_CODE);
builder.setImageWidth(150); //in millimeters
builder.setMargins(new MarginsF(0.5f, 0.5f, 0.5f, 0.5f)); //make the same small margins
builder.setCodeLocation(CodeLocation.None); //to hide codetext
BufferedImage topImg = builder.generateBarCodeImage();
builder.rotate(90);
BufferedImage leftImg = builder.generateBarCodeImage();
builder.rotate(90);
BufferedImage bottomImg = builder.generateBarCodeImage();
builder.rotate(90);
BufferedImage rigthtImg = builder.generateBarCodeImage();
BufferedImage frameImg = new BufferedImage(topImg.getWidth(), rigthtImg.getHeight() + 2 * topImg.getHeight(), rigthtImg.getType());
// Initialize graphics object from the image
Graphics graphics = frameImg.getGraphics();
graphics.drawImage(topImg, 0, 0, null);
graphics.drawImage(leftImg, 0, topImg.getHeight(), null);
graphics.drawImage(bottomImg, 0, topImg.getHeight() + leftImg.getHeight(), null);
graphics.drawImage(rigthtImg, topImg.getWidth() - rigthtImg.getWidth(), topImg.getHeight(), null);
// save Patch code frame
File outputfile = new File(Utils.getDataDir(GenerateAPatchCode.class) + "Barcode/AdvancedFeatures/" + "patch-code-frame.png");
ImageIO.write(frameImg, "png", outputfile);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(GenerateAustraliaPostBarcode.class) + "Barcode/AdvancedFeatures/";
// Create instance of BarCodeBuilder, specify code-text and Symbology in the constructor
BarCodeBuilder builder = new BarCodeBuilder("1234567890", com.aspose.barcode.EncodeTypes.AUSTRALIA_POST);
// Set format control code to standard
builder.setAustraliaPostFormatControlCode(AustraliaPostFormatControlCode.Standard);
// Save the image to disk in PNG format
builder.save(dataDir + "australiaPostBarcode.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(Checksum.class) + "Barcode/AdvancedFeatures/";
// Create an instance of BarCodeReader class
// Set file path
// Set the recognition type
com.aspose.barcode.barcoderecognition.BarCodeReader reader =
new com.aspose.barcode.barcoderecognition.BarCodeReader("1bc.png", com.aspose.barcode.barcoderecognition.DecodeType.CODE_128);
// Perform read operation
reader.read();
// Create an array of Code128DataPortion class
// Call the GetCode128DataPortions method
com.aspose.barcode.barcoderecognition.Code128DataPortion[] code128DataPortions = reader.getCode128DataPortions();
// Execute Loop for each Code128DataPortion instance
for (com.aspose.barcode.barcoderecognition.Code128DataPortion code128DataPortion : code128DataPortions)
{
// Display the subtype and data
System.out.println("Code128SubType: " + code128DataPortion.getCode128SubType());
System.out.println("Data:" + code128DataPortion.getData());
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(ManageXAndYDimension.class) + "Barcode/AdvancedFeatures/";
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText("12345678");
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
//Set the x-dimension for the bars of the barcode
bb.setxDimension(0.5f);
//Save the Barcode image to file
bb.save(dataDir + "xDimention.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(ManageXAndYDimension.class) + "Barcode/AdvancedFeatures/";
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText("12345678");
bb.setEncodeType(com.aspose.barcode.EncodeTypes.PDF_417);
//Set the Y-Dimension for the bars of the barcode
bb.setyDimension(4);
//Save the Barcode image to file
bb.save(dataDir + "yDimention.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(SetBarsHeight.class) + "Barcode/AdvancedFeatures/";
//Instantiate barcode object
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText("12345678");
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
//Set the bar height to be 3 milimeter
bb.setBarHeight(3.0f);
//Save the Barcode image to file
bb.save(dataDir + "barsHeight.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(SetStartAndStopSymbols.class) + "Barcode/AdvancedFeatures/";
// Create instance of BarCodeBuilder, specify codetext and symbology in the constructor
BarCodeBuilder builder = new BarCodeBuilder("12345678", com.aspose.barcode.EncodeTypes.CODABAR);
// Set the codabar start symbol to A
builder.setCodabarStartSymbol(CodabarSymbol.A);
// Set the codabar stop symbol to D
builder.setCodabarStopSymbol(CodabarSymbol.D);
// Save the image to disk in PNG format
builder.save(dataDir + "startAndStopSymbols.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(SupplementData.class) + "Barcode/AdvancedFeatures/";
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText("123456789123");
bb.setEncodeType(com.aspose.barcode.EncodeTypes.EAN13);
//Set the supplement data (5 Digit)
bb.setSupplementData("12345");
//Set space between the supplemental barcode and main barcode
bb.setSupplementSpace(2.0f);
//Save the Barcode image to file
bb.save(dataDir + "supplementData.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(WideNarrowRatio.class) + "Barcode/AdvancedFeatures/";
//Instantiate barcode object
BarCodeBuilder bb = new BarCodeBuilder();
//Set the code text of the barcode
bb.setCodeText("12345678");
//Set the symbology type to code39
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_39_EXTENDED);
//Set the wide to narrow ratio for the barcode
bb.setWideNarrowRatio(3.0f);
// Save the image to disk in PNG format
bb.save(dataDir + "wideNarrowRatio.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// Create an instance of Metered class
Metered matered = new Metered();
// Access the setMeteredKey property and pass public and private keys as parameters
matered.setMeteredKey("PublicKey", "PrivateKey");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeCaption.class) + "Barcode/BasicFeatures/";
//Instantiate Barcode object
BarCodeBuilder bb = new BarCodeBuilder();
//Set the code text of the barcode
bb.setCodeText("1234567");
//Set the symbology type to code128
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
Caption caption = new Caption();
caption.setText("Captions");
caption.setTextAlign(com.aspose.barcode.StringAlignment.Center);
bb.setCaptionAbove(caption);
bb.setCaptionBelow(caption);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
bb.getCaptionAbove().setTextAlign(com.aspose.barcode.StringAlignment.Far);
bb.getCaptionAbove().setText("Aspose.Demo");
bb.getCaptionAbove().setVisible(true);
bb.getCaptionAbove().setFont(new java.awt.Font("Pristina", Font.PLAIN, 14));
bb.getCaptionAbove().setForeColor(java.awt.Color.RED);
bb.getCaptionBelow().setTextAlign(com.aspose.barcode.StringAlignment.Near);
bb.getCaptionBelow().setText("Aspose.Demo");
bb.getCaptionBelow().setVisible(true);
bb.getCaptionBelow().setFont(new java.awt.Font("Pristina", Font.PLAIN, 14));
bb.getCaptionBelow().setForeColor(java.awt.Color.RED);
//Save the Barcode image to file
bb.save(dataDir + "barcodeCaption.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(CodeTextForegroundColor.class) + "Barcode/BasicFeatures/";
//Instantiate Barcode object
BarCodeBuilder bb = new BarCodeBuilder();
//Set up code text (data to be encoded)
bb.setCodeText("1234567");
//Set up code text color
bb.setCodeTextColor(Color.RED);
//Save the Barcode image to file
bb.save(dataDir + "codeTextForegroundColor.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(CodeTextLocation.class) + "Barcode/BasicFeatures/";
BarCodeBuilder builder = new BarCodeBuilder("GTIN:898978777776665655 " + "UID: 121212121212121212 " + "Batch:GH768 " + "Exp.Date:150923", com.aspose.barcode.EncodeTypes.DATA_MATRIX);
builder.setCodeLocation(CodeLocation.Right);
builder.setMargins(new MarginsF(0, 0, 0, 0));
builder.save(dataDir + "codetextRight.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(SetCodeText.class) + "Barcode/BasicFeatures/";
//Instantiate BarCodeBuilder object
BarCodeBuilder bb = new BarCodeBuilder();
//Set the code text for the barcode
bb.setCodeText("12345678");
//Set the symbology type to Code128
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
//Set the width of the bars to 0.5 millimeter
bb.setxDimension(0.5f);
//Save the barcode image to file
bb.save(dataDir + "setCodeText.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(SpecifySymbology.class) + "Barcode/BasicFeatures/";
// Generate and save the image to file
BarCodeBuilder builder = new BarCodeBuilder();
// Set code text
builder.setCodeText("test-123");
// Set Symbology type
builder.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_39_STANDARD);
builder.save(dataDir + "Code39Standard.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeImageBorders.class) + "BarcodeImage/BasicFeatures/";
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText("1234567");
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
//Set border style to solid
bb.setBorderDashStyle(BorderDashStyle.Solid);
//Set border margins by assigning an instance of MarginsF
bb.setMargins(new MarginsF(2f, 2f, 2f, 2f));
//Set border width
bb.setBorderWidth(0.5f);
//Set the border's color to red
bb.setBorderColor(java.awt.Color.RED);
//Enable border to be shown in the barcode
bb.setBorderVisible(true);
// Save the image
bb.save(dataDir + "barcode-image-borders.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeImageMargins.class) + "BarcodeImage/BasicFeatures/";
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText("1234567");
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
bb.getMargins().setLeft(2f);
bb.getMargins().setRight(2f);
bb.getMargins().setTop(2f);
bb.getMargins().setBottom(2f);
// Save the image
bb.save(dataDir + "barcode-image-margins.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeImageQuality.class) + "BarcodeImage/BasicFeatures/";
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText("1234567");
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
//Set the graphics drawing hint to be Anti Alias
bb.setImageQuality(ImageQualityMode.AntiAlias);
// Save the image
bb.save(dataDir + "barcode-image-quality.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(ColorizeBarcodeImage.class) + "BarcodeImage/BasicFeatures/";
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText("1234567");
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
//Set background color of the barcode
bb.setBackColor(Color.YELLOW);
//Set the foreground color of the barcode
bb.setForeColor(Color.BLUE);
//Set border's color
bb.setBorderColor(Color.RED);
//Set the code text's color
bb.setCodeTextColor(Color.RED);
//Caption's color
bb.getCaptionAbove().setForeColor(Color.darkGray);
bb.getCaptionBelow().setForeColor(Color.CYAN);
// Save the image
bb.save(dataDir + "colorizeBarcode.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
//Instantiate barcode object
BarCodeBuilder bb = new BarCodeBuilder();
//Border will be visible
bb.setBorderVisible(true);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(GenerateBarcodeWithEmptyBars.class) + "BarcodeImage/BasicFeatures/";
//Create an instance of BarCodeBuilder and initialize it with CodeText and Symbology
BarCodeBuilder builder = new BarCodeBuilder("TEXT", Symbology.Code128);
//Set the FilledBars property to false
builder.setFilledBars(false);
//Save the resultant barcode image on disk
builder.save(dataDir + "barcodeWithEmptyBars.png", BarCodeImageFormat.Png);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(RotateBarcodeImage.class) + "BarcodeImage/BasicFeatures/";
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText("1234567");
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_39_STANDARD);
//Rotate clockwise for 180 degree (upside down)
bb.setRotationAngleF(180);
// Save the image
bb.save(dataDir + "barcode-image-rotate.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
//Instantiate barcode object
BarCodeBuilder bb = new BarCodeBuilder();
//Set border's width to be 0.5 milimeter
bb.setBorderWidth(0.5f);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
public class RenderBarcodeToGraphicsObject {
public static void main(String[] args) {
// Create frame instance
Frame f = new Frame();
// Set frame size
f.setSize(300, 300);
// Create and add barcode instance to frame
f.add(new MyBarCode());
// Display frame
f.setVisible(true);
}
}
class MyBarCode extends java.awt.Canvas {
public void paint(Graphics g) {
// The path to the resource directory.
String dataDir = Utils.getDataDir(MyBarCode.class) + "BarcodeImage/RenderingFeatures/";
String fileName = dataDir + "barcode.png";
BarCodeBuilder bb = new BarCodeBuilder();
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
bb.setCodeText("12345678");
bb.save(fileName);
// Load and Draw the image on applet
MediaTracker tr = new MediaTracker(this);
File sourceimage = new File(fileName);
Image image;
try {
image = ImageIO.read(sourceimage);
tr.addImage(image, 0);
g.drawImage(image, 0, 0, this);
} catch (IOException e) {
e.printStackTrace();
}
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder bb = new BarCodeBuilder();
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
bb.setCodeText("12345678");
// Generate bar code image
Image image = bb.generateBarCodeImage();
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
public class RenderBarcodeToPrinter {
public static void main(String[] args) {
// Create frame instance
Frame f = new Frame();
// Set frame size
f.setSize(300, 300);
// Create and add barcode instance to frame
f.add(new RenderBarCode());
// Display frame
f.setVisible(true);
}
}
class RenderBarCode extends java.awt.Canvas {
public void paint(Graphics g) {
BarCodeBuilder bb = new BarCodeBuilder();
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
bb.setCodeText("12345678");
BufferedImage bimg = (BufferedImage) bb.generateBarCodeImage();
int w = bimg.getWidth();
int h = bimg.getHeight();
int[] rgb = new int[w * h];
bimg.getRGB(0, 0, w, h, rgb, 0, w);
if (rgb.length > 0) {
System.out.println("RGB OK.");
}
g.drawImage(bimg, 0, 0, this);
}
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
public class RenderBarcodeToServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
BarCodeBuilder b = new BarCodeBuilder();
b.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
b.setCodeText("12345678");
BufferedImage image = b.getBarCodeImage();
response.setContentType("image/png");
OutputStream outputStream = response.getOutputStream();
ImageIO.write(image, "png", outputStream);
outputStream.close();
}
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeCustomSize.class) + "BarcodeImage/UtilityFeatures/";
//Instantiate barcode object
BarCodeBuilder builder = new BarCodeBuilder();
//Set the Code text for the barcode
builder.setCodeText("1234567890");
//Set the symbology type to Code39Standard
builder.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_39_STANDARD);
// Set auto size false
builder.setAutoSize(false);
// Set height
builder.setImageHeight(50);
// Set width
builder.setImageWidth(120);
// Save the image
builder.save(dataDir + "barcode-custom-size.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeImageResolution.class) + "BarcodeImage/UtilityFeatures/";
//Instantiate barcode object
BarCodeBuilder bb = new BarCodeBuilder();
//Set the Code text for the barcode
bb.setCodeText("1234567");
//Set the symbology type to code128
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
//Create an instance of resolution and apply on the barcode image with customized resolution settings
bb.setResolution(new Resolution(200f, 400f, ResolutionMode.Customized));
// Save the image
bb.save(dataDir + "barcode-image-resolution.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeCustomSize.class) + "BarcodeImage/UtilityFeatures/";
// Create an instance of BarCodeBuilder class
// Set barcode text
// Set encoding type
com.aspose.barcode.BarCodeBuilder builder =
new com.aspose.barcode.BarCodeBuilder("1234567890", com.aspose.barcode.EncodeTypes.CODE_128);
// Set graphic unit
builder.setGraphicsUnit(com.aspose.barcode.GraphicsUnit.Pixel);
// Call GetMinimumBarCodeSize method to get the minimum size required
java.awt.geom.Dimension2D minSize = builder.getMinimumBarCodeSize();
// Set Auto size to false
builder.setAutoSize(false);
// Set image height & width with the help of min size got from GetMinimumBarCodeSize method
builder.setImageWidth((float) minSize.getWidth());
builder.setImageHeight((float)minSize.getHeight());
// Save the barcode image
javax.imageio.ImageIO.write( builder.getBarCodeImage(), "PNG", new java.io.File("minimumresult.png") );
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// Create instance of BarCodeBuilder, specify codetext and symbology in the constructor
BarCodeBuilder builder = new BarCodeBuilder("12345678", Symbology.Code128);
// Set printer name
builder.setPrinterName("ML-1640 Series");
// start a print job
builder.print();
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(SaveBarcodeImagesToDifferentFormats.class) + "BarcodeImage/UtilityFeatures/";
//Instantiate barcode object
BarCodeBuilder bb = new BarCodeBuilder();
//Set the Code text for the barcode
bb.setCodeText("1234567");
//Set the symbology type to code128
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
//Save the image to your system and set its image format to Jpeg
bb.save(dataDir + "barcode-image-format.jpg", ImageFormat.getJpeg());
//Save the image to your system and set its image format to PNG
bb.save(dataDir + "barcode-image-format.png", ImageFormat.getPng());
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder builder = new BarCodeBuilder();
builder.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
builder.setCodeText("123456");
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
builder.save(outStream, BarCodeImageFormat.Jpeg);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(SetSizeUnitForBarcodeImage.class) + "BarcodeImage/UtilityFeatures/";
//Instantiate barcode object
BarCodeBuilder bb = new BarCodeBuilder();
//Set the Code text for the barcode
bb.setCodeText("1234567");
//Set the symbology type to code128
bb.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
//Set the bar height to 3 points
bb.setBarHeight(3.0f);
//Set the measuring unit of barcode to point
bb.setGraphicsUnit(GraphicsUnit.Point);
// Save the image
bb.save(dataDir + "barcode-size-unit.jpg");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeOrientation.class) + "BarcodeReader/advanced_features/";
// Read code39 barcode from image
String image = dataDir + "code39Extended.jpg";
BarCodeReader reader = new BarCodeReader(image, com.aspose.barcode.barcoderecognition.DecodeType.CODE_39_STANDARD);
// Set orientation
reader.setOrientationHints(Orientation.Rotate90);
// Try to recognize all possible barcodes in the image
while (reader.read()) {
System.err.println("Codetext: " + reader.getCodeText());
}
// Close reader
reader.close();
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeRegionInformationFromTheImage.class) + "BarcodeReader/advanced_features/";
// Read code39 barcode from image
String imageFilePath = dataDir + "code39Extended.jpg";
BarCodeReader reader = new BarCodeReader(imageFilePath, com.aspose.barcode.barcoderecognition.DecodeType.CODE_39_STANDARD);
// Try to recognize all possible barcodes in the image
while (reader.read()) {
// Get the region information
BarCodeRegion region = reader.getRegion();
if (region != null) {
// Display x and y coordinates of barcode detected
Point[] point = region.getPoints();
System.out.println("Top left coordinates: X = " + point[0].x + ", Y = " + point[0].y);
System.out.println("Bottom left coordinates: X = " + point[1].x + ", Y = " + point[1].y);
System.out.println("Bottom right coordinates: X = " + point[2].x + ", Y = " + point[2].y);
System.out.println("Top right coordinates: X = " + point[3].x + ", Y = " + point[3].y);
}
System.out.println("Codetext: " + reader.getCodeText());
}
// Close the reader
reader.close();
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(FasterImageProcessingForBarcodeRecognition.class) + "BarcodeReader/advanced_features/";
// Read code39 barcode from image
String imageFilePath = dataDir + "datamatrix.bmp";
//Create an instance of BarCodeReader and set image and symbology type to recognize
BarCodeReader reader = new BarCodeReader(imageFilePath, com.aspose.barcode.barcoderecognition.DecodeType.DATA_MATRIX);
//Set recognition hints
reader.setImageBinarizationHints(RecognitionHints.ImageBinarization.MedianSmoothing);
reader.setMedianSmoothingWindowSize(4);
//Try to recognize the barcode from the image
while (reader.read())
{
//Display the CodeText
System.out.println("Codetext: " + reader.getCodeText());
}
// Close the reader
reader.close();
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(GenerateMultipleBarcodesOnASingleImage.class) + "BarcodeReader/advanced_features/";
collection.put("ONE123", com.aspose.barcode.EncodeTypes.CODE_39_STANDARD);
collection.put("Process Collection", com.aspose.barcode.EncodeTypes.DATA_MATRIX);
collection.put("Dictionary Collection", com.aspose.barcode.EncodeTypes.QR);
collection.put("X06712AT", com.aspose.barcode.EncodeTypes.CODE_128);
collection.put("979026000043", com.aspose.barcode.EncodeTypes.EAN_13);
collection.put("Aztec BarCode", com.aspose.barcode.EncodeTypes.AZTEC);
ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
for (Object key : collection.keySet()) {
BarCodeBuilder bb = new BarCodeBuilder();
bb.setCodeText((String) key);
bb.setEncodeType((Long) collection.get(key));
images.add(bb.generateBarCodeImage());
}
int maxWidth = 0;
int sumHeight = 0;
for (BufferedImage bmp : images)
{
sumHeight += bmp.getHeight();
if (maxWidth < bmp.getWidth())
maxWidth = bmp.getWidth();
}
int offset = 10;
BufferedImage resultBitmap = new BufferedImage(maxWidth + offset * 2, sumHeight + offset * images.size(), BufferedImage.TYPE_INT_ARGB);
Graphics g = resultBitmap.getGraphics();
g.setColor(Color.white);
//g.drawRect(0, 0, width, height);
g.fillRect(0, 0, resultBitmap.getWidth(), resultBitmap.getHeight());
int yPosition = offset;
for (int i = 0; i < images.size(); ++i)
{
BufferedImage currentBitmap = images.get(i);
g.drawImage(currentBitmap, offset, yPosition, null);
yPosition += currentBitmap.getHeight() + offset;
}
File outputfile = new File(dataDir + "output.png");
ImageIO.write(resultBitmap, "png", outputfile);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(GetAllPossible1DBarcodesFromAnImage.class) + "BarcodeReader/advanced_features/";
// Initialize the BarCodeReader object
BarCodeReader reader = new BarCodeReader(dataDir + "MultipleBarcodes.png", com.aspose.barcode.barcoderecognition.DecodeType.CODE_128);
// Call read method
reader.read();
// Now get all possible barcodes
int iCount = 0;
while (reader.read())
{
// Display code text, symbology, detected angle, recognition percentage of the barcode
System.out.println("Code Text: " + reader.getCodeText() + " Symbology: " + reader.getCodeType() + " Recognition percentage: " + reader.getAngle());
// Display x and y coordinates of barcode detected
Point[] point = reader.getRegion().getPoints();
System.out.println("Top left coordinates: X = " + point[0].getX() + ", Y = " + point[0].getY());
System.out.println("Bottom left coordinates: X = " + point[1].getX() + ", Y = " + point[1].getY());
System.out.println("Bottom right coordinates: X = " + point[2].getX() + ", Y = " + point[2].getY());
System.out.println("Top right coordinates: X = " + point[3].getX() + ", Y = " + point[3].getY());
iCount = iCount + 1;
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(GetBarCodeRecognitionQualityInPercent.class) + "BarcodeReader/advanced_features/";
// Initialize the BarCodeReader object
BarCodeReader reader = new BarCodeReader(dataDir + "code39Extended.jpg", com.aspose.barcode.barcoderecognition.DecodeType.ALL_SUPPORTED_TYPES);
// Call read method
while (reader.read()) {
System.out.println(reader.getCodeText() + " Type: " + reader.getCodeType());
float percent = reader.getRecognitionQuality();
System.out.println("Percent: " + percent);
}
reader.close();
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(MarkingBarcodeRegionsInAnImage.class) + "BarcodeReader/advanced_features/";
// Create an instance of BarCodeReader class and specify the image and symbology
BarCodeReader reader = new BarCodeReader(dataDir + "Code39Std.png", com.aspose.barcode.barcoderecognition.DecodeType.CODE_39_STANDARD);
// Read all the barcodes from the images
while (reader.read()) {
// Display the symbology type
System.out.println("BarCode Type: " + reader.getCodeType());
// Display the codetext
System.out.println("BarCode CodeText: " + reader.getCodeText());
// Get the barcode region
BarCodeRegion region = reader.getRegion();
if (region != null) {
// Initialize an object of type BufferedImage to get the Graphics object
BufferedImage bufferedImage = ImageIO.read(new File(dataDir + "Code39Std.png"));
// Initialize graphics object from the image
Graphics g = bufferedImage.getGraphics();
// Initialize paint object
Paint p = new GradientPaint(0, 0, Color.red, 100, 100, Color.pink, true);
region.drawBarCodeEdges(g, Color.RED);
// Save the image
ImageIO.write(bufferedImage, "png", new File(dataDir + "Code39StdOut.png"));
}
}
reader.close();
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(ReadBarcodeFromSpecificRegionOfImage.class) + "BarcodeReader/advanced_features/";
// Open the stream. Read only access is enough for Aspose.BarCode to load an image.
InputStream stream = new FileInputStream(dataDir + "specificRegion.png");
// Create an instance of BarCodeReader class
// and specify an area to look for the barcode
BarCodeReader reader = new BarCodeReader(stream, new Rectangle(0, 0, 100, 50), com.aspose.barcode.barcoderecognition.DecodeType.PDF_417);
// Read all barcodes in the provided area
while (reader.read()) {
// Display the codetext and symbology type of the barcode found
System.out.println("Codetext: " + reader.getCodeText() + " Symbology: " + reader.getCodeType());
}
// Close the reader
reader.close();
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
public class ReadMultipleMacropdf417BarcodeImages {
public static void main(String[] args) throws Exception {
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(ReadMultipleMacropdf417BarcodeImages.class) + "BarcodeReader/advanced_features/";
String strFileID = "1";
File dir = new File(dataDir);
File[] files = dir.listFiles(new Filter(strFileID));
for (int nCount = 1 ; nCount <= files.length ; nCount++) {
// We got list of all the files, now read barcodes
BarCodeReader reader = new BarCodeReader(files[nCount-1].getAbsolutePath(), com.aspose.barcode.barcoderecognition.DecodeType.MACRO_PDF_417);
if (reader.read() == true) {
System.out.println("File: " + files[nCount-1].getAbsolutePath() + " == FileID: " + reader.getMacroPdf417FileID() +
" == SegmentID: " + reader.getMacroPdf417SegmentID() +
" == CodeText: " + reader.getCodeText());
}
reader.close();
}
}
}
//Create new class for filtering the files list
class Filter implements FileFilter {
String fileID = "";
public Filter(String fileID) {
this.fileID = fileID;
}
@Override
public boolean accept(File file) {
// File names are in the format of FileID_*.png
return file.getName().endsWith("png") && file.getName().startsWith(fileID + "_");
}
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(SwitchBarcodeRecognitionModes.class) + "BarcodeReader/advanced_features/";
// Create an instance of BarCodeReader and set image and symbology type to recognize
BarCodeReader reader = new BarCodeReader(dataDir + "code39Extended.jpg", com.aspose.barcode.barcoderecognition.DecodeType.DATA_MATRIX);
// Set recognition mode
reader.setRecognitionMode(RecognitionMode.ManualHints);
// Set manual hints
reader.setManualHints(ManualHint.InvertImage | ManualHint.IncorrectBarcodes);
// Try to recognize the barcode from the image
while (reader.read()) {
// Display the CodeText
System.out.println("Codetext: " + reader.getCodeText());
}
// Close the reader
reader.close();
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(UsingManualHintsToOptimizeScan.class) + "BarcodeReader/advanced_features/";
long s = System.currentTimeMillis();
String filename = dataDir + "datamatrix.bmp";
BarCodeReader reader = new BarCodeReader(filename, com.aspose.barcode.barcoderecognition.DecodeType.GS1_DATA_MATRIX);
System.out.println("Skip rotated barcodes");
reader.setRecognitionMode(RecognitionMode.ManualHints);
reader.setManualHints(ManualHint.SkipRotatedBarcodes);
while (reader.read()) {
System.out.println(reader.getReadTypeName() + ": " + reader.getCodeText() + " QA:" + reader.getRecognitionQuality());
}
long res1 = System.currentTimeMillis() - s;
System.out.println(res1);
System.out.println();
s = System.currentTimeMillis();
reader = new BarCodeReader(filename, com.aspose.barcode.barcoderecognition.DecodeType.GS1_DATA_MATRIX);
System.out.println("Not skip rotated barcodes");
while (reader.read()) {
System.out.println(reader.getCodeType() + ": " + reader.getCodeText() + " QA:" + reader.getRecognitionQuality());
}
long res2 = System.currentTimeMillis() - s;
System.out.println(res2);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(Barcode_Recognition.class) + "BarcodeReader/basic_features/";
//Initialize barcode reader
BarCodeReader rd = new BarCodeReader(dataDir + "CodeText.jpg");
// read barcode of type Code39Extended
while(rd.read())
{
// print the code text, if barcode found
System.out.println("CodeText: " + rd.getCodeText().toString());
// print the symbology type
System.out.println("Symbology type: " + rd.getCodeType());
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(RecognizingMultipleSymbologiesInSingleImage.class) + "BarcodeReader/basic_features/";
BufferedImage img = ImageIO.read(new File(dataDir + "MultipleBarcodes.png"));
// Initialize barcode reader
BarCodeReader rd = new BarCodeReader(img, com.aspose.barcode.barcoderecognition.DecodeType.ALL_SUPPORTED_TYPES);
// Read all types of barcode
while (rd.read()) {
// Print the code text, if barcode found
System.out.println("CodeText: " + rd.getCodeText().toString());
// Print the symbology type
System.out.println("CodeText: " + rd.getReadType());
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(SpecificBarcodeSymbology.class) + "BarcodeReader/basic_features/";
BufferedImage img = ImageIO.read(new File(dataDir + "CodeText.jpg"));
// Initialize barcode reader
BarCodeReader rd = new BarCodeReader(img, com.aspose.barcode.barcoderecognition.DecodeType.CODE_128);
// Read barcode of type Code39Extended
while (rd.read()) {
// Print the code text, if barcode found
System.out.println("CodeText: " + rd.getCodeText().toString());
// Print the symbology type
System.out.println("CodeText: " + rd.getCodeType());
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(RecognizingMultipleSymbologiesInSingleImage.class) + "BarcodeReader/basic_features/";
BufferedImage img = ImageIO.read(new File(dataDir + "MultipleBarcodes.png"));
// Initialize barcode reader
BarCodeReader rd = new BarCodeReader(img, com.aspose.barcode.barcoderecognition.DecodeType.CODE_39_EXTENDED);
while (rd.read()) {
// Print the code text, if barcode found
System.out.println("CodeText: " + rd.getCodeText().toString());
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(LoadingFromAFile.class) + "BarcodeReader/loading_images/";
// Initialize bar code reader
BarCodeReader reader = new BarCodeReader(dataDir + "CodeText.jpg");
//OR
// Initialize bar code reader
BarCodeReader barcodeReader = new BarCodeReader(dataDir + "CodeText.jpg", com.aspose.barcode.barcoderecognition.DecodeType.CODE_39_EXTENDED);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(LoadingFromAStream.class) + "BarcodeReader/loading_images/";
// Open the stream. Read only access is enough for Aspose.BarCode to load an image.
InputStream stream = new FileInputStream(dataDir + "CodeText.jpg");
// Create an instance of BarCodeReader class and pass InputStream object as a parameter
BarCodeReader reader = new BarCodeReader(stream);
//OR
// Open the stream. Read only access is enough for Aspose.BarCode to load an image.
InputStream stream1 = new FileInputStream(dataDir + "CodeText.jpg");
// Create an instance of BarCodeReader class and pass InputStream object and bar code symbology type as parameters
BarCodeReader reader1 = new BarCodeReader(stream, com.aspose.barcode.barcoderecognition.DecodeType.CODE_39_EXTENDED);
//OR
// Open the stream. Read only access is enough for Aspose.BarCode to load an image.
InputStream stream2 = new FileInputStream(dataDir +"CodeText.jpg");
// Create an instance of BarCodeReader class and pass InputStream object, Rectangle object and bar code symbology type as parameters
BarCodeReader reader2 = new BarCodeReader(stream, new Rectangle(0, 0, 100, 50), com.aspose.barcode.barcoderecognition.DecodeType.CODE_39_EXTENDED);
//OR
// Load the image. Read only access is enough for Aspose.BarCode to load an image.
BufferedImage img3 = ImageIO.read(new File(dataDir + "CodeText.jpg"));
// Create an instance of BarCodeReader class and pass BufferedImage object as parameter
BarCodeReader reader3 = new BarCodeReader(img3);
//OR
// Load the image. Read only access is enough for Aspose.BarCode to load an image.
BufferedImage img4 = ImageIO.read(new File(dataDir + "CodeText.jpg"));
// Create an instance of BarCodeReader class and pass BufferedImage object and bar code symbology type as parameters
BarCodeReader reader4 = new BarCodeReader(img4, com.aspose.barcode.barcoderecognition.DecodeType.PDF_417);
//OR
// Load the image. Read only access is enough for Aspose.BarCode to load an image.
BufferedImage img5 = ImageIO.read(new File(dataDir + "CodeText.jpg"));
// Create an instance of BarCodeReader class and pass BufferedImage object, Rectangle object and bar code symbology type as parameters
BarCodeReader reader5 = new BarCodeReader(img5, new Rectangle(0, 0, 100, 50), com.aspose.barcode.barcoderecognition.DecodeType.CODE_39_EXTENDED);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.QR, "Aspose.BarCode sample");
generator.getXDimension().setPixels(3);
generator.getMargins().getLeft().setPixels(20);
generator.getMargins().getRight().setPixels(20);
generator.save(dataDir + "GenerateBarcodeWithoutRestrictedBarcodeSize_out.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.QR, "Aspose.BarCode sample");
generator.setAutoSizeMode(AutoSizeMode.Nearest);
generator.getBarCodeWidth().setPixels(200);
generator.getBarCodeHeight().setPixels(200);
generator.save(dataDir + "GenerateBarcodeWithRestrictedBarcodeSize_out.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.CODE_16_K);
generator.setAutoSizeMode(AutoSizeMode.Nearest);
generator.getBarCodeWidth().setPixels(100);
generator.getCodeTextStyle().setLocation(CodeLocation.None);
generator.save("Code16K_Nearest.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.QR, "Aspose.BarCode sample");
generator.getXDimension().setPixels(3);
generator.getD2().setAspectRatio(1.5f);
generator.recalculateValues();
System.out.println("Width = " + generator.getBarCodeWidth().getPixels());
System.out.println("Height = " + generator.getBarCodeWidth().getPixels());
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.AUSTRALIAN_POSTE_PARCEL);
String codetext = generator.getCodeText(); //99712345678901234567890103456
generator = new BarCodeGenerator(EncodeTypes.EAN_13);
codetext = generator.getCodeText(); //590123412345
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.QR);
generator.getQR().setVersion(QRVersion.VERSION_18);
generator.getQR().setErrorLevel(QRErrorLevel.LevelM);
generator.save(dataDir + "GroupingPropertiesByBarcodeType_out.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
String CODICE = "(90)0843110730<<<<452287005001T8";
String displayedText = "(90)0843" + "\r\n" +
"110730<<<<" + "\r\n" +
"452287" + "\r\n" +
"005001T8" + "\r\n";
BarCodeBuilder builder = new BarCodeBuilder(CODICE, EncodeTypes.GS_1_DATA_MATRIX);
builder.setCodeLocation(CodeLocation.Right);
builder.setDisplay2DText(displayedText);
BufferedImage bitmap = builder.generateBarCodeImage();
File imageFile = new File("Display2DText.png");
ImageIO.write(bitmap, "png", imageFile);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.DOT_CODE);
generator.setDotCodeMask(2);
BufferedImage bitmap = generator.generateBarCodeImage();
File imageFile = new File("DotCode.png");
ImageIO.write(bitmap, "png", imageFile);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.DATA_MATRIX);
generator.setAutoSizeMode(AutoSizeMode.Interpolation);
generator.getBarCodeWidth().setMillimeters(50);
generator.getBarCodeHeight().setInches(1.3f);
BufferedImage bitmap = generator.generateBarCodeImage();
File imageFile = new File("DataMatrix.png");
ImageIO.write(bitmap, "png", imageFile);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.MAXI_CODE);
generator.setMaxiCodeEncodeMode(5);
BufferedImage bitmap = generator.generateBarCodeImage();
File imageFile = new File("MaxiCodeEncodeMode.png");
ImageIO.write(bitmap, "png", imageFile);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.UPCA_GS_1_DATABAR_COUPON);
generator.save("UpcaGs1DatabarCoupon.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeGenerator generator = new BarCodeGenerator(EncodeTypes.QR, "Aspose.BarCode sample");
generator.setAutoSizeMode (AutoSizeMode.Nearest);
generator.getBarCodeWidth().setMillimeters(20);
generator.getBarCodeHeight().setMillimeters(20);
generator.save(dataDir + "for_display_out.png");
generator.setResolution(300);
generator.save(dataDir + "for_printer_out.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(SpecificBarcodeSymbology.class) + "BarcodeReader/basic_features/";
// Create an instance of BarCodeBuilder class
// Set the barcode text
// Set the barcode symbology
com.aspose.barcode.BarCodeBuilder builder = new com.aspose.barcode.BarCodeBuilder("123456789012", com.aspose.barcode.EncodeTypes.EAN_13);
// Generate Barcode image and store it
java.awt.image.BufferedImage barcode = builder.generateBarCodeImage();
// Load the logo/other image from disk
java.awt.image.BufferedImage picture = javax.imageio.ImageIO.read(new java.io.File("path_to_your_picture.png"));
// Create a new empty image with new Calculated height &amp; width
java.awt.image.BufferedImage output = new java.awt.image.BufferedImage(
Math.max(barcode.getWidth(), picture.getWidth()), barcode.getHeight() + picture.getHeight(),
java.awt.image.BufferedImage.TYPE_INT_ARGB);
// Get the Graphics object
java.awt.Graphics g = output.getGraphics();
// Set the canvas color
g.setColor(java.awt.Color.WHITE);
// Draw the primary image (barcode image) on the canvas
g.drawImage(picture, 0, 0, null);
// Draw the second image (logo image) on the canvas inside the barcode image
g.drawImage(barcode, 0, picture.getHeight(), null);
// Save the final barcode image
java.io.File imageFile = new java.io.File("output.png");
javax.imageio.ImageIO.write(output, "PNG", imageFile);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder objBuilder = new BarCodeBuilder();
objBuilder.setEncodeType(com.aspose.barcode.EncodeTypes.DATA_MATRIX);
objBuilder.setCodeText("1234567890");
objBuilder.save(dataDir + "datamatrix.bmp");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// Create an instance of BarCodeBuilder class
// Set codetext value and EncodeType
com.aspose.barcode.BarCodeBuilder buidler = new com.aspose.barcode.BarCodeBuilder("ABCDEF123456", com.aspose.barcode.EncodeTypes.DATA_MATRIX);
// Set the DataMatrix encoding mode to C40
buidler.setDataMatrixEncodeMode(com.aspose.barcode.DataMatrixEncodeMode.C40);
// Save the barcode image
buidler.save("dataMatrixC40.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder objBuilder = new BarCodeBuilder();
objBuilder.setEncodeType(com.aspose.barcode.EncodeTypes.DATA_MATRIX);
objBuilder.setDataMatrixEncodeMode(DataMatrixEncodeMode.Custom);
objBuilder.setCodeTextEncoding(Charset.forName("UTF-8"));
objBuilder.setCodeText("öäüéГ ГÐ?");
objBuilder.save(dataDir + "output_Utf8.bmp", BarCodeImageFormat.Bmp);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder objBuilder = new BarCodeBuilder();
objBuilder.setEncodeType(com.aspose.barcode.EncodeTypes.DATA_MATRIX);
objBuilder.setDataMatrixEncodeMode(DataMatrixEncodeMode.ASCII);
objBuilder.setCodeText("This is the data to be encoded");
objBuilder.save(dataDir + "encodeModeForDataMatrix.bmp");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b = new BarCodeBuilder();
bb.setEncodeType(com.aspose.barcode.EncodeTypes.AZTEC);
b.setCodeText("1234567890");
b.save(dataDir + "aztec.bmp", BarCodeImageFormat.Bmp);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b = new BarCodeBuilder();
bb.setEncodeType(com.aspose.barcode.EncodeTypes.AZTEC);
b.setAztectErrorLevel(50);
b.setCodeText("1234567890");
b.save(dataDir + "error_correction_aztec.bmp", BarCodeImageFormat.Bmp);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b = new BarCodeBuilder();
b.setEncodeType(com.aspose.barcode.EncodeTypes.PDF_417);
b.setCodeText("1234567890");
b.save(dataDir + "pdf417.bmp");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder builder = new BarCodeBuilder("This is text data", com.aspose.barcode.EncodeTypes.PDF_417);
// Set Pdf417 Compaction Mode to Text
builder.setPdf417CompactionMode(Pdf417CompactionMode.Text);
// Save the image to disk in PNG format
builder.save(dataDir + "pdf417CompactionMode.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b = new BarCodeBuilder();
b.setEncodeType(com.aspose.barcode.EncodeTypes.PDF_417);
b.setPdf417ErrorLevel(Pdf417ErrorLevel.Level8);
b.setCodeText("12345");
b.save(dataDir + "pdf417ErrorCorrectionLevel.bmp");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b = new BarCodeBuilder();
b.setEncodeType(com.aspose.barcode.EncodeTypes.QR);
b.setCodeText("1234567890");
b.save(dataDir + "QRBarcode.bmp", BarCodeImageFormat.Bmp);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b = new BarCodeBuilder();
b.setEncodeType(com.aspose.barcode.EncodeTypes.QR);
b.setQRErrorLevel(QRErrorLevel.LevelH);
b.setCodeText("1234567890");
b.save(dataDir + "errorCorrectionQRBarcode.bmp", BarCodeImageFormat.Bmp);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// Define barcode image height & width
int QRCODE_IMAGE_HEIGHT = 300;
int QRCODE_IMAGE_WIDTH = 300;
// Create an instance of BarCodeBuilder class
// Set the barcode text
// Set the barcode symbology
BarCodeBuilder builder = new BarCodeBuilder("123456789", com.aspose.barcode.EncodeTypes.QR);
// Set the error level
builder.setQRErrorLevel(com.aspose.barcode.QRErrorLevel.LevelH);
// Set the Graphics Unit
builder.setGraphicsUnit(2);
// Generate the barocde image and save it as image in an object of BufferedImage class
java.awt.image.BufferedImage image = builder.getCustomSizeBarCodeImage(QRCODE_IMAGE_WIDTH, QRCODE_IMAGE_HEIGHT, false);
System.out.println("ImageHeight: " + image.getHeight());
// Load the image in an object of BufferedImage class - this is the image that you want to embed into the barcode image.
java.awt.image.BufferedImage overlay = javax.imageio.ImageIO.read(new java.io.File("wifi_logo.jpg"));
// Calculate the height & width
int deltaHeight = image.getHeight() - overlay.getHeight();
int deltaWidth = image.getWidth() - overlay.getWidth();
// Create a new empty image
java.awt.image.BufferedImage combined = new java.awt.image.BufferedImage(QRCODE_IMAGE_WIDTH, QRCODE_IMAGE_HEIGHT,
java.awt.image.BufferedImage.TYPE_INT_ARGB);
// Get the Graphics2D object
java.awt.Graphics2D g = (java.awt.Graphics2D)combined.getGraphics();
// Draw the primary image (barcode image) on the canvas
g.drawImage(image, 0, 0, null);
g.setComposite(java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 1f));
// Draw the second image (logo image) on the canvas inside the barcode image
g.drawImage(overlay, (int)Math.round(deltaWidth/2), (int)Math.round(deltaHeight/2), null);
// Create and save the final very of the image with barcode and logo inside it
java.io.File imageFile = new java.io.File("qrcode_with_logo.png");
javax.imageio.ImageIO.write(combined, "PNG", imageFile);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b = new BarCodeBuilder();
b.setEncodeType(com.aspose.barcode.EncodeTypes.CODE_128);
b.setCodeText("1234567890");
//Hide code text
b.setCodeLocation(CodeLocation.None);
b.setRotationAngleF(90);
b.save(dataDir + "rotation_qr.bmp", BarCodeImageFormat.Bmp);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b = new BarCodeBuilder();
b.setEncodeType(com.aspose.barcode.EncodeTypes.DATA_MATRIX);
// Width of each module
b.setxDimension(0.6f);
b.setCodeText("123456789");
b.save(dataDir + "datamatrix.bmp");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b= new BarCodeBuilder();
b.setEncodeType(com.aspose.barcode.EncodeTypes.PDF_417);
// Width of each module
b.setxDimension(0.6f);
// Height of each module
b.setyDimension(1.2f);
b.setCodeText("this is some test code text. \n Second line \n third line.");
b.save(dataDir + "pdf417.bmp");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
ApplyALicense.applyALicense();
// The path to the resource directory.
String dataDir = Utils.getDataDir(DetectTheUnicodeEncoding.class) + "2DBarcode/BasicFeatures/";
String imageFilePath = dataDir + "unicodeEncoding.png";
BarCodeBuilder builder = new BarCodeBuilder();
builder.setCodeText("Слово");
builder.setEncodeType(com.aspose.barcode.EncodeTypes.QR);
builder.setCodeTextEncoding(StandardCharsets.UTF_8);
builder.save(imageFilePath, BarCodeImageFormat.Png);
BarCodeReader reader = new BarCodeReader(imageFilePath, com.aspose.barcode.barcoderecognition.DecodeType.QR);
reader.setDetectEncoding(true);
if (reader.read()) {
System.out.println(reader.getCodeText()); //"Слово"
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(GenerateAztecBarcode.class) + "TwoD_barcodes/BasicFeatures/";
// Initialize BarCode builder class object
BarCodeBuilder builder = new BarCodeBuilder();
// Set the barcode text
builder.setCodeText("25");
// Set symbology type as Aztec
builder.setEncodeType(com.aspose.barcode.EncodeTypes.AZTEC);
// save barcode
builder.save(dataDir + "Aztec.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder objBuilder = new BarCodeBuilder("1234676", com.aspose.barcode.EncodeTypes.DATA_MATRIX);
objBuilder.save("temp.png", com.aspose.barcode.BarCodeImageFormat.Png);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(SetAztecSymbolMode.class) + "TwoD_barcodes/BasicFeatures/";
// Create an instance of BarCodeBuilder class.
BarCodeBuilder b = new BarCodeBuilder();
// Set the Code text.
b.setCodeText("25");
// Set the barcode type.
b.setEncodeType(com.aspose.barcode.EncodeTypes.AZTEC);
// set the AztecSymbolMode property.
b.setAztecSymbolMode(AztecSymbolMode.Rune);
// Save the barcode as PNG image.
b.save(dataDir + "testRune25.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
// Instantiate BarCodeBuilder object
com.aspose.barcode.BarCodeBuilder builder = new com.aspose.barcode.BarCodeBuilder();
// Set the Code text for the barcode
builder.setCodeText("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
// Set the symbology type to QR
builder.setEncodeType(com.aspose.barcode.EncodeTypes.QR);
// Set the error level
builder.setQRErrorLevel(com.aspose.barcode.QRErrorLevel.LevelQ);
// Set the QR barcode version number
builder.setQRVersion(com.aspose.barcode.QRVersion.VERSION_10);
//Save the image
builder.save("qr_version10_errorQ.png");
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
String dataDir = Utils.getDataDir(GenerateMultipleMacroPdf417.class) + "2DBarcode/UtilityFeatures/";
BarCodeBuilder builder = new BarCodeBuilder();
builder.setEncodeType(com.aspose.barcode.EncodeTypes.MACRO_PDF_417);
// Create array for storing multiple barcodes
int nSize = 4;
String[] lstCodeText = new String[] {"code-1", "code-2", "code-3", "code-last"};
String strFileID = "1";
// Check the listbox for getting codetext and generating the barcodes
for (int nCount = 1; nCount <= nSize; nCount++) {
builder.setCodeText(lstCodeText[nCount - 1]);
// fileID should be same for all the generated barcodes
builder.setMacroPdf417FileID(Integer.parseInt(strFileID));
// Assign segmentID in increasing order (1,2,3,....)
builder.setMacroPdf417SegmentID(nCount);
// Save the barcode (fileid_segmentid.png)
builder.save(dataDir + strFileID + "_" + nCount + ".png");
}
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b = new BarCodeBuilder();
b.setEncodeType(com.aspose.barcode.EncodeTypes.DATA_MATRIX);
b.setCodeText("The quick brown fox jumps over the lazy dog\n The quick brown fox jumps over the lazy dog\n");
b.setCodeLocation(CodeLocation.Below);
b.save(dataDir + "datamatrix.bmp", BarCodeImageFormat.Bmp);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
BarCodeBuilder b = new BarCodeBuilder();
b.setEncodeType(com.aspose.barcode.EncodeTypes.DATA_MATRIX);
b.setCodeText("The quick brown fox jumps over the lazy dog\n The quick brown fox jumps over the lazy dog\n");
b.setCodeLocation(CodeLocation.None);
b.setCodeTextFont(new Font("Serif", Font.BOLD + Font.ITALIC, 20));
b.save(dataDir + "reduceFontSize.bmp", BarCodeImageFormat.Bmp);
// For complete examples and data files, please go to https://github.com/aspose-barcode/Aspose.BarCode-for-Java
String dataDir = Utils.getDataDir(SetAspectRatioOfBarcodes.class) + "2DBarcode/UtilityFeatures/";
// Create instance of BarCodeBuilder class
BarCodeBuilder builder = new BarCodeBuilder("1234567890", com.aspose.barcode.EncodeTypes.PDF_417);
// Set Aspect Ratio to 3:2 or 1.5
builder.setAspectRatio(1.5f);
// Save the barcode image to disk in PNG format
builder.save(dataDir + "barcode_aspect_ratio.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment