---
title: Java
slug: cisdk-java
docTags: 
createdAt: 2023-03-12T03:13:21.000Z
---

## Step 1:   Install ClearImage SDK

Download and install the [ClearImage SDK](https://barcode-reader.inliteresearch.com/get-cisdk). &#x20;

## Step 2:   Configure your project

Add <font color="#eb144c">`ClearImageJ.jar`</font> to CLASSPATH. Typically file is located in <font color="#eb144c">`C:\Program Files (x86)\Inlite\Libs`</font> folder.

Java API uses several methods to locate <font color="#eb144c">`ClearImage.dll`</font> listed below in descending priority.

- Explicit path using the <font color="#eb144c">`CiServer.loadClearImage(ClearImageDllPath)`</font> method.
- Folder specified by <font color="#eb144c">`-Djava.library.path="ClearImageDllPath"`</font> on the java command line.
- Folders specified by the <font color="#eb144c">`PATH`</font> environmental variable

Use the correct  **32-bit** or **64-bit** version of  <font color="#eb144c">`ClearImage.dll`</font>, that matches your Java runtime version.    <font color="#eb144c">`ClearImage.dll`</font> is located in subfolder of   <font color="#eb144c">`C:\Program Files (x86)\Inlite\ClearImageSDK`</font> folder  <font color="#eb144c">`\win32`</font>  and  <font color="#eb144c">`\x64`</font>&#x20;

This code demonstrates the use of ClearImage Java API.  Runtime errors are reported in <font color="#eb144c">`Exception`</font> object.

```java
 ICiServer initClearImage() {
	 try {
		CiServer.loadClearImage("C:\\Program Files (x86)\\Inlite\\ClearImageSDK\\x64\\ClearImage.dll");    
		 //  Create ClearImage Server
		 CiServer objServer = new CiServer();
		 ICiServer Server = onjServer.getICiServer();
		 return Server;
	 } catch (Exception ex) {
		 System.out.println(ex.getMessage());
		 return null;
	 }
 }

 void readBarcodes(ICiServer Ci, String fileName, int page) {
	 ICiBarcodePro reader = null;
	 try {
		 reader = Ci.CreateBarcodePro(); // Create and configure barcode reader
		 reader.setType(new FBarcodeType(FBarcodeType.cibfCode39, FBarcodeType.cibfCode128));
		 reader.getImage().Open(fileName, page); // Open image from an image file
		 int n = reader.Find(0); // Read barcodes
		 for (int i = 1; i <= n; i++) { // Process results
			 ICiBarcode Bc = reader.getBarcodes().getItem(i); // getItem is 1-based
			 System.out.println(" Barcode + type: " + Bc.getType() + "   Text: \n" + Bc.getText());
		 }
	 } catch (Exception ex) { // Process exceptions
		 System.out.println(); ex.printStackTrace();
	 } finally {
		 if (reader != null) try {
			 reader.getImage().Close(); // Close images and free memory
		 } catch (Exception ex) {;}
	 }
 }
```

## ClearImage project development

### Extend functionality

While developing your application, you can combine code examples on this page with methods described in [ClearImage API documentation](http://www.inliteresearch.com/help/ci) .

### Exception handling

ClearImage Java API reports errors as an <font color="#eb144c">`Exception`</font> object.&#x20;

```csharp
try {
    //  Call ClearImage .NET API
    } catch (Exception ex)	{
		System.out.println(ex.getMessage());
	}
```

## Barcode Reading

### Read 1D  barcodes from a page

This example reads code 39 and code 128 barcodes.   Set barcode types used in your application. Setting <font color="#eb144c">`reader.setAutoDetect1D(EBoolean.ciTrue);`</font> automatically finds the barcode type, but it is slower and is not recommended for production.  


```java
void ReadBarcode1D_page(ICiServer Server, String fileName, int page) {
	try {
		ICiBarcodePro reader = Server.CreateBarcodePro();
		reader.setType(new FBarcodeType(FBarcodeType.cibfCode39,
				FBarcodeType.cibfCode128)); 
		// readerbcr.setAutoDetect1D(EBoolean.ciTrue);
		reader.getImage().Open(fileName, page);
		reader.Find(0);
		for (int i = 1; i <= reader.getBarcodes().getCount(); i++) {
			ICiBarcode Bc = reader.getBarcodes().getItem(i);
			System.out.println(Bc.getText()); 
		}
	} catch (Exception ex) {
		System.out.println(ex.getMessage());
	} finally {
		System.gc();
	}
}
```

### Read 2D Barcode from a page

This example reads PDF417, DataMatrix, and QR code. 2D barcodes might contain binary data available from <font color="#eb144c">`Bc.getData()`</font> as <font color="#eb144c">`byte[]`</font>

```java
void ReadBarcode2D_page(ICiServer Server, String fileName, int page) {
	try {
		ICiQR reader = Server.CreateQR();
		// ICiPdf417 reader = Server.CreatePdf417();
		// ICiDataMatrix reader  = Server.CreateDataMatrix();
		reader.getImage().Open(fileName, page);
		reader.Find(0);
		for (int i = 1; i <= reader.getBarcodes().getCount(); i++) {
			ICiBarcode Bc = reader.getBarcodes().getItem(i);
			System.out.println(Bc.getText()); 
		}
	} catch (Exception ex) {
		System.out.println(ex.getMessage());
	} finally {
		System.gc();
	}
}
```

### Read Postal Barcodes from a page

This example reads Postal Barcodes from an image file page.  &#x20;

```java
void ReadBarcodePostal_page(ICiServer Server, String fileName, int page) {
	try {
		ICiBarcodePro reader = Server.CreateBarcodePro();
		reader.setType(new FBarcodeType(FBarcodeType.cibf4State)); 
		reader.getImage().Open(fileName, page);
		reader.Find(0);
		for (int i = 1; i <= reader.getBarcodes().getCount(); i++) {
			ICiBarcode Bc = reader.getBarcodes().getItem(i);
			EBarcodeType type = Bc.getType();
			if (type == EBarcodeType.cibUspsIntelligentMail)
					System.out.println(" US Intelligent Mail ");
			if (type == EBarcodeType.cibBpoPostcode)
					System.out.println("UK Royal Mail ");
			if (type == EBarcodeType.cibAustralianPost)
					System.out.println("Australian Mail ");
			if (type == EBarcodeType.cibSingaporePost)
					System.out.println("Singapore Mail ");
			System.out.println(Bc.getText()); 
		}
	} catch (Exception ex) {
		System.out.println(ex.getMessage());
	} finally {
		System.gc();
	}
}
```

### Read Driver's License barcode

This example reads the Driver's License barcode from an image file page.  &#x20;

```java
void ReadBarcode2D_page(ICiServer Server, String fileName, int page) {
	try {
		ICiPdf417 reader = Server.CreatePdf417();
		reader.getImage().Open(fileName, page);
		reader.Find(0);
		for (int i = 1; i <= reader.getBarcodes().getCount(); i++) {
			ICiBarcode Bc = reader.getBarcodes().getItem(i);
			System.out.println(Bc.getText()); 
			if (Bc.getInfo("last") != "") {
				System.out.println("last:    " + Bc.getInfo("last"));
				System.out.println("first:   " + Bc.getInfo("first"));
				System.out.println("middle:  " + Bc.getInfo("middle"));
				System.out.println("suffix:  " + Bc.getInfo("suffix"));
				System.out.println("dob:     " + Bc.getInfo("dob"));
				System.out.println("eyes:    " + Bc.getInfo("eyes"));
				System.out.println("hair:    " + Bc.getInfo("hair"));
				System.out.println("sex:     " + Bc.getInfo("sex"));
				System.out.println("height:  " + Bc.getInfo("height"));
				System.out.println("weight:  " + Bc.getInfo("weight"));
				System.out.println("street:  " + Bc.getInfo("street"));
				System.out.println("city:    " + Bc.getInfo("city"));
				System.out.println("state:   " + Bc.getInfo("state"));
				System.out.println("postal:  " + Bc.getInfo("postal"));
				System.out.println("country: " + Bc.getInfo("country"));
				System.out.println("id:      " + Bc.getInfo("id"));
				System.out.println("issued:  " + Bc.getInfo("issued"));
				System.out.println("expires: " + Bc.getInfo("expires"));
			}
		}
	} catch (Exception ex) {
		System.out.println(ex.getMessage());
	} finally {
		System.gc();
	}
}
```

### Read from  a multi-page image file

This example reads barcodes from a single page or all pages of a multi-page image file.  &#x20;

```java
void ReadBarcode1D_page(ICiServer Server, String fileName, int page) {
	try {
		ICiBarcodePro reader = Server.CreateBarcodePro();
		reader.setType(new FBarcodeType(FBarcodeType.cibfCode39,
				FBarcodeType.cibfCode128)); 
		int page = 1;
		while (true) {
			reader.getImage().Open(fileName, page);
			reader.Find(0);
			for (int i = 1; i <= reader.getBarcodes().getCount(); i++) {
				ICiBarcode Bc = reader.getBarcodes().getItem(i);
				System.out.println(Bc.getText());
			}
			page++;
			if (page > reader.getImage().getPageCount())
				break;
		}
	} catch (Exception ex) {
		System.out.println(ex.getMessage());
	} finally {
		System.gc();
	}
}
```

### Optimize barcode reading

This [page](docId\:SmK4-LGRtfEx0zacVU2LV) describes methods for improving the recognition rate and/or speed

## Image Processing

### Repair image page

This example uses some of the most popular image-processing functions. In a production application, the specific sequence of functions should be selected based on specific application needs. 

```java
void RepairPage (ICiRepair repair)
	{
	repair.AutoDeskew();
	repair.AutoRotate();
	repair.AutoCrop(10, 10, 10, 10);   
	repair.AdvancedBinarize(0, 0 , 0);     
	repair.BorderExtract(EBorderExtractFlags.ciBexBorderDeskewCrop, EBorderExtractAlgorithm.ciBeaCleaner); // Deskew and crop based on black border
	repair.RemovePunchHoles();
	repair.SmoothCharacters(ESmoothType.ciSmoothDarkenEdges);
	repair.CleanNoise(3);               // Clean black noise of 3 pixels
		//	repair.CleanNoiseExt                // Clean black and white noise
		//	  (new ECleanNoiseFlags(ECleanNoiseFlags.ciCnxBlackNoise, ECleanNoiseFlags.ciCnxWhiteNoise), 3, 3, 10, 0); 
	repair.ReconstructLines(ELineDirection.ciLineVertAndHorz);
	}

void Repair_page(ICiServer Server, String fileName, int page, String fileOut) {
	try {
		ICiRepair repair = Server.CreateRepair ();
		repair.getImage().Open (fileName, page);
		RepairPage(repair);
		repair.getImage().SaveAs(fileOut, EFileFormat.ciEXT);
	} catch (Exception ex) {
		System.out.println(ex.getMessage());
	} finally {
		System.gc();
	}
}
```

### Repair all images in a multi-page file

This example repairs all images in a multi-page file and saves the result in the <font color="#eb144c">`fileOut`</font> file.

```java
void Repair_page(ICiServer Server, String fileName, String fileOut) {
	try {
		ICiRepair repair = Server.CreateRepair ();
		int page = 1;
		while (true) {
			repair.getImage().Open (fileName, page);
			RepairPage(repair);  // See code above
			repair.getImage().Append(fileOut, EFileFormat.ciEXT);
			page++;	
			if (page > repair.getImage().getPageCount())
				break;
			}
	} catch (Exception ex) {
		System.out.println(ex.getMessage());
	} finally {
		System.gc();
	}
}
```







