Introduction
As Internet connection speed getting faster, more and more people tend to embrace cloud service. Any desktop applications could be transformed to cloud applications with better maintainability, security, and stability. End-users can use web browsers to get connected with social friends, store tons of personal data, watch live video, edit documents, publish blog posts and even write code. In this article, we will take a glimpse of how to make Dynamic Barcode Reader SDK work with web technologies. You will see a cloud-based barcode scanner that consists of a webcam, HTML5-supported web browser, IIS web server, and .NET barcode reader SDK.
Why Dynamsoft Barcode Reader
Supported 1D/2D Barcode Types
- 1D: Code 39, Code 93, Code 128, Codabar, EAN-8, EAN-13, UPC-A, UPC-E, Interleaved 2 of 5 (ITF), Industrial 2 of 5 (Code 2 of 5 Industry, Standard 2 of 5, Code 2 of 5), ITF-14
- 2D: QRCode, DataMatrix, and PDF417
Supported Image Types
- BMP, JPEG, PNG, GIF, TIFF, and PDF
Detected Barcode Information
- Barcode type
- Barcode count
- Barcode value as string
- Barcode raw data as bytes
- Barcode bounding rectangle
- Coordinate of four corners
- Page number
Features
- Accurate: reads barcodes within a specified area of a selected image.
- Effective: reads multiple barcodes in one image.
- Powerful: reads poor quality and damaged barcodes.
- Flexible: detects barcode at any orientation and rotation angle.
Supported Programming Languages
- C#, VB.net, Java, C++, PHP, JavaScript, Python and etc.
Implementation of Webcam Barcode Scanner
Testing environment
Microsoft Edge, Firefox, Chrome, and Opera.
The Anatomy of Sample Code
- Open Visual Studio to create a new web project.
- Click References to import Dynamsoft.BarcodeReader.dll.
- Create a C# class
BarrecodeReaderRepo
to handle barcode detection on the server-side:
using System;
using System.Drawing;
using System.IO;
using Dynamsoft.Barcode;
namespace BarcodeDLL
{
public class BarrecodeReaderRepo
{
private static readonly char CSep = Path.DirectorySeparatorChar;
private static readonly string StrPath = AppDomain.CurrentDomain.BaseDirectory + "Images";
private static readonly string CollectFolder = StrPath + CSep + "Collect";
public static string Barcode(string strImgBase64, Int64 iformat, int iMaxNumbers, ref string strResult)
{
if (string.IsNullOrEmpty(strImgBase64.Trim())) throw new Exception("No barcode exist.");
return DoBarcode(strImgBase64, iformat, iMaxNumbers, ref strResult);
}
private static string DoBarcode(string strImgBase64, Int64 format, int iMaxNumbers, ref string strResult)
{
strResult = "";
var strResturn = "[";
var listResult = GetBarcode(strImgBase64, format, iMaxNumbers);
if (listResult == null || listResult.Length == 0)
{
strResult = "No barcode found. ";
return "[]";
}
strResult = "Total barcode(s) found: " + listResult.Length + "";
var i = 1;
foreach (var item in listResult)
{
strResult = strResult + "  Barcode " + i + ":";
strResult = strResult + "    Type: " + item.BarcodeFormat + "";
strResult = strResult + "    Value: " + item.BarcodeText + "";
strResult = strResult + "    Region: {Left: " + item.ResultPoints[0].X
+ ", Top: " + item.ResultPoints[0].Y
+ ", Width: " + item.BoundingRect.Width
+ ", Height: " + item.BoundingRect.Height + "}";
i++;
}
strResturn = strResturn.Substring(0, strResturn.Length - 1);
strResturn += "]";
return strResturn;
}
public static BarcodeResult[] GetBarcode(string strImgBase64, Int64 format, int iMaxNumbers)
{
var reader = new Dynamsoft.Barcode.BarcodeReader();
var options = new ReaderOptions
{
MaxBarcodesToReadPerPage = iMaxNumbers,
BarcodeFormats = (BarcodeFormat) format
};
reader.ReaderOptions = options;
reader.LicenseKeys = "693C401F1CC972A511F060EA05D537CD";
return reader.DecodeBase64String(strImgBase64);
}
}
}
- Create a Generic Handler WebcamBarcodeReader.ashx to process the HTTP request. Parse stream to get the base64 string, and then pass it to
BarrecodeReaderRepo
:
public class WebcamBarcodeReader : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
context.Request.InputStream.Position = 0;
string jsonString;
using (var inputStream = new StreamReader(context.Request.InputStream))
{
jsonString = inputStream.ReadToEnd();
}
var postData = JsonConvert.DeserializeObject<PostData>(@jsonString);
if (postData == null) return;
var strResult = "";
BarrecodeReaderRepo.Barcode(postData.Base64Data, postData.BarcodeType, 10, ref strResult);
if (strResult.IndexOf("No barcode found", StringComparison.Ordinal) == -1)
{
strResult = "|" + strResult;
}
strResult += DateTime.Now;
context.Response.Write(strResult);
}
catch (Exception exp)
{
context.Response.Write("Error: " + exp.Message);
}
}
public bool IsReusable
{
get
{
return true;
}
}
public class PostData
{
public string Base64Data { get; set; }
public int BarcodeType { get; set; }
}
}
- Create an element
<video></video>
. Open a webcam in a web page with the HTML5 API getUserMedia
:
function toggleCamera() {
var videoSource = videoSelect.value;
var constraints = {
video: {
optional: [{
sourceId: videoSource
}]
}
};
startCamera(constraints);
}
function successCallback(stream) {
window.stream = stream;
videoElement.src = window.URL.createObjectURL(stream);
videoElement.play();
}
function errorCallback(error) {
console.log("Error: " + error);
}
function startCamera(constraints) {
if (navigator.getUserMedia) {
navigator.getUserMedia(constraints, successCallback, errorCallback);
} else {
console.log("getUserMedia not supported");
}
}
- How to send preview images to the remote server? Keep drawing frames on a hidden canvas. Convert the canvas data to
base64
string and send it in Ajax. If there is nothing detected, use the method setTimeout
to send the preview data for detection again:
context.drawImage(videoElement, 0, 0, width, height);
var base64 = dbrCanvas.toDataURL('image/png', 1.0);
var data = base64.replace(/^data:image\/(png|jpeg|jpg);base64,/, "");
var imgData = JSON.stringify({
Base64Data: data,
BarcodeType: getBarcodeFormat()
});
$.ajax({
url: 'WebcamBarcodeReader.ashx',
dataType: 'json',
data: imgData,
type: 'POST',
complete: function (result) {
console.log(result);
if (isPaused) {
return;
}
var barcode_result = document.getElementById('dbr');
var aryResult;
aryResult = result.responseText.split('|');
if (result.responseText.indexOf("No barcode") == -1) {
resetButtonGo();
barcode_result.innerHTML = result.responseText.substring(aryResult[0].length + 1);
}
else {
barcode_result.innerHTML = result.responseText;
setTimeout(scanBarcode, 200);
}
}
});
Online Demo
Try it now!
Get SDK and Samples
Interested in the above-mentioned webcam barcode scanner demo? Download the 30-day free trial of Dynamic Barcode Reader to build your barcode applications.
Dynamic Barcode Reader 30-day Free Trial
For more samples about how to use Dynamic Barcode Reader SDK, please visit the following page:
Dynamic Barcode Reader Demos
One More Thing
Dynamsoft Barcode Reader SDK is not only available for Windows, but also for Linux and Mac OS X. You can have fun with it on your favorite platform. If you have any questions, please feel free to contact support@dynamsoft.com.