VintaSoft Barcode .NET SDK v10.0
Read barcodes in ASP.NET MVC

All resource-intensive operations in ASP.NET MVC application are performed asynchronously, so the barcode recognition should be also performed asynchronously.

1. Asynchronous barcode recognition

The process of asynchronous barcode recognition can be divided into the following steps:
  1. Client-side: ASP.NET MVC application sends an asynchronous request to a web service
  2. Server-side: The web service recognizes barcodes and sends the results to the application
  3. Client-side: The application processes the barcode recognition results

Client-side of ASP.NET MVC application

The WebBarcodeReaderJS class, from Vintasoft.Barcode.js file, can be used on a client-side of ASP.NET MVC application for barcode recognition.
The WebBarcodeReaderJS class allows to:
Important: JQuery (version 1.11 or higher) and Vintasoft.Shared.js file are necessary for correct work of Vintasoft.Barcode.js file.

Example: Here is JavaScript example that demonstrates how to recognize Code39 barcodes from image:
// create service that allows to write barcode
var barcodeService = new Vintasoft.Shared.WebServiceControllerJS("/vintasoft/api/Barcode/");

// create the barcode reader
var barcodeReader = new Vintasoft.Barcode.WebBarcodeReaderJS(barcodeService);
// specify that Code39 barcode must be searched
barcodeReader.get_Settings().set_BarcodeType(new Vintasoft.WebBarcodeTypeEnumJS("Code39"));

// create web image, where barcodes must be recognized
var source = new Vintasoft.Shared.WebUrlImageSourceJS("/UploadedImageFiles/AllSupportedBarcodes.png");
var image = new Vintasoft.Shared.WebImageJS(source, 0);

// send an asynchronous request for barcode recognition
barcodeReader.readBarcodes(image, __readBarcodes_success, __readBarcodes_fail);


// Barcodes are recognized successfully.
function __readBarcodes_success(data) {
    if (data.success) {
        // parse the barcode recognition result
    }
    else {
        alert(data.errorMessage);
    }
}

// Barcode recognition is failed.
function __readBarcodes_fail(data) {
    // show information about error
    alert(data.responseJSON.ExceptionMessage);
}
        


Example: Here is JavaScript example that demonstrates how to recognize Code39, Code128, EAN13 and UPCA barcodes from image and obtain the results of print quality tests for all recognized barcodes:
// create service that allows to recognize barcodes
var barcodeService = new Vintasoft.Shared.WebServiceControllerJS("/vintasoft/api/Barcode/");

// create the barcode reader
var barcodeReader = new Vintasoft.Barcode.WebBarcodeReaderJS(barcodeService);
// specify that Code39, Code128, EAN13 and UPCA barcode must be searched
var barcodeTypesToSearch = new Vintasoft.Barcode.WebBarcodeTypeEnumJS("Code39");
barcodeTypesToSearch = barcodeTypesToSearch.add("Code128");
barcodeTypesToSearch = barcodeTypesToSearch.add("EAN13");
barcodeTypesToSearch = barcodeTypesToSearch.add("UPCA");
barcodeReader.get_Settings().set_BarcodeType(barcodeTypesToSearch);
// specify that the print quality test information must be collected for all recognized barcodes
barcodeReader.get_Settings().set_CollectTestInformation(true);

// create web image, where barcodes must be recognized
var source = new Vintasoft.Shared.WebUrlImageSourceJS("/UploadedImageFiles/AllSupportedBarcodes.png");
var image = new Vintasoft.Shared.WebImageJS(source, 0);

// send an asynchronous request for barcode recognition
barcodeReader.readBarcodes(image, __readBarcodes_success, __readBarcodes_fail);


// Barcodes are recognized successfully.
function __readBarcodes_success(data) {
    if (data.success) {
        // parse the barcode recognition result
    }
    else {
        alert(data.errorMessage);
    }
}

// Barcode recognition is failed.
function __readBarcodes_fail(data) {
    // show information about error
    alert(data.responseJSON.ExceptionMessage);
}
        


Server-side of ASP.NET MVC application

Depending on application architecture the web service, which recognizes barcodes, can be based on Web API 2 controller (ASP.NET MVC 5) or Web API controller (ASP .NET MVC 4).
SDK provides the following ready-to-use web services:
Important: Vintasoft.Barcode.dll, Vintasoft.Shared.dll, Vintasoft.Shared.Web.dll and Vintasoft.Barcode.Web.Services.dll assemblies are necessary for correct work of Vintasoft.Barcode.Web.Api2Controllers.dll or Vintasoft.Barcode.Web.ApiControllers.dll assembly.

Here is a code snippet that demonstrates how in ASP.NET MVC 4 application create Web API controller:
using Vintasoft.Barcode.Web.ApiControllers;

namespace AspNetMvcBarcodeDemos.Controllers
{
    // API controller that handles HTTP requests from clients and
    // allows to read barcodes from image generate barcode image.
    public class BarcodeController : VintasoftBarcodeApiController
    {

        public BarcodeController()
            : base()
        {
        }

    }
}
                

Here is a code snippet that demonstrates how in ASP.NET MVC 4 application register route to the created the Web API controller:
using System.Web.Http;

namespace AspNetMvcBarcodeDemos
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DemoAPI",
                routeTemplate: "vintasoft/api/{controller}/{action}"
            );
        }
    }
}
                


2. Demo applications

The AspNetMvc4BarcodeDemos project (can be found in distributive package of VintaSoft Barcode .NET SDK) contains the BarcodeReaderDemo, which demonstrates how to implement an asynchronous barcode recognition in ASP.NET MVC 4.
The AspNetMvc5BarcodeDemos project (can be found in distributive package of VintaSoft Barcode .NET SDK) contains the BarcodeReaderDemo, which demonstrates how to implement an asynchronous barcode recognition in ASP.NET MVC 5.