Hello, world!

In this article, you will learn how to build a basic web page that requests an image file from an user and extracts the text from it with with Aspose.OCR for JavaScript via C++.

You will need

  • A compatible web browser.
  • Any text editor.
  • Web server to overcome CORS limitations. In this article, we will use Python 3 to run the server.
  • Some image containing a text. You can simply download the one from this article.
  • 15 minutes of spare time.

Preparing

  1. Create a directory somewhere on your system where the project files will be kept. For example, C:\Aspose-JS-Example\.
    This directory will later be referred as project directory.

  2. Download Aspose.OCR for JavaScript via C++ and extract it somewhere on your system.

  3. Copy the files from lib directory of the extracted Aspose.OCR for JavaScript via C++ package to the the project directory:

    • asposeocr.js
    • asposeocr.wasm
    • asposeocr.data
  4. Download a sample image:

    Source image

Coding

  1. Create a simple index.html file in the the project directory.

    <!doctype html>
    <html>
       <head>
          <title>Aspose.OCR for JavaScript via C++</title>
       </head>
       <body>
       </body>
    </html>
    
  2. Add an input field in which the user can upload an image file and an element to display recognition results to the page body:

    <p><b>Provide image file:</b></p>
    <input type="file" id="sourceFile">
    <p><b>Log:</b></p>
    <div id="outputConsole" style="border: solid 1px #cccccc; padding: 10px;"></div>
    
  3. Reference Aspose.OCR for JavaScript via C++ main script:

    <script src="asposeocr.js"></script>
    
  4. Add a custom JavaScript for image processing:

    <script>
    var Module = {
       onRuntimeInitialized: function()
       {
          // Initialize interface elements
          const sourceFile = document.getElementById("sourceFile");
          sourceFile.addEventListener("change", handleImgUpload);
          const outputConsole = document.getElementById("outputConsole");
          log("Select an image to continue...");
    
          // Recognition
          function handleImgUpload(event)
          {
             // Load image file provided by the user
             const file = event.target.files[0];
             log(`Source image: ${file.name}`);
             const reader = new FileReader();
             reader.onload = function(e){
                const fileData = new Uint8Array(e.target.result);
                let filename = file.name;
                let stream = Module.FS.open(filename, "w+");
                Module.FS.write(stream, fileData, 0, fileData.length, 0);
                Module.FS.close(stream);
                // Initialize recognition engine
                var input = Module.WasmAsposeOCRInput();
                var inputs = new Module.WasmAsposeOCRInputs();
                var settings = Module.WasmAsposeOCRRecognitionSettings();
                // Send image file for recognition
                input.url = filename;
                inputs.push_back(input);
                var result = Module.AsposeOCRRecognize(inputs, settings);
                // Get recognition results as text
                var result_str = Module.AsposeOCRSerializeResult(result, Module.ExportFormat.text);
                // Show recognition results
                log("Recognition result:");
                log(result_str);
             };
             reader.readAsArrayBuffer(file);
             log("Recognition started, please wait...");
          }
    
          // Logging
          function log(text)
          {
             outputConsole.innerHTML += `<p>${text}</p>`;
          }
       }
    };
    </script>
    

Full listing

<!doctype html>
<html>
   <head>
      <title>Aspose.OCR for JavaScript via C++</title>
   </head>
   <body>
      <p><b>Provide image file:</b></p>
      <input type="file" id="sourceFile">
      <p><b>Log:</b></p>
      <div id="outputConsole" style="border: solid 1px #cccccc; padding: 10px;"></div>
      <script>
      var Module = {
         onRuntimeInitialized: function()
         {
            // Interface
            const sourceFile = document.getElementById("sourceFile");
            sourceFile.addEventListener("change", handleImgUpload);
            const outputConsole = document.getElementById("outputConsole");
            log("Select an image to continue...");

            // Recognition
            function handleImgUpload(event)
            {
               const file = event.target.files[0];
               log(`Source image: ${file.name}`);
               const reader = new FileReader();
               reader.onload = function(e){
                  const fileData = new Uint8Array(e.target.result);
                  let filename = file.name;
                  let stream = Module.FS.open(filename, "w+");
                  Module.FS.write(stream, fileData, 0, fileData.length, 0);
                  Module.FS.close(stream);
                  var input = Module.WasmAsposeOCRInput();
                  var inputs = new Module.WasmAsposeOCRInputs();
                  var settings = Module.WasmAsposeOCRRecognitionSettings();
                  input.url = filename;
                  inputs.push_back(input);
                  var result = Module.AsposeOCRRecognize(inputs, settings);
                  // Get recognition results as text
                  var result_str = Module.AsposeOCRSerializeResult(result, Module.ExportFormat.text);
                  log("Recognition result:");
                  log(result_str);
               };
               reader.readAsArrayBuffer(file);
               log("Recognition started, please wait...");
            }

            // Logging
            function log(text)
            {
               outputConsole.innerHTML += `<p>${text}</p>`;
            }
         }
      };
      </script>
      <script src="asposeocr.js"></script>
   </body>
</html>

Running

  1. Open the command prompt and navigate to the project directory.

  2. Run the HTTP server using Python: python -m http.server 8080

    If the port 8080 is already occupied, provide a different one.

  3. Open http://localhost:8080/ with a web browser.

  4. Select an image and wait for recognition to complete.

You will see extracted text inside the outputConsole DIV element:

Select an image to continue...

Source image: source.png

Recognition started, please wait...

Recognition result:

Hello. World! I can read this text

What’s next

Congratulations! You have performed OCR on an image and extracted the machine-readable text from it directly from a web page. Read the Developer reference for details on developing advanced applications with Aspose.OCR for JavaScript C++.