Saturday, March 14, 2015

OCR for Win8



Noticing the Trend for mobile/ tablet applications, every organization is going for a mobile version of its app or website. If you are going for an application that involves image processing, one of the most common and needed requirement is for OPTICAL CHARACTER RECOGNITION. Now as win 8 being new, implementing OCR with win8 app may be the need in many of your applications.
LEADTOOLS help us to achieve this in a simplest way. It is a free SDK available for win 8. Any Windows Store application can easily be created with the help of LEAD Technologies imaging SDKs. The LEADTOOLS Optical Character Recognition SDK provides native WinRT libraries that can run on any mobile, tablet or desktop device. They Can help you convert any image to pdfs, plain text etc or implement your custom logic.

Features of Leadtools WINRT
· Win32, x64 and ARM, Native WinRT binaries
· Develop Windows Store applications that target any Windows 8 desktop, tablet or mobile device
· Image viewer controls designed specifically for WinRT and Windows Store apps
o  Compatible with Expression Blend
o  Supports both mouse and multi-touch gesture input
o  Built-in interactive modes such as pan, scale, pinch and zoom, magnifying glass and more
o  Automatically scale images to fit, fit width and stretch to the control size
· Load, convert and save more than 150 image formats
·      Advanced bit depth, color space and compression support for common formats including PDF, PDF/A, JPEG, JPEG 2000, TIFF, JBIG2 and more OCR Features

OCR Features for WinRT
·      Fast, accurate and reliable optical character recognition for use in any application or environment
·      Choose from several built-in and custom dictionaries to improve OCR results
·      Recognize text from over 30 languages and character sets including English, Spanish, French, German, Japanese, Chinese, Arabic and more
·      Automatically detect the document's language
·      Full page analysis and Zonal recognition
·      Unique color and bitonal image recognition
·      Automated document image cleanup



Functional Code
In this example we will be allowing user to load an image and then we will recognize the characters in that and return the text recognized.

Step 1 Create a win 8 metro app and add reference to the LEADTOOLS binaries for win8.

Step 2 Initialize the LEADTOOLS OCR engine and preparing a document.
// Create an instance of the engine
string strEngineDirectory = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, @"OCR");
_ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false);
_ocrEngine.Startup(null, null, string.Empty, strEngineDirectory);
// Create the OCR document
_ocrDocument = _ocrEngine.DocumentManager.CreateDocument();
Step3 Load an image and add it as a page to our document.
// Show the file picker
var picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.ViewMode = PickerViewMode.List;
foreach (var imageFormat in _imageFormats)
picker.FileTypeFilter.Add(imageFormat.Extension);
var file = await picker.PickSingleFileAsync();
if (file == null)
return;
// Create a LEADTOOLS stream from the file
ILeadStream leadStream = LeadStreamFactory.Create(file);
// Get the RasterCodecs object to load the image from the OCR engine
RasterCodecs codecs = _ocrEngine.RasterCodecsInstance;
// Load the image (first page only)
RasterImage rasterImage = await codecs.LoadAsync(leadStream, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1);
// Add it to the OCR engine
// Check if we have previous pages, remove them
_ocrDocument.Pages.Clear();
_ocrPage = _ocrDocument.Pages.AddPage(rasterImage, null);

Snapshot displaying image Loaded



Step 4 OCR to Text
Converting an image to raw text is very easy with the RecognizeText function which returns the results as a string object.
// Auto-zone the page
_ocrPage.AutoZone(null);
// Recognize the page and get the results as text
TextResults.Text = _ocrPage.RecognizeText(null);

Snapshot displaying image to text conversion



Hope you will find this useful.
For any queries you can contact me at saurabhpahuja@yahoo.co.in
Warm Regards,
Saurabh Pahuja


Wednesday, January 7, 2015

Creating a Hello World Widget in DOJO

WHY DOJO

As there are numerous quality  toolkits available for JavaScript  and a number of other JavaScript toolkits of vivid quality then   why should you go for any one particular , specially DOJO.
Here are the reasons why you should go for dojo:-

AMD and Modular
Now as JavaScript at client side is growing in size in every web application , having AMD and modularity will help us in making    the applications faster in performance and easier in      maintenance. Using one large JavaScript library without any asynchronous   loading is no longer a good practice now.
Dojo has been implementing functionality in Modules for years
 now, and requiring only the necessary modules using Define and Require helps in making quick applications.
Eg
define([
"dijit/_WidgetBase",
"dijit/_TemplatedMixin",
"dojo/_base/declare",
"dojo/dom-attr"
],
function(_Widget, _Templated, declare, domAttr){
}  

Using dojo/declare for implementing Classes and hence providing Extensibility

A true class system is not provided by JavaScript, but a class inheritance system is provided by the Dojo Toolkit with the   help of dojo/declare module. Declare is very useful as with the help of it programmers can:
• Remove redundant code
• Extend an existing functionality
• Provide better customization
• Easily share existing module code

Dojo’s UI Framework:-Dijit
Dijit, Dojo’s UI Framework, is Dojo Toolkit’s X-factor over 
other Java Script frameworks. It has a lot of form and other 
tools. It boasts of:-

• Out of the box feature for localization
• complete support for accessibility
• Easy customization
• Complete modularity in existing widgets
• Facility for custom theming

Dojo Objective Harness(DOH): Testing Frame work of DOJO
Testing is a very important aspect in any application. Dojo Objective Harness , Dojo Toolkit’s testing framework, is 
 included in each Dojo version download. With DOH writing Tests is very easy, plus you can write tests in different formats as well.

INTRO
In this doc, you'll learn how to create a basic hello world 
custom widget. We will be using pieces of dojo and dijit 
 framework to create our own custom widgets, mainly we will 
use dijit/_WidgetBase and dijit/_TemplatedMixin in widget creation.We will be using DIJIT framework which comes with 
dojo toolkit. DIJIT is basically a collection of controls 
called widgets. we will cover this process in steps:-
Step 1) First of all include dojo library in your project.







Step 2) Include DOJO library in your parent HTML page where you will be placing your widget.

<script src=" ./dojo/dojo.js" data-dojo-config="async:true"></script> is called the loader statement and is used to load
 the dojo library in your page. Setting data-dojo-config="async:true" allows you load the dojo library 
 asynchronously on your page.




Step 3) Create a HelloWorld.js file defining the widget, that is how the the widget
 would look like and what would be its behaviour.

define([
"dijit/_WidgetBase",
"dijit/_TemplatedMixin",
"dojo/_base/declare",
"dojo/dom-attr"
],
function(_Widget, _Templated, declare, domAttr){
return declare("myWidgets.HelloWorld", [_Widget, _Templated],{
templateString: '<div data-dojo-attach-point="_box"></div>',
postCreate: function(){
domAttr.set(this._box, "innerHTML", "Hello, World!");
}
});
});

Define is used to tell to dojo that what all modules we will
 be requiring in our widget definition. Here first we define
"dijit/_WidgetBase","dijit/_TemplatedMixin","dojo/_base/declare",  "dojo/dom-attr" and have their corresponding argument being passed to the callback function.
Inside the call back function we are declaring a class:-
• the first parameter of class depicts: class name
• the second parameter of class depicts: modules dependency
• the third parameter of class depicts: class definition.
Now in the class definition first we specify the template 
String property , which decides how your widget would look
 like.
That is it contains the template for your widget. The 
data-dojo-attach-point property is used to have an attach point
 that is how you will point to any node inside your template.
Now you will override the postCreate function , and inside that   you will use domAttr(reference or obj of dojo/dom-attr) 
module’s “set” method to specify what should be displayed 
 inside your widget.

Step 4) Include the above created widget on your HTML page.

<body>
<script>
require(["dojo", "dojo/dom", "myApp/widget/HelloWorld"],
function(dojo, dom, HelloWorld){
var helloWorld = new HelloWorld();
helloWorld.placeAt(dom.byId("test"));
helloWorld.startup();
}
);
</script>
<div id="test"></div>
</body>


The above code shows how to include custom “Hello World” widget 
on your page.
Require works similar to define. Here first you require all the modules you want to load your widget on to page.
The third module mentioned here is our custom hello world widget, notice the path for mapping to exact location.
Now in the callback function:
• First you create an instance of HelloWorld widget
• Then we use placeAt function to place it at desired location.
• Third we call the startUp() function on the widget instance
Below is the snapshot of the widget.





After doing the above steps you have a bare bone widget now you
 can add any functionality as per your requirements.

In case of any Query , please reach out to me at:- saurabhpahuja@yahoo.co.in
Regards
Saurabh Pahuja 
Sr Developer