6

has anyone gotten this http://www.xarg.org/project/jquery-webcam-plugin/, to work with aps.net mvc 3? I can't seem to decode the image, using the WebImage class or BitmapImage.

I've tired to do this with Silverlight, but I'm really unsure on how to upload the image. I do not need to save the image, I just want to process it, what im really looking to do is read a bar-code via web app.

I just can't seem to find a good guide to uploading an image from Silverlight or flash to my MVC app.

Thanks in advance.

nagates
  • 620
  • 13
  • 40
  • Have you succeeded creating this web app which takes a photo and uploads to server to read the barcode? I am about to do exactly the same thing and have troubles – Code Pope Sep 29 '15 at 15:00
  • Yea, I've some limited success using silverlight. Able to read the image, but had a hell of time reading the actual bar code, probably need a better webcam. – nagates Sep 29 '15 at 21:21
  • But silverlight does not work on Android and IOS! So your web app is not for Android and IOS devices? – Code Pope Sep 30 '15 at 12:32
  • I never really finished this project, at the time I didn't really have a good mobile device to test with and was going to figure that part out later.Might look at this... http://stackoverflow.com/questions/6336641/html5-camera-access-through-browser-in-ios – nagates Sep 30 '15 at 14:00

2 Answers2

8

The documentation contains many examples. All that's needed is to read and try out.

So here's how your Index.cshtml view might look like using the browser's HTML5 canvas element to encode the raw image data coming from the webcam into a PNG image that will be sent to the server using an AJAX request:

<script src="@Url.Content("~/Scripts/jquery.webcam.js")" type="text/javascript"></script>

<div id="webcam"></div>
<a href="#" id="upload">Capture image and send for processing</a>

<script type="text/javascript">
    var pos = 0, ctx = null, saveCB, image = [];
    var canvas = document.createElement('canvas');
    canvas.setAttribute('width', 320);
    canvas.setAttribute('height', 240);
    ctx = canvas.getContext('2d');
    image = ctx.getImageData(0, 0, 320, 240);

    var saveCB = function (data) {
        var col = data.split(';');
        var img = image;
        for (var i = 0; i < 320; i++) {
            var tmp = parseInt(col[i]);
            img.data[pos + 0] = (tmp >> 16) & 0xff;
            img.data[pos + 1] = (tmp >> 8) & 0xff;
            img.data[pos + 2] = tmp & 0xff;
            img.data[pos + 3] = 0xff;
            pos += 4;
        }

        if (pos >= 4 * 320 * 240) {
            ctx.putImageData(img, 0, 0);
            $.post('@Url.Action("Upload")', { type: 'data', image: canvas.toDataURL("image/png") }, function (result) {
                if (result.success) {
                    alert('The image was successfully sent to the server for processing');
                }
            });
            pos = 0;
        }
    };

    $('#webcam').webcam({
        width: 320,
        height: 240,
        mode: 'callback',
        swffile: '@Url.Content("~/scripts/jscam_canvas_only.swf")',
        onSave: saveCB,
        onCapture: function () {
            webcam.save();
        }
    });

    $('#upload').click(function () {
        webcam.capture();
        return false;
    });
</script>

and your Home controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Upload(string image)
    {
        image = image.Substring("data:image/png;base64,".Length);
        var buffer = Convert.FromBase64String(image);
        // TODO: I am saving the image on the hard disk but
        // you could do whatever processing you want with it
        System.IO.File.WriteAllBytes(Server.MapPath("~/app_data/capture.png"), buffer);
        return Json(new { success = true });
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Nice answer and code structure, I found this easier to follow than there actual example code :). Did have an issue with passing the data back when the URI string was too long but using a non wrapped ajax call sorted that incase anyone else has the issue... $.ajax({ dataType: "json", type:'POST', url: "your function location', data: {image : canvas.toDataURL("image/png")}, success: function(response){} }); – Scott Alexander Apr 17 '14 at 15:30
  • I have implemented the code, but when testing in my mobile device, nothing happens. Even the mobile device doesn't ask to use the camera and there is no success message and nothing in the app_data folder. Can anybody help me? It is very urgent – Code Pope Sep 29 '15 at 14:15
2

You might give www.scriptcam.com a try, there is extensive documentation available on this jquery plugin.

John x
  • 4,031
  • 8
  • 42
  • 67
John
  • 21
  • 1