Table of contents
Overview
Numerous image processing functions offered by ClearImage help to:
- Improve the quality and reduce the size of compressed image files
- Prepare image for better visual representation on the Web or in an application.
- Improve OCR/ICR reading rate
- Enable the reading of severely flawed or poor quality barcodes by ClearImage barcode readers
The following examples demonstrate the use of the most popular image processing function. These are merely examples for demonstrating the use of the functions, and do not represent actual production examples. In a production application the specific sequence of functions should be selected based on specific application needs. The ClearImage Demo application offers scripting specifically to support the experimentation and selection process with real life images:
- Use the Repair menu to try particular functions and parameters
- Use the Script menu to build a sequence of functions for easy testing with multiple image files
- Use the File menu -> "Open Sample Script and Image …" to see how some of the most popular functions work
Repair image page
C#
12345678910111213141516171819202122232425262728293031323334
using Inlite.ClearImageNet;// . . . void RepairPage(ImageEditor editor){ editor.AutoDeskew(); editor.AutoRotate(); editor.AutoCrop(10, 10, 10, 10); // Crop to 10 pixels on each side to bitonal // editor.AdvancedBinarize(); // Convert to bitonal an image with complex background patterns editor.ToBitonal(); editor.BorderExtract(BorderExtractMode.deskewCrop); // Deskew and crop based on black border editor.RemovePunchHoles(); editor.SmoothCharacters(); editor.CleanNoise(3); // Clean balck noise of 3 pixels // editor.CleanNoise // Clean black and white noise // (CleanNoiseFlags.black | CleanNoiseFlags.white, 3, 3, 10); editor.ReconstructLines(LineDirection.horzAndVert);} void Repair_image(string fileName, int page, string fileOut){ try { ImageEditor editor = new ImageEditor(); editor.Image.Open(fileName, page); // Do image reapir RepairPage(editor); // Save results editor.Image.SaveAs(fileOut, Inlite.ClearImage.EFileFormat.ciEXT); // To Save results in multi-page TIFF or PDF file use // editor.Image.Append(fileOut, Inlite.ClearImage.EFileFormat.ciEXT); } catch (Exception ex) { ProcessException(ex); }}
VB
1234567891011121314151617181920212223242526272829303132333435
Imports Inlite.ClearImageNet' . . . Sub RepairPage(editor As ImageEditor) editor.AutoDeskew() editor.AutoRotate() editor.AutoCrop(10, 10, 10, 10) ' Crop to 10 pixels on each side to bitonal ' editor.AdvancedBinarize(); // Convert to bitonal an image with complex background patterns editor.ToBitonal() editor.BorderExtract(BorderExtractMode.deskewCrop) ' Deskew and crop based on black border editor.RemovePunchHoles() editor.SmoothCharacters() editor.CleanNoise(3) ' Clean balck noise of 3 pixels ' editor.CleanNoise // Clean black and white noise ' (CleanNoiseFlags.black | CleanNoiseFlags.white, 3, 3, 10); editor.ReconstructLines(LineDirection.horzAndVert)End Sub Sub Repair_image(fileName As String, page As Integer, fileOut As String ) Try Dim editor As New ImageEditor() editor.Image.Open(fileName, page) ' Do image reapir RepairPage(editor) ' Save results editor.Image.SaveAs(fileOut, Inlite.ClearImage.EFileFormat.ciEXT) ' To Save results in multi-page TIFF or PDF file use ' editor.Image.Append(fileOut, Inlite.ClearImage.EFileFormat.ciEXT) Catch ex As Exception ProcessException(ex) End TryEnd Sub
C++
1234567891011121314151617181920212223242526
void RepairPage (ICiRepairPtr repair) { repair->AutoDeskew(); repair->AutoRotate(); repair->AutoCrop(10, 10, 10, 10); // Crop to 10 pixels on each side to bitonal repair->AdvancedBinarize(0, 0 , 0); // Convert to bitonal an image with complex background patterns repair->BorderExtract(ciBexBorderDeskewCrop, ciBeaCleaner); // Deskew and crop based on black border repair->RemovePunchHoles(); repair->SmoothCharacters(); repair->CleanNoise(3); // Clean balck noise of 3 pixels // repair->CleanNoiseExt // Clean black and white noise // ((ECleanNoiseFlags) (ciCnxBlackNoise | ciCnxWhiteNoise), 3, 3, 10); repair->ReconstructLines(ciLineVertAndHorz); } void repair_image (const char *fileName, const long page, const char *fileOut) { try { ICiRepairPtr repair = Ci->CreateRepair (); repair->Image->Open (_bstr_t(fileName), page); RepairPage(repair); repair->Image->SaveAs(_bstr_t(fileOut), ciEXT); } catch (_com_error &ex) {ProcessException(ex);} }
Java
1234567891011121314151617181920212223242526272829
void RepairPage (ICiRepair repair) { repair.AutoDeskew(); repair.AutoRotate(); repair.AutoCrop(10, 10, 10, 10); // Crop to 10 pixels on each side to bitonal repair.AdvancedBinarize(0, 0 , 0); // Convert to bitonal an image with complex background patterns 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_image (String fileName, int page, String fileOut) { try { ICiRepair repair = Ci.CreateRepair (); repair.getImage().Open (fileName, page); RepairPage(repair); repair.getImage().SaveAs(fileOut, EFileFormat.ciEXT); } catch (Exception ex) { ProcessException(ex); } finally { // Collect garbage periodically to release JNI-managed // objects System.gc(); } }
PHP
1234567891011121314151617181920212223242526272829303132333435363738
function RepairPage ($repair){ $repair->AutoDeskew(); $repair->AutoRotate(); $repair->AutoCrop(10, 10, 10, 10); // Crop to 10 pixels on each side to bitonal $repair->AdvancedBinarize(0, 0 , 0); // Convert to bitonal an image with complex background patterns $ciBexBorderDeskewCrop = 3; $ciBeaCleaner = 2; $repair->BorderExtract($ciBexBorderDeskewCrop, $ciBeaCleaner); // Deskew and crop based on black border $repair->RemovePunchHoles(); $ciSmoothDarkenEdges = 1; $repair->SmoothCharacters($ciSmoothDarkenEdges); $repair->CleanNoise(3); // Clean balck noise of 3 pixels // $ciCnxBlackNoise = 1; $ciCnxWhiteNoise = 2; // $repair->CleanNoiseExt($ciCnxBlackNoise + $ciCnxWhiteNoise, 3, 3, 10, 0); // Clean black and white noise $ciLineVertAndHorz = 3; $repair->ReconstructLines($ciLineVertAndHorz);} function repair_image ($fileName, $page, $fileOut){ try { // Create CiServer object: $Ci = new COM("ClearImage.ClearImage"); // Create and configure repair $repair = $Ci->CreateRepair(); // Open Image $repair->Image->Open($fileName, $page); // Do repair RepairPage ($repair); // Save results $repair->Image->SaveAs($fileOut, 0); // ciEXT } catch (Exception $e) { // generic exception handler print "<br>\n" . "Exceptiom in line " . $e->getLine(); print "<br>\n" . $e->getMessage() . "\n<br>"; }}
Delphi
12345678910111213141516171819202122232425262728293031323334353637383940414243
uses ...,ClearImage_TLB, comobj; // . . . procedure RepairPage (repair: ICiRepair); begin repair.AutoDeskew(); repair.AutoRotate(); repair.AutoCrop(10, 10, 10, 10); // Crop to 10 pixels on each side to bitonal repair.AdvancedBinarize(0, 0 , 0); // Convert to bitonal an image with complex background patterns repair.BorderExtract(ciBexBorderDeskewCrop, ciBeaCleaner); // Deskew and crop based on black border repair.RemovePunchHoles(); repair.SmoothCharacters(ciSmoothDarkenEdges); repair.CleanNoise(3); // Clean black noise of 3 pixels // repair.CleanNoiseExt // Clean black and white noise // (ciCnxBlackNoise + ciCnxWhiteNoise, 3, 3, 10, 0); repair.ReconstructLines(ciLineVertAndHorz); end; procedure repair_image (const fileName: string; const page: integer; const fileOut: string); var Ci: ICiServer; repair: ICiRepair; begin try try begin //Create repair Ci:=CoCiServer.Create; repair:=Ci.CreateRepair; repair.Image.Open(fileName, page); // Do repair RepairPage(repair); // Save results repair.Image.SaveAs(fileOut, ciExt); end except on E:Exception do //Process errors ShowMessage(Format('Error:%s.File:%s', [E.Message,FileName])); end finally end end;
VBScript/ASP
12345678910111213141516171819202122232425262728293031323334353637383940414243
Sub RepairPage (repair) On Error Resume Next repair.AutoDeskew If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub repair.AutoRotate If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub repair.AutoCrop 10, 10, 10, 10 ' Crop to 10 pixels on each side to bitonal If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub repair.AdvancedBinarize 0, 0 , 0 ' Convert to bitonal an image with complex background patterns If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ciBexBorderDeskewCrop = 3 ciBeaCleaner = 2 repair.BorderExtract ciBexBorderDeskewCrop, ciBeaCleaner ' Deskew and crop based on black border If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub repair.RemovePunchHoles If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ciSmoothDarkenEdges = 1 repair.SmoothCharacters ciSmoothDarkenEdges If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub repair.CleanNoise 3 ' Clean balck noise of 3 pixels ' ciCnxBlackNoise = 1: ciCnxWhiteNoise = 2 ' repair.CleanNoiseExt (ciCnxBlackNoise + ciCnxWhiteNoise), 3, 3, 10 ' Clean black and white noise ciLineVertAndHorz = 3 repair.ReconstructLines ciLineVertAndHorz If Err.Number <> 0 Then WScript.Echo Err.Description : Exit SubEnd Sub Sub repair_image(fileName, page, fileOut) On Error Resume Next 'Create CiServer object: Set Ci = CreateObject("ClearImage.ClearImage") ' Create and configure repair Set repair = Ci.CreateRepair If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ' Open Image repair.Image.Open fileName, page If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ' Do repair RepairPage repair ' Save results repair.Image.SaveAs fileOut, 0 ' ciEXT If Err.Number <> 0 Then WScript.Echo Err.Description : Exit SubEnd Sub
Repair images in multi-page file
C#
1234567891011121314151617
using Inlite.ClearImageNet;// . . . private static void _OnEditPage(object sender, EditPageEventArgs e){ RepairPage(e.Editor);} internal static void Repair_file(string fileName, string fileOut){ try { ImageEditor editor = new ImageEditor(); bool ret = editor.Edit(fileName, _OnEditPage, fileOut, ImageFileFormat.outputFileExtension, true); } catch (Exception ex) { ProcessException(ex); }}
VB
1234567891011121314
Imports Inlite.ClearImageNet' . . . Sub _OnEditPage(sender As Object, e As EditPageEventArgs) RepairPage(e.Editor)End Sub Sub Repair_file(fileName As String, fileOut As String) Try Dim editor As New ImageEditor() Dim ret As Boolean = editor.Edit(fileName, AddressOf _OnEditPage, fileOut, ImageFileFormat.outputFileExtension, True) Catch ex As Exception ProcessException(ex) End TryEnd Sub
C++
1234567891011121314151617181920
void repair_file (const char *fileName, const char *fileOut) { try { // configure reader ICiRepairPtr repair = Ci->CreateRepair (); int page = 1; while (true) { // open file page repair->Image->Open (_bstr_t(fileName), page); // process page RepairPage(repair); // Save page repair->Image->Append(_bstr_t(fileOut), ciEXT); // go to the next page page++; if (page > repair->Image->PageCount) break; } } catch (_com_error &ex) {ProcessException(ex);} }
Java
1234567891011121314151617181920212223
void repair_file (String fileName, String fileOut) { try { // configure reader ICiRepair repair = Ci.CreateRepair (); int page = 1; while (true) { // open file page repair.getImage().Open (fileName, page); // process page RepairPage(repair); // Save page repair.getImage().Append(fileOut, EFileFormat.ciEXT); // go to the next page page++; if (page > repair.getImage().getPageCount()) break; } } catch (Exception ex) { ProcessException(ex); } finally { // Collect garbage periodically to release JNI-managed // objects System.gc(); } }
PHP
1234567891011121314151617181920
function repair_file ($fileName, $fileOut){ // Create CiServer object: $Ci = new COM("ClearImage.ClearImage"); // Create and configure repair $repair = $Ci->CreateRepair(); $page=1; while(true) { // Open image file $repair->Image->Open($fileName, $page); // Do repair RepairPage ($repair); // Save results $repair->Image->Append($fileOut, 0); // ciEXT $page++; if ($page > $repair->Image->PageCount) break; }}
Delphi
12345678910111213141516171819202122232425262728293031323334
uses ...,ClearImage_TLB, comobj; // . . . procedure repair_file (const fileName: string; const fileOut: string); var Ci: ICiServer; repair: ICiRepair; page: integer; begin try try begin //Create repair Ci:=CoCiServer.Create; repair:=Ci.CreateRepair; page:=1; while true do begin repair.Image.Open(fileName, page); // Do repair RepairPage(repair); // Save results repair.Image.Append(fileOut, ciExt); page := page + 1; if (page > repair.Image.PageCount) then Break; end; end except on E:Exception do //Process errors ShowMessage(Format('Error:%s.File:%s', [E.Message,FileName])); end finally end end;
VBScript/ASP
123456789101112131415161718192021
Sub repair_file(fileName, fileOut) On Error Resume Next 'Create CiServer object: Set Ci = CreateObject("ClearImage.ClearImage") ' Create and configure repair Set repair = Ci.CreateRepair If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub page = 1 do ' Open Image repair.Image.Open fileName, page If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub ' Do repair RepairPage repair ' Save results repair.Image.Append fileOut, 0 ' ciEXT If Err.Number <> 0 Then WScript.Echo Err.Description : Exit Sub page = page + 1 if (page > repair.Image.PageCount) Then Exit Do LoopEnd Sub
Repair images in a stream
C#
123456789101112131415161718
using Inlite.ClearImageNet;// . . . void _OnEditPage(object sender, EditPageEventArgs e){ RepairPage(e.Editor);} Stream Repair_stream(Stream stream, ImageFileFormat output_format){ try { ImageEditor repair = new ImageEditor(); MemoryStream msOut = repair.Edit(stream, _OnEditPage, output_format); return msOut; } catch (Exception ex) { ProcessException(ex); return null;}}
VB
12345678910111213141516
Imports Inlite.ClearImageNet' . . . Sub _OnEditPage(sender As Object, e As EditPageEventArgs) RepairPage(e.Editor)End Sub Function Repair_stream(stream As Stream, output_format As ImageFileFormat) As Stream Try Dim repair As New ImageEditor() Dim msOut As MemoryStream = repair.Edit(stream, AddressOf _OnEditPage, output_format) Return msOut Catch ex As Exception ProcessException(ex) Return Nothing End TryEnd Function