Skip to content

Commit

Permalink
Fixed and improved page /java/barcode-generation-and-recognition-tuto…
Browse files Browse the repository at this point in the history
…rial/
  • Loading branch information
alex-grinin committed Feb 11, 2025
1 parent c4671a3 commit 0c06b7f
Showing 1 changed file with 13 additions and 70 deletions.
83 changes: 13 additions & 70 deletions java/getting-started/library-tutorial/_index.md
Original file line number Diff line number Diff line change
@@ -1,82 +1,25 @@
---
title: Barcode Generation and Recognition Tutorial
title: Code Examples
type: docs
weight: 80
feedback: BARCODECOM
url: /java/barcode-generation-and-recognition-tutorial/
---
This tutorial explains how to use ***Aspose.BarCode for Java*** to generate a PDF417 barcode image and then read a barcode from this image.

In the task considered in this tutorial, we use Eclipse as an IDE, which is free and could be downloaded here:
<http://www.eclipse.org/downloads/>

## **Barcode Generation**
1. Start Eclipse and create a new Java project
## **How to Work with Non-ASCII Symbols**
This example demonstrates how to generate and read a PDF417 barcode containing non-ASCII text using Aspose.BarCode for Java.

**Creating a new Java project**

![todo:image_alt_text](barcode-generation-and-recognition-tutorial_1.png)
{{<highlight java>}}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.PDF_417);
gen.setCodeText("sýkora je malý pták", StandardCharsets.UTF_8);
gen.save(outputStream, BarCodeImageFormat.PNG);

1. Call the project Demo and click **Finish**
We will configure the library later.
BarCodeReader reader = new BarCodeReader(new ByteArrayInputStream(outputStream.toByteArray()), DecodeType.PDF_417);
BarCodeResult[] results = reader.readBarCodes();

**Naming the project**

![todo:image_alt_text](barcode-generation-and-recognition-tutorial_2.png)

1. Right-click on the project we have just created and select required properties

**Setting project properties**

![todo:image_alt_text](barcode-generation-and-recognition-tutorial_3.png)

1. Click on the **Java Build Path** item and select the **Libraries** tab
1. Click **Add external Jars**
1. In the opened file dialog, locate Aspose.BarCode.jar and Servlet-api.jar. They are available in the lib directory of the download package of Aspose.BarCode for Java

**Adding references**

![todo:image_alt_text](barcode-generation-and-recognition-tutorial_4.png)

{{% alert color="primary" %}}

You can use your own Servlet-api.jar if available.

{{% /alert %}}

1. Right-click on the Demo project and select **New** and then **Class** at Package Explorer.

**Creating a new class**

![todo:image_alt_text](barcode-generation-and-recognition-tutorial_5.png)

1. Let this new class be an Applet and name it "Test"

**Setting properties for the new class**

![todo:image_alt_text](barcode-generation-and-recognition-tutorial_6.png)

1. Add the code to generate a PDF417 barcode:

{{< gist "aspose-com-gists" "9dea2dd38be50330a824dd05da062a97" "Examples-src-main-java-com-aspose-barcode-examples-TwoD_barcodes-basic_features-Test-GenerateBarCodeDemoApplet.java" >}}

1. Right-click on this class in the package explorer and select **Run As** and **Java Applet**

![todo:image_alt_text](barcode-generation-and-recognition-tutorial_7.png)

1. Save the image to a file. Continuing the sample above, add this code to save the generated barcode image to a file:

{{< gist "aspose-com-gists" "9dea2dd38be50330a824dd05da062a97" "Examples-src-main-java-com-aspose-barcode-examples-TwoD_barcodes-basic_features-Test-SaveToDisk.java" >}}

## **Barcode Recognition**
Continuing the sample above, we can add the code to read a barcode from an image:

{{< gist "aspose-com-gists" "9dea2dd38be50330a824dd05da062a97" "Examples-src-main-java-com-aspose-barcode-examples-barcode_recognition-basic_features-Barcode_Recognition-Barcode_Recognition.java" >}}

The evaluation version of Aspose.BarCode for Java allows decoding only Code 39 barcodes without limitations. To test the reading speed and accuracy for other symbologies, please use the demo JAR provided in the download package.

{{% alert color="primary" %}}

If creating an instance of class BarCodeReader throws an exception, it is probably because the image format is not supported. Download the free JAI library to load the image from [www.java.net](http://www.java.net).

{{% /alert %}}
System.out.println("Code Type : " + results[0].getCodeTypeName());
System.out.println("Code Text : " + results[0].getCodeText(StandardCharsets.UTF_8));
{{</highlight>}}

0 comments on commit 0c06b7f

Please sign in to comment.