Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Aspose.BarCode for Java

This Gist contains code snippets for sample code of Aspose.BarCode for Java.

Gists for Aspose.BarCode for Java
// The path to the resource directory.
String dataDir = Utils.getDataDir(ApplyALicense.class) + "License/";
// Apply a license to avoid the evaluation WaterMark in the BarCode image.
License license = new License();
license.setLicense(dataDir + "Aspose.Total.Java.lic");
// Apply a license to avoid the evaluation WaterMark in the BarCode image.
License license = new License();
license.setLicense(new java.io.FileInputStream("Aspose.BarCode.Java.lic"));
// set metered public and private keys
com.aspose.barcode.metered.Metered objMeter = new com.aspose.barcode.metered.Metered();
// Access the setMeteredKey property and pass public and private keys as parameters
objMeter.setMeteredKey("PublicKey", "PrivateKey");
// DO PROCESSING
// get metered data amount
double amount = com.aspose.barcode.metered.Metered.getConsumptionQuantity();
// Display information
System.out.println("Amount consumed: " + amount);
// The path to the resource directory.
String dataDir = Utils.getDataDir(Checksum.class) + "Barcode/AdvancedFeatures/";
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_128, "12345");
generator.getParameters().getBarcode().setChecksumAlwaysShow(true);
// Save the Barcode image to file
generator.save(dataDir + "checksum.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(ApplyingChecksumValidation.class) + "Barcode/AdvancedFeatures/";
// Generation
// Instantiate BarcodeGenerator object
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODABAR, "1234567890");
// Set the EnableChecksum property to yes
generator.getParameters().getBarcode().setChecksumEnabled(EnableChecksum.YES);
// Set the CodabarChecksumMode
generator.getParameters().getBarcode().getCodabar().setCodabarChecksumMode(CodabarChecksumMode.MOD_10);
// Save the image on the system
generator.save(dataDir + "Codabar_Mod10.png");
// Recognition
// Initialize reader object
BarCodeReader reader = new BarCodeReader(dataDir + "Codabar_Mod10.png",
com.aspose.barcode.barcoderecognition.DecodeType.CODABAR);
// Set ChecksumValidation property of the reader to On
reader.setChecksumValidation(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());
}
// 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", 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());
}
// The path to the resource directory.
String dataDir = Utils.getDataDir(SupplementData.class) + "Barcode/AdvancedFeatures/";
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.DATABAR_EXPANDED_STACKED,
"(01)98898765432106(3202)012345(15)991231");
// Set the column property to define segments per row
generator.getParameters().getBarcode().getDataBar().setColumns(6);
// Save the image
generator.save(dataDir + "6segmets.png");
// The path to the resource directory.
String dataDir = Utils.getDataDir(Checksum.class) + "Barcode/AdvancedFeatures/";
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_39_STANDARD, "1234567");
// Make the checksum to be visible on the barcode
bb.getParameters().getBarcode().setChecksumEnabled(EnableChecksum.DEFAULT);
// Save the Barcode image to file
bb.save(dataDir + "checksum.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(GenerateAPatchCode.class) + "Barcode/AdvancedFeatures/";
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.PATCH_CODE, "Patch T");
generator.save(dataDir + "patch.bmp");
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.PATCH_CODE, "Patch T");
generator.getParameters().getBarcode().getBarCodeWidth().setMillimeters(150); // in millimeters
generator.getParameters().getBarcode().getPadding().getTop().setPixels(0.5f);// make the same small margins
generator.getParameters().getBarcode().getPadding().getRight().setPixels(0.5f);
generator.getParameters().getBarcode().getPadding().getLeft().setPixels(0.5f);
generator.getParameters().getBarcode().getPadding().getBottom().setPixels(0.5f);
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.NONE); // to hide
// codetext
BufferedImage topImg = generator.generateBarCodeImage();
generator.getParameters().setRotationAngle(90);
BufferedImage leftImg = generator.generateBarCodeImage();
generator.getParameters().setRotationAngle(90);
BufferedImage bottomImg = generator.generateBarCodeImage();
generator.getParameters().setRotationAngle(90);
BufferedImage rigthtImg = generator.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);
// 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
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.AUSTRALIA_POST, "1234567890");
// Save the image to disk in PNG format
generator.save(dataDir + "australiaPostBarcode.png");
// The path to the resource directory.
String dataDir = Utils.getDataDir(ManageXAndYDimension.class) + "Barcode/AdvancedFeatures/";
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "12345678");
// Set the x-dimension for the bars of the barcode
generator.getParameters().getBarcode().getXDimension().setMillimeters(0.5f);
// Save the Barcode image to file
generator.save(dataDir + "xDimention.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(ManageXAndYDimension.class) + "Barcode/AdvancedFeatures/";
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.PDF_417, "12345678");
// Set the Y-Dimension for the bars of the barcode
generator.getParameters().getBarcode().getBarHeight().setMillimeters(4);
// Save the Barcode image to file
generator.save(dataDir + "yDimention.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(SetBarsHeight.class) + "Barcode/AdvancedFeatures/";
// Instantiate barcode object
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "12345678");
// Set the bar height to be 3 milimeter
generator.getParameters().getBarcode().getBarHeight().setMillimeters(3.0f);
// Save the Barcode image to file
generator.save(dataDir + "barsHeight.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(SetStartAndStopSymbols.class) + "Barcode/AdvancedFeatures/";
// Create instance of BarcodeGenerator, specify codetext and symbology in the
// constructor
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODABAR, "12345678");
// Set the Codabar start symbol to A
generator.getParameters().getBarcode().getCodabar().setCodabarStartSymbol(CodabarSymbol.A);
// Set the Codabar stop symbol to D
generator.getParameters().getBarcode().getCodabar().setCodabarStopSymbol(CodabarSymbol.D);
// Save the image to disk in PNG format
generator.save(dataDir + "startAndStopSymbols.png");
// The path to the resource directory.
String dataDir = Utils.getDataDir(SupplementData.class) + "Barcode/AdvancedFeatures/";
// Create instance of BarcodeGenerator, specify codetext and symbology in the
// constructor
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.EAN_13, "123456789123");
// Set the supplement data (5 Digit)
generator.getParameters().getBarcode().getSupplement().setSupplementData("12345");
// Set space between the supplemental barcode and main barcode
generator.getParameters().getBarcode().getSupplement().getSupplementSpace().setPoint(2.0f);
// Save the Barcode image to file
generator.save(dataDir + "supplementData.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(WideNarrowRatio.class) + "Barcode/AdvancedFeatures/";
// Instantiate barcode object
// Create instance of BarcodeGenerator, specify codetext and symbology in the
// constructor
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "12345678");
// Set the wide to narrow ratio for the barcode
generator.getParameters().getBarcode().setWideNarrowRatio(3.0f);
// Save the image to disk in PNG format
generator.save(dataDir + "wideNarrowRatio.png");
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeCaption.class) + "Barcode/BasicFeatures/";
// Create instance of BarcodeGenerator, specify codetext and symbology in the
// constructor
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "12345678");
bb.getParameters().getCaptionAbove().setAlignment(TextAlignment.LEFT);
bb.getParameters().getCaptionAbove().setText("Aspose.Demo");
bb.getParameters().getCaptionAbove().setVisible(true);
bb.getParameters().getCaptionAbove().getFont().setFamilyName("Pristina");
bb.getParameters().getCaptionAbove().getFont().getSize().setPoint(14);
bb.getParameters().getCaptionAbove().setTextColor(java.awt.Color.RED);
bb.getParameters().getCaptionBelow().setAlignment(TextAlignment.RIGHT);
bb.getParameters().getCaptionBelow().setText("Aspose.Demo");
bb.getParameters().getCaptionBelow().setVisible(true);
bb.getParameters().getCaptionBelow().getFont().setFamilyName("Pristina");
bb.getParameters().getCaptionBelow().getFont().getSize().setPoint(14);
bb.getParameters().getCaptionBelow().setTextColor(java.awt.Color.RED);
// Save the Barcode image to file
bb.save(dataDir + "barcodeCaption.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(CodeTextForegroundColor.class) + "Barcode/BasicFeatures/";
// Create instance of BarcodeGenerator, specify codetext and symbology in the
// constructor
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "12345678");
// Set up code text color
generator.getParameters().getBarcode().getCodeTextParameters().setColor(Color.RED);
// Save the Barcode image to file
generator.save(dataDir + "codeTextForegroundColor.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(CodeTextLocation.class) + "Barcode/BasicFeatures/";
// Create instance of BarcodeGenerator, specify codetext and symbology in the
// constructor
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.DATA_MATRIX,
"GTIN:898978777776665655 " + "UID: 121212121212121212 " + "Batch:GH768 " + "Exp.Date:150923");
// Set up code text color
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.ABOVE);
generator.save(dataDir + "codetextAbove.png");
// The path to the resource directory.
String dataDir = Utils.getDataDir(SetCodeText.class) + "Barcode/BasicFeatures/";
// Create instance of BarcodeGenerator, specify codetext and symbology in the
// constructor
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "12345678");
// Set the width of the bars to 0.5 millimeter
generator.getParameters().getBarcode().getXDimension().setMillimeters(0.5f);
// Save the barcode image to file
generator.save(dataDir + "setCodeText.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(SpecifySymbology.class) + "Barcode/BasicFeatures/";
// Create instance of BarcodeGenerator, specify codetext and symbology in the
// constructor
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_39_STANDARD, "Test-123");
generator.save(dataDir + "Code39Standard.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeImageBorders.class) + "BarcodeImage/BasicFeatures/";
// Instantiate Barcode object, Set the Symbology type to code128 and Set the
// Code text for the barcode
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "1234567");
// Set border style to solid
bb.getParameters().getBorder().setDashStyle(BorderDashStyle.SOLID);
// Set border margins for Top, Right, Left and Bottom
bb.getParameters().getBarcode().getPadding().getTop().setPixels(2f);
bb.getParameters().getBarcode().getPadding().getRight().setPixels(2f);
bb.getParameters().getBarcode().getPadding().getLeft().setPixels(2f);
bb.getParameters().getBarcode().getPadding().getBottom().setPixels(2f);
// Set border width
bb.getParameters().getBorder().getWidth().setPixels(2.5f);
// Set the border's color to red
bb.getParameters().getBorder().setColor(java.awt.Color.RED);
// Enable border to be shown in the barcode
bb.getParameters().getBorder().setVisible(true);
// Save the image
bb.save(dataDir + "barcode-image-borders.jpg");
// Enable border to be shown in the barcode
bb.getParameters().getBorder().setVisible(true);
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeImageMargins.class) + "BarcodeImage/BasicFeatures/";
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "1234567");
//Set border margins for Top, Right, Left and Bottom
bb.getParameters().getBarcode().getPadding().getTop().setPixels(2f);
bb.getParameters().getBarcode().getPadding().getRight().setPixels(2f);
bb.getParameters().getBarcode().getPadding().getLeft().setPixels(2f);
bb.getParameters().getBarcode().getPadding().getBottom().setPixels(2f);
// Save the image
bb.save(dataDir + "barcode-image-margins.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeImageQuality.class) + "BarcodeImage/BasicFeatures/";
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128,"1234567");
bb.getParameters().setResolution(400);
// Save the image
bb.save(dataDir + "barcode-image-quality.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(ColorizeBarcodeImage.class) + "BarcodeImage/BasicFeatures/";
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128,"1234567");
//Set background color of the barcode
bb.getParameters().setBackColor(Color.YELLOW);
//Set the foreground color of the barcode
bb.getParameters().getBarcode().setForeColor(Color.BLUE);
//Set border's color
bb.getParameters().getBorder().setColor(Color.RED);
//Set the code text's color
bb.getParameters().getBarcode().getCodeTextParameters().setColor(Color.RED);
// Save the image
bb.save(dataDir + "colorizeBarcode.png");
// The path to the resource directory.
String dataDir = Utils.getDataDir(GenerateBarcodeWithEmptyBars.class) + "BarcodeImage/BasicFeatures/";
//Create an instance of BarcodeGenerator and initialize it with CodeText and Symbology
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "TEXT");
//Set the FilledBars property to false
generator.getParameters().getBarcode().setFilledBars(false);
//Save the resultant barcode image on disk
generator.save(dataDir + "barcodeWithEmptyBars.png", BarCodeImageFormat.PNG);
// The path to the resource directory.
String dataDir = Utils.getDataDir(RotateBarcodeImage.class) + "BarcodeImage/BasicFeatures/";
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_39_EXTENDED,"1234567");
//Rotate clockwise for 180 degree (upside down)
bb.getParameters().setRotationAngle(180);
// Save the image
bb.save(dataDir + "barcode-image-rotate.jpg");
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";
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "12345678");
try {
bb.save(fileName);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 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);
}
}
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "12345678");
// Generate bar code image
Image image = bb.generateBarCodeImage();
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) {
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "1234567");
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);
}
}
public class RenderBarcodeToServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "1234567");
BufferedImage image = bb.generateBarCodeImage();
response.setContentType("image/png");
OutputStream outputStream = response.getOutputStream();
ImageIO.write(image, "png", outputStream);
outputStream.close();
}
}
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeCustomSize.class) + "BarcodeImage/UtilityFeatures/";
// Instantiate barcode object, Set the Code text for the barcode and the
// symbology type to Code39Standard
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_39_STANDARD,
"1234567890");
// Set auto size false
generator.getParameters().getBarcode().setAutoSizeMode(AutoSizeMode.NEAREST);
// Set height
generator.getParameters().getBarcode().getBarCodeHeight().setMillimeters(50);
// Set width
generator.getParameters().getBarcode().getBarCodeWidth().setMillimeters(120);
// Save the image
generator.save(dataDir + "barcode-custom-size.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeImageResolution.class) + "BarcodeImage/UtilityFeatures/";
// Instantiate barcode object, Set the symbology type to code128 and Set the
// Code text for the barcode
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "1234567");
// customized resolution settings
bb.getParameters().setResolution(200f);
// Save the image
bb.save(dataDir + "barcode-image-resolution.jpg");
// The path to the resource directory.
String dataDir = Utils.getDataDir(BarcodeCustomSize.class) + "BarcodeImage/UtilityFeatures/";
// Instantiate barcode object, Set the symbology type to code128 and Set the
// Code text for the barcode
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "1234567");
bb.getParameters().getBarcode().setAutoSizeMode(AutoSizeMode.NONE);
// Set image height & width to minimum
bb.getParameters().getBarcode().getBarCodeWidth().setMillimeters(1);
bb.getParameters().getBarcode().getBarCodeHeight().setMillimeters(1);
// Save the barcode image
javax.imageio.ImageIO.write(bb.generateBarCodeImage(), "PNG", new java.io.File(dataDir + "minimumresult.png"));
// The path to the resource directory.
String dataDir = Utils.getDataDir(SaveBarcodeImagesToDifferentFormats.class) + "BarcodeImage/UtilityFeatures/";
// Instantiate barcode object, Set the symbology type to code128 and Set the
// Code text for the barcode
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "1234567");
// Save the image to your system and set its image format to Jpeg
bb.save(dataDir + "barcode-image-format.jpg", BarCodeImageFormat.JPEG);
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_128, "123456");
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
generator.save(outStream, BarCodeImageFormat.JPEG);
// The path to the resource directory.
String dataDir = Utils.getDataDir(SetSizeUnitForBarcodeImage.class) + "BarcodeImage/UtilityFeatures/";
// Instantiate barcode object, Set the symbology type to code128 and Set the
// Code text for the barcode
BarcodeGenerator bb = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_128, "1234567");
// Set the bar height to 3 points
bb.getParameters().getBarcode().getBarHeight().setPoint(3.0f);
// Save the image
bb.save(dataDir + "barcode-size-unit.jpg");
// 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);
// Barcode orientation is detected automatically
// Try to recognize all possible barcodes in the image
while (reader.read()) {
System.err.println("Codetext: " + reader.getCodeText());
}
// Close reader
reader.close();
// 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();
// 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 high performance mode
reader.setQualitySettings(QualitySettings.getHighPerformance());
// set separate options
reader.getQualitySettings().setAllowMedianSmoothing(true);
reader.getQualitySettings().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();
// The path to the resource directory.
String dataDir = Utils.getDataDir(GenerateMultipleBarcodesOnASingleImage.class)
+ "BarcodeReader/advanced_features/";
HashMap collection = new HashMap();
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()) {
BarcodeGenerator bb = new BarcodeGenerator((BaseEncodeType) collection.get(key));
bb.setCodeText((String) key);
// bb.setSymbologyType((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);
// 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;
}
// 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();
// The path to the resource directory.
String dataDir = Utils.getDataDir(UsingManualHintsToOptimizeScan.class) + "BarcodeReader/advanced_features/";
// Create an instance of BarCodeReader class
// Set file path
// Set the recognition type
BarCodeReader reader = new BarCodeReader(dataDir + "1_1.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());
}
// 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();
// 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");
// Get BufferedImage with exact bar code only
java.awt.image.BufferedImage img = javax.imageio.ImageIO.read(new java.io.File(dataDir));
// Create an instance of BarCodeReader class
// and specify an area to look for the barcode
BarCodeReader reader = new BarCodeReader(img, 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();
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 + "_");
}
}
// The path to the documents directory.
String dataDir = Utils.getDataDir(SingleWipedBarsInPattern.class) + "BarcodeReader/advanced_features/";
// Create an instance of BarCodeReader class
// Set file path
// Set the recognition type
BarCodeReader reader = new BarCodeReader(dataDir + "file.tiff", DecodeType.CODE_128);
// Perform read operation
reader.getQualitySettings().setAllowOneDWipedBarsRestoration(true);
while (reader.read()) {
System.out.println("codetext: " + reader.getCodeText());
System.out.println("Symbology type: " + reader.getCodeType());
}
reader.close();
// 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.setQualitySettings(QualitySettings.getHighPerformance());
// 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();
// 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.GS_1_DATA_MATRIX);
System.out.println("Skip rotated barcodes");
reader.setQualitySettings(QualitySettings.getHighQuality());
while (reader.read()) {
System.out.println(
reader.getCodeType() + ": " + 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.GS_1_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);
// 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());
}
// 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, 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.getCodeType());
}
// 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());
}
// 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());
}
// 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);
// 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");
// 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
// Create an instance of BarCodeReader class
// and pass image object, Rectangle object and bar code symbology type as
// parameters
BarCodeReader reader2 = new BarCodeReader(img3, 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 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);
BarCodeReader reader = new BarCodeReader("GS1Code128", DecodeType.MOST_COMMON_TYPES);
while (reader.read()) {
String codeType = reader.getCodeType().toString();
String codeText = reader.getCodeText();
System.out.println(codeType + ", " + codeText);
}
BarCodeReader reader = new BarCodeReader("test.png", DecodeType.POSTAL_TYPES);
while (reader.read()) {
String codeType = reader.getCodeType().toString();
String codeText = reader.getCodeText();
System.out.println(codeType + ", " + codeText);
}
BarCodeReader reader = new BarCodeReader("test.png", DecodeType.TYPES_1D);
while (reader.read()) {
String codeType = reader.getCodeType().toString();
String codeText = reader.getCodeText();
System.out.println(codeType + ", " + codeText);
}
public class Common
{
public static String HOST_URI = "jdbc:mysql://host/test";
public static String USERNAME = "root";
public static String PASSWORD = "pwd";
}
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.aspose.barcode.barcoderecognition.BarCodeReader;
import com.aspose.barcode.barcoderecognition.DecodeType;
// This class will select the records from MySQL DB
// read a BLOB type column to fetch image stream from DB
// construct a file on disk and recognize the barcode from the image
public class FetchAndRecognizeBarCode {
public void PerformRecognition() {
try {
String strBarCodeImage = "c:\\temp\\code39.jpg";
// Step-1 - Select the record from the DB on the basis of ProductNumber
// String strProductNumber = "NOK-E71";
// Open a connection to the database
Connection con = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(Common.HOST_URI, Common.USERNAME, Common.PASSWORD);
// Create a statement to execute the SELECT SQL
PreparedStatement st = con.prepareStatement("SELECT * FROM Product ");
// st.setString(1, strProductNumber);
st.executeQuery();
// Get the resultset
ResultSet rs = st.getResultSet();
// Now check if we have any record in the resultset
int nCount = 0;
while (rs.next()) {
// We got a record, read BLOB field and create image from it
String len1 = rs.getString("BarCodeImage");
int len = len1.length();
byte[] b = new byte[len];
// Create stream to read the image
InputStream in = rs.getBinaryStream("BarCodeImage");
// Write the stream to a file
int index = in.read(b, 0, len);
OutputStream outImgBarCode = new FileOutputStream(strBarCodeImage);
while (index != -1) {
// Write bytes to file
outImgBarCode.write(b, 0, index);
// Read next bytes
index = in.read(b, 0, len);
}
// Close the stream and connection
outImgBarCode.close();
// Now that we have got the image from the database
// read the barcode from the image
BarCodeReader reader = new BarCodeReader(strBarCodeImage, DecodeType.CODE_39_STANDARD);
while (reader.read()) {
System.out.println("BarCode found: Code Text: " + reader.getCodeText());
}
nCount++;
}
System.out.println(nCount + " records found.");
con.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import com.aspose.barcode.EncodeTypes;
import com.aspose.barcode.generation.BarcodeGenerator;
// This class will generate a barcode image, save it to file on disk
// then create the stream from file and insert the image stream into DB (BLOB type column)
public class GenerateAndSaveBarCode {
public void PerformInsertExample() {
try {
// Step-1 - Generate barcode and save temporarily in a file
String strBarCodeImage = "c:\\temp\\code39.jpg";
String strCodeText = "NOK-E71";
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_39_STANDARD);
generator.setCodeText(strCodeText);
generator.save(strBarCodeImage);
// Step-2 - insert a new record in MySQL DB
Connection con = null;
// Open connection
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(Common.HOST_URI, Common.USERNAME, Common.PASSWORD);
// Prepare statement
PreparedStatement pre = con.prepareCall(
"Insert INTO Product (ProductNumber, ProductName, BarCodeImage) " + "VALUES (?, ?, ?) ");
// Set product number and product name
pre.setString(1, "NOK-E71");
pre.setString(2, "Nokia E Series - E71");
// 3rd column is for barcode image. DB type is BLOB
// for saving the image, we need to create stream from the image file
File imgFile = new File(strBarCodeImage);
FileInputStream fin = new FileInputStream(imgFile);
pre.setBinaryStream(3, fin, (int) imgFile.length());
// Now execute the statement
pre.executeUpdate();
System.out.println("Insertion successfull.");
// Close connection
pre.close();
con.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public void PerformUpdateExample() {
try {
// Step-1 - Generate barcode and save temporarily in a file
String strBarCodeImage = "c:\\temp\\code39.jpg";
String strCodeText = "NOK-E71-UPDATED";
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_39_STANDARD);
generator.setCodeText(strCodeText);
generator.save(strBarCodeImage);
// Step-2 - update the record in MySQL DB
Connection con = null;
// Open connection
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(Common.HOST_URI, Common.USERNAME, Common.PASSWORD);
// Prepare statement
PreparedStatement pre = con.prepareCall("UPDATE Product SET BarCodeImage = ? WHERE ProductNumber = ? ");
// Barcode image column. DB type is BLOB
// For saving the image, we need to create stream from the image file
File imgFile = new File(strBarCodeImage);
FileInputStream fin = new FileInputStream(imgFile);
pre.setBinaryStream(1, fin, (int) imgFile.length());
// 2nd column in where condition is the ProductNumber
pre.setString(2, "NOK-E71");
// Now execute the statement
pre.executeUpdate();
System.out.println("Update successfull.");
// Close connection
pre.close();
con.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
public class Main {
public static void main(String[] args) {
// To insert and update barcode image in MySQL DB
GenerateAndSaveBarCode sample1 = new GenerateAndSaveBarCode();
sample1.PerformInsertExample();
sample1.PerformUpdateExample();
// To fetch the image from DB and recognize codetext
FetchAndRecognizeBarCode sample2 = new FetchAndRecognizeBarCode();
sample2.PerformRecognition();
}
}
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR, "Aspose.BarCode sample");
generator.getParameters().getBarcode().getXDimension().setPixels(3);
generator.getParameters().getBarcode().getPadding().getLeft().setPixels(20);
generator.getParameters().getBarcode().getPadding().getRight().setPixels(20);
generator.save(dataDir + "GenerateBarcodeWithoutRestrictedBarcodeSize_out.png");
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR, "Aspose.BarCode sample");
generator.getParameters().getBarcode().setAutoSizeMode(AutoSizeMode.NEAREST);
generator.getParameters().getBarcode().getBarCodeWidth().setPixels(200);
generator.getParameters().getBarcode().getBarCodeHeight().setPixels(200);
generator.save(dataDir + "GenerateBarcodeWithRestrictedBarcodeSize_out.png");
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_16_K);
generator.getParameters().getBarcode().setAutoSizeMode(AutoSizeMode.NEAREST);
generator.getParameters().getBarcode().getBarCodeWidth().setPixels(100);
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.NONE);
generator.save(dataDir + "Code16K_Nearest.png");
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR, "Aspose.BarCode sample");
generator.getParameters().getBarcode().getXDimension().setPixels(3);
generator.getParameters().getBarcode().getQR().setAspectRatio(1.5f);
System.out.println("Width = " + generator.getParameters().getBarcode().getBarCodeWidth().getPixels());
System.out.println("Height = " + generator.getParameters().getBarcode().getBarCodeHeight().getPixels());
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.AUSTRALIAN_POSTE_PARCEL);
String codetext = generator.getCodeText(); // 99712345678901234567890103456
generator = new BarcodeGenerator(EncodeTypes.EAN_13);
codetext = generator.getCodeText(); // 590123412345
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR);
generator.getParameters().getBarcode().getQR().setQrVersion(QRVersion.VERSION_18);
generator.getParameters().getBarcode().getQR().setQrErrorLevel(QRErrorLevel.LEVEL_M);
generator.save(dataDir + "GroupingPropertiesByBarcodeType_out.png");
String CODICE = "(90)0843110730<<<<452287005001T8";
String displayedText = "(90)0843" + "\r\n" + "110730<<<<" + "\r\n" + "452287" + "\r\n" + "005001T8" + "\r\n";
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.GS_1_DATA_MATRIX, CODICE);
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.ABOVE);
generator.getParameters().getBarcode().getCodeTextParameters().setTwoDDisplayText(displayedText);
BufferedImage bitmap = generator.generateBarCodeImage();
File imageFile = new File(dataDir + "Display2DText.png");
ImageIO.write(bitmap, "png", imageFile);
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.DOT_CODE);
generator.getParameters().getBarcode().getDotCode().setDotCodeMask(2);
BufferedImage bitmap = generator.generateBarCodeImage();
File imageFile = new File(dataDir + "DotCode.png");
ImageIO.write(bitmap, "png", imageFile);
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.DATA_MATRIX);
generator.getParameters().getBarcode().setAutoSizeMode(AutoSizeMode.INTERPOLATION);
generator.getParameters().getBarcode().getBarCodeWidth().setMillimeters(50);
generator.getParameters().getBarcode().getBarCodeHeight().setInches(1.3f);
BufferedImage bitmap = generator.generateBarCodeImage();
File imageFile = new File(dataDir + "DataMatrix.png");
ImageIO.write(bitmap, "png", imageFile);
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.MAXI_CODE);
generator.getParameters().getBarcode().getMaxiCode().setMaxiCodeEncodeMode(5);
BufferedImage bitmap = generator.generateBarCodeImage();
File imageFile = new File(dataDir + "MaxiCodeEncodeMode.png");
ImageIO.write(bitmap, "png", imageFile);
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.UPCA_GS_1_CODE_128_COUPON);
// UPCA part is "514141100906", GS1Code128 part is "(8102)03"
generator.setCodeText("514141100906(8102)03");
generator.save(dataDir + "UpcaGs1Code128Coupon.png");
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.UPCA_GS_1_DATABAR_COUPON);
// UPCA part is "514141100906", GS1Databar part is "(8110)001234502239811110555"
generator.setCodeText("512345678900(8110)001234502239811110555");
generator.save("UpcaGs1DatabarCoupon.png");
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.QR, "Aspose.BarCode sample");
generator.getParameters().getBarcode().setAutoSizeMode(AutoSizeMode.NEAREST);
generator.getParameters().getBarcode().getBarCodeWidth().setMillimeters(20);
generator.getParameters().getBarcode().getBarCodeHeight().setMillimeters(20);
generator.save(dataDir + "for_display_out.png");
generator.getParameters().setResolution(300);
generator.save(dataDir + "for_printer_out.png");
System.out.println("Assembly version: " + BuildVersionInfo.ASSEMBLY_VERSION);
System.out.println("Product: " + BuildVersionInfo.PRODUCT);
System.out.println("Release Date: " + BuildVersionInfo.RELEASE_DATE);
BarcodeGenerator generator = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.EAN_13, "348503498549085409");
// Error message will not been thrown in case of false, default value is also
// false
generator.getParameters().getBarcode().setThrowExceptionWhenCodeTextIncorrect(true);
// Get barcode image
BufferedImage image = generator.generateBarCodeImage();
String dataDir = Utils.getDataDir(CreateAndSetSizeForWholePictureWithBarcode.class) + "TechnicalArticles/";
// Generate the bar code
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.PDF_417);
// Set the code text
generator.setCodeText("One thing 2 thing");
// Set the code text location
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.NONE);
// Set margins
generator.getParameters().getBarcode().getPadding().getBottom().setPixels(0);
generator.getParameters().getBarcode().getPadding().getLeft().setPixels(0);
generator.getParameters().getBarcode().getPadding().getRight().setPixels(0);
generator.getParameters().getBarcode().getPadding().getTop().setPixels(0);
// Get BufferedImage with exact bar code only
BufferedImage img = generator.generateBarCodeImage();
// Saving the buffered image
File outputfile = new File(dataDir + "custombarcode.png");
ImageIO.write(img, "png", outputfile);
String dataDir = Utils.getDataDir(CreateAnImageWithExactBarcodeOnly.class) + "TechnicalArticles/";
// Generate the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_128);
// Set the code text
generator.getParameters().getBarcode().getCodeTextParameters().setTwoDDisplayText("123456");
// Get bitmap with exact barcode only
BufferedImage image = generator.generateBarCodeImage();
// Saving the buffered image
File outputfile = new File(dataDir + "custombarcode.png");
ImageIO.write(image, "png", outputfile);
// Instantiate BarCodeReader object
BarCodeReader reader = new BarCodeReader("rotatedbarcode.jpg", DecodeType.CODE_128);
// read Code128 bar code
while (reader.read()) {
// detect bar code orientation
System.out.println("Rotaion Angle: " + reader.getAngle());
}
reader.close();
public class FoundBarCodes {
String CodeText;
com.aspose.barcode.barcoderecognition.BarCodeRegion BarCodeRegion;
public FoundBarCodes(String cText, com.aspose.barcode.barcoderecognition.BarCodeRegion region) {
this.CodeText = cText;
this.BarCodeRegion = region;
}
public String getCodeText() {
return CodeText;
}
public com.aspose.barcode.barcoderecognition.BarCodeRegion getRegion() {
return BarCodeRegion;
}
}
String dataDir = Utils.getDataDir(Pdf417BarcodeWithChineseCharacters.class) + "TechnicalArticles/";
// Generate the barcode
BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.PDF_417);
// Set the code text
String codetext = "被洪水困�的";
ByteBuffer bytebuffer = Charset.forName("MS936").encode(codetext);
byte[] bytes = bytebuffer.array();
String codeText = new String(bytes);
gen.setCodeText(codeText);
// Set the display text
gen.getParameters().getBarcode().getCodeTextParameters().setTwoDDisplayText(codetext);
gen.save(dataDir + "barcode.png");
String dataDir = Utils.getDataDir(Pdf417BarcodeWithTurkishCharacters.class) + "TechnicalArticles/";
// Generate the barcode
BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.PDF_417);
// Set the code text
String codetext = "AYŞE" + "\n" + "Ümit" + "\n" + "Ümit@estee.com" + "\n" + "Türkiye";
ByteBuffer bytebuffer = Charset.forName("windows-1254").encode(codetext);
byte[] bytes = bytebuffer.array();
String codeText = new String(bytes);
gen.setCodeText(codeText);
// Set the display text
gen.getParameters().getBarcode().getCodeTextParameters().setTwoDDisplayText(codetext);
gen.save(dataDir + "barcode.png");
String dataDir = Utils.getDataDir(ReadAndSortBarcodesInSpecificOrder.class) + "TechnicalArticles/";
String path = dataDir + "barcode.png";
List<FoundBarCodes> found = new ArrayList<FoundBarCodes>();
BarCodeReader reader = new BarCodeReader(path, DecodeType.CODE_128);
while (reader.read())
found.add(new FoundBarCodes(reader.getCodeText(), reader.getRegion()));
Comparator<FoundBarCodes> foundComparator = new Comparator<FoundBarCodes>() {
@Override
public int compare(FoundBarCodes e1, FoundBarCodes e2) {
return e1.getCodeText().compareTo(e2.getCodeText());
}
};
found.sort(foundComparator);
int i = 1;
for (FoundBarCodes barcode : found) {
Point[] point = barcode.BarCodeRegion.getPoints();
System.out.println("Codetext ( " + i + " ): " + barcode.CodeText);
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());
System.out.println();
i++;
}
import com.aspose.barcode.*;
import com.aspose.barcode.License;
import com.aspose.barcode.barcoderecognition.BarCodeReader;
import com.aspose.barcode.barcoderecognition.BaseDecodeType;
import com.aspose.barcode.examples.ApplyALicense;
import com.aspose.barcode.examples.Utils;
import com.aspose.barcode.examples.barcode_recognition.advanced_features.BarcodeOrientation;
import com.aspose.barcode.generation.BarcodeGenerator;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.aspose.pdf.*;
import com.aspose.pdf.facades.PdfExtractor;
public class RecognitionFromPDF {
public static void main(String[] args) {
try {
// Set license for Barcode
ApplyALicense.applyALicense();
// Set license for PDF
com.aspose.pdf.License licBarCode = new com.aspose.pdf.License();
licBarCode.setLicense("Aspose.Pdf.Java.lic");
String dataDir = Utils.getDataDir(RecognitionFromPDF.class) + "TechnicalArticles/";
String strPdfDoc = dataDir + "output1.pdf";
String strBarCodeImage = "";
// Generate barcode and add to PDF file
BarcodeGenerator builder = new BarcodeGenerator(EncodeTypes.CODE_39_STANDARD);
builder.setCodeText("test123");
String strBarCodeImage