Click here to Skip to main content

OCR using Silverlight 5

Introduction

It was impossible to implement the OCR module of Document Management System which is capable of scanning the local documents( Eg: passport) of user and extract the text , in ASP.NET alone. So I implemented an RIA(Rich Internet Application) using Silverlight, WCF, MODI (Microsoft Office Document Imaging) . I would like to share the idea for who seeking the same.

If you are going to implement the exact MRZ application, take a look at along with this article

http://en.wikipedia.org/wiki/Machine_Readable_Zone

Background

Pre-requisists

  • Visual studio 2010-SP1
  • Silverlight 5 developer tools
  • MS Office 2007

By default MODI is disabled in MS Office. Please enable it from

  1. Add or remove programms-> Microsoft Office 2007->Change
  2. Add or remove features-> Office tools
  3. Microsoft office document imaging-> Scanning, OCR and Indexing service filter .

To make your application possible to access scanner device of client machine from browser, your application must be 'trusted one'. To make the application trusted, follow these

Application must be 'Runnable out of browser'

  • Go to properties of silverlight application and check the properties.

  • Edit the registry using regedit.exe in client machine
  • Sign the .xap file

To enable in-browser trusted applications

  1. Configure the target computers to allow trusted applications inside the browser by setting the following registry key:
    • Key path for 32-bit computers: HKEY_LOCAL_MACHINE\Software\Microsoft\Silverlight\
    • Key path for 64-bit computers: HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Silverlight\
    • Value name: AllowElevatedTrustAppsInBrowser
    • Value type: DWORD
    • Valid Values:
      • Disabled - 0x00000000
      • Enabled - 0x00000001
  2. Ensure that you do not set conflicting registry keys. For more information, see the AllowInstallOfElevatedTrustApps and AllowLaunchOfElevatedTrustApps registry keys in Group Policy Settings. Specifically, you can disallow the installation of trusted applications without affecting their in-browser use. However, disallowing the launch of trusted applications will cause them to run in partial trust, even inside the browser.
  3. Sign the .xap files with a valid, code-signing certificate. For more information, see the "Application Signing" section of Trusted Applications.
  4. Install the certificate to the Trusted Publishers certificate store and (if necessary) the Trusted Root Certification Authorities store. For more information, see Deploying Certificates to the Trusted Publishers Store.

    Visit for more detals: http://msdn.microsoft.com/en-us/library/gg192793(v=vs.96).aspx

Using the code

The web.config file of host application is edited to increase the buffer size.

//
<?xml version="1.0"?>
 
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
 
<configuration>
    <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
 
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>      
      <basicHttpBinding>
        <binding name ="ServicesBinding" 
             maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
          <readerQuotas maxArrayLength ="2147483647" maxBytesPerRead="2147483647" 
            maxDepth ="2147483647" maxNameTableCharCount ="2147483647" 
            maxStringContentLength ="2147483647"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
        multipleSiteBindingsEnabled="true" />
    <services>
      <service name="SilverlightApplication1.Web.Service1">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="ServicesBinding"
            contract="SilverlightApplication1.Web.Service1" >
          <identity>
            <dns value ="localhost"/>
          </identity>
        </endpoint >
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

Acquire image from scanner in silverlight page (Not that, this is unmanaged code)

using (dynamic CommonDialog = AutomationFactory.CreateObject("WIA.CommonDialog"))
{
   
        dynamic imageFile = CommonDialog.ShowAcquireImage(
        1,
        2,
        65536,
      "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}", false, true, false);}

The following WCF method extracts text from image using MODI

[OperationContract]
public String doOCR(byte[] image)
{
    FileStream fileStream = null;
    BinaryWriter writer = null;
    string filePath;
    try
    {
        filePath = HttpContext.Current.Server.MapPath(@"Images\temp.jpg");             
        MemoryStream ms = new MemoryStream(image);
        Image im = Image.FromStream(ms);
        im.Save(filePath);
        MODI.Document md = new MODI.Document();
        md.Create(filePath);
        md.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true);
        MODI.Image img = (MODI.Image)md.Images[0];            
        return img.Layout.Text;                
              
    }
    catch (Exception ex)
    {
        return ex.InnerException.ToString();
    }
    finally
    {
        if (fileStream != null)
            fileStream.Close();
        if (writer != null)
            writer.Close();
    }   
}

Points of Interest

I really enjoy to share what I learned from others.


Web02 | 2.8.160208.1 | Advertise | Privacy
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service