
Originariamente inviata da
Kahm
mi aspetto che se usato da cellulare, mi appaia la fotocamera e mi faccia scannerizzare qualcosa
esiste un prodotto del genere in HTML?
Esistono prodotti che ti consentono di prendere in input una immagine e ti restituiscono i/il codice a barre eventualmente trovato, il problema spesso è la qualità. In ogni caso fartelo da te non è impossibile ma nemmeno semplicissimo.
Lato client ovviamente non è necessario installare nulla se vuoi usare il browser, basta usare la webcam con html5, ma l'elaborazione dell'immagine dovrai farla lato server.
Per cui una cosa del genere per quello che riguarda lato client:
(codice preso dal web e rielaborato per questa occasione):
codice:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div class="camera">
<video id="video">Video stream not available.</video>
<button id="startbutton">Take photo</button>
</div>
<canvas id="canvas">
</canvas>
<div class="output">
<img id="photo" alt="The screen capture will appear in this box.">
</div>
<input id="output" />
<script>
var width = 320; // We will scale the photo width to this
var height = 0; // This will be computed based on the input stream
var streaming = false;
var video = null;
var canvas = null;
var photo = null;
var startbutton = null;
startup();
function startup() {
video = document.getElementById('video');
canvas = document.getElementById('canvas');
photo = document.getElementById('photo');
startbutton = document.getElementById('startbutton');
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then(function (stream) {
video.srcObject = stream;
video.play();
})
.catch(function (err) {
console.log("An error occurred: " + err);
});
video.addEventListener('canplay', function (ev) {
if (!streaming) {
height = video.videoHeight / (video.videoWidth / width);
video.setAttribute('width', width);
video.setAttribute('height', height);
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
streaming = true;
}
}, false);
startbutton.addEventListener('click', function (ev) {
takepicture();
ev.preventDefault();
}, false);
clearphoto();
}
function clearphoto() {
var context = canvas.getContext('2d');
context.fillStyle = "#AAA";
context.fillRect(0, 0, canvas.width, canvas.height);
var data = canvas.toDataURL('image/png');
photo.setAttribute('src', data);
}
function takepicture() {
var context = canvas.getContext('2d');
if (width && height) {
canvas.width = width;
canvas.height = height;
context.drawImage(video, 0, 0, width, height);
var data = canvas.toDataURL('image/png');
photo.setAttribute('src', data);
fetch("/api/barcode", {
headers: {
'Content-Type': 'application/json'
},
method: "POST",
body: JSON.stringify({ value: data })
}).then(res => {
document.getElementById("output").value = res;
});
} else {
clearphoto();
}
}
</script>
</body>
</html>
Webapi in cui dovrai integrare il lettore:
codice:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace BarcodeWebAPI.Controllers
{
public class BarcodeController : ApiController
{
[HttpPost]
public Parameter Post([FromBody] Parameter img)
{
Image image = LoadBase64(img.value);
//codice per la scansione in cui usare l'immagine in arrivo, di solito una cosa del genere:
var result = BarcodeScanner.Scan(image);
//
return new Parameter() { value = result };
}
public static Image LoadBase64(string base64)
{
var data = base64.Split(',').Last();
byte[] bytes = Convert.FromBase64String(data);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}
return image;
}
}
public class Parameter
{
public string value { get; set; }
}
}