LEADTOOLS Support
Document
Document SDK Examples
HOW TO: HTML5 MVC Simple Drivers License Demo
#1
Posted
:
Monday, May 8, 2017 2:29:42 PM(UTC)
Groups: Manager, Tech Support, Administrators
Posts: 218
Was thanked: 12 time(s) in 12 post(s)
Attached is an ASP.NET MVC Simple Drivers License demo written in Visual Studio 2017 C#.
The HTML is simply a form with a File input that lets the client upload an image. When you submit the form it calls a HttpPost MVC Controller that will take the uploaded file as a stream and pass that to a method to extract the drivers license fields.
To run this project, put your Evaluation (or release) license in the LEADTOOLS root folder and rename it to 'LEADTOOLS.LIC' and copy/paste your developer key into the ExtractDriversLicense method:
Code:string lic = HostingEnvironment.MapPath("~/LEADTOOLS/") + "LEADTOOLS.LIC";
string key = "DEVELOPER KEY";
RasterSupport.SetLicense(lic, key);
Here is the HTML:
Code:<div class="container" id="container">
<div class="row">
<div class="col-md-6">
<form>
<div class="form-group" id="uploadDiv">
<label style="cursor: pointer" for="fileUpload">File Input</label>
<input style="cursor: pointer" type="file" id="file" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">Please choose the file to upload</small>
</div>
<input type="button" class="btn btn-primary" value="Upload Drivers License" onclick="Recognize()">
</form>
</div>
</div>
<div class="row" style="display: none;" id="results">
<h3>JSON Results:</h3>
</div>
</div>
Here is the JavaScript Recognize Method with the AJAX call:
Code:function Recognize(){
var fd = new FormData();
var file = $("#file");
fd.append('formFieldName', file[0].files[0]);
var posturl = "http://localhost:24940/Recognition/RecognizeImage?id=123";
$.ajax({
url: posturl,
method: 'post',
data: fd,
processData: false,
contentType: false
}).done(function (data) {
var response = JSON.stringify(data, undefined, 2);
$("#json").remove();
$("#results").append("<pre class='col-md-6' id='json'><code><xmp>" + response + "</xmp></code></pre>");
$("#results").show();
}).fail(function (jqXHR, textStatus) {
alert('error');
}).always(function () {
$("body").css("cursor", "default");
$('form').trigger("reset");
});
}
Here is the MVC Controller:
[HttpPost]
Code:public ActionResult RecognizeImage()
{
RecognitionResponse response = new RecognitionResponse();
if (Request.Files != null && Request.Files.Count != 0)
using (Stream stream = Request.Files[0].InputStream)
response = ExtractDriverLicense(response, stream);
else
response.Status = HttpStatusCode.NotAcceptable;
return Json(response);
}
Here is the Extract Drivers License method:
Code:private RecognitionResponse ExtractDriverLicense(RecognitionResponse response, Stream stream)
{
try
{
string lic = HostingEnvironment.MapPath("~/LEADTOOLS/") + "LEADTOOLS.LIC";
string key = "DEVELOPER KEY";
RasterSupport.SetLicense(lic, key);
using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false))
{
string ocrEngineFiles = HostingEnvironment.MapPath("~/OCRRuntimefiles/");
ocrEngine.Startup(null, null, null, ocrEngineFiles);
string dlDirectory = HostingEnvironment.MapPath("~/DriversLicenseForms/");
IMasterFormsRepository driversLicenseRepository = new DiskMasterFormsRepository(ocrEngine.RasterCodecsInstance, dlDirectory);
StringBuilder sb = new StringBuilder();
Dictionary<string, string> dlResults = new Dictionary<string, string>();
using (RasterCodecs codecs = new RasterCodecs())
using (RasterImage image = codecs.Load(stream))
using (AutoFormsEngine engine = new AutoFormsEngine(driversLicenseRepository, ocrEngine, null, AutoFormsRecognitionManager.Ocr, 30, 80, true))
{
AutoFormsRunResult runResult = engine.Run(image, null, null, null);
foreach (FormPage formPage in runResult.FormFields)
foreach (FormField field in formPage)
if ((field as TextFormField) != null)
dlResults.Add(field.Name, (field.Result as TextFormFieldResult)?.Text.Replace(Environment.NewLine, ""));
}
response.DriverLicenseResults = dlResults;
}
return response;
}
catch (Exception ex)
{
response.Status = HttpStatusCode.InternalServerError;
return response;
}
}
![UserPostedImage](https://www.leadtools.com/support/publicforumimages/mvcdeiverslicensedemo.png)
Hadi Chami
Developer Support Manager
LEAD Technologies, Inc.
![LEAD Logo LEAD Logo](https://www.leadtools.com/images/new-site-images/leadtools-logo-header.png)
LEADTOOLS Support
Document
Document SDK Examples
HOW TO: HTML5 MVC Simple Drivers License Demo
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.