在ASP.NET MVC 5应用程序中通过相机读取条形码

6

我使用ASP.NET MVC 5创建了一个网站,这个网站也可以作为Web应用程序在移动设备上使用。但是现在我想在用户使用移动设备上的应用程序时添加使用手机摄像头扫描条形码的功能。当然,有像PhoneGap这样的工具可以读取条形码,但关键是我想在我的ASP.NET MVC 5项目中添加此功能。
那么,在ASP.NET MVC 5中是否有一种通过移动摄像头读取条形码的方法?


1
尝试 <input type="file" id="mypic" accept="image/*"> - Martheen
2个回答

4
我已经解决了这个问题,以下是解决方案: 在视图文件(Index.chtml)中:
     <form>
            <input type="file" class="upload" size="45" name="file" id="file">
    </form>

<input type="file"...> 放在 form 标签中非常重要。 接下来使用 JavaScript。我使用它是因为我希望在单击浏览按钮时立即调用控制器。您也可以使用提交按钮。
JavaScript:

       $('#file').on("change", function () {
        for (i = 0; i < $('form').length; i++) {
            if ($('form').get(i)[0].value != "") /* get the file tag, you have to customize this code */
            {
                var formdata = new FormData($('form').get(i));
                CallService(formdata);
                break;
            }
        }
    });
    function CallService(file) {
        $.ajax({
            url: '@Url.Action("Scan", "Home")',
            type: 'POST',
            data: file,
            cache: false,
            processData: false,
            contentType: false,
            success: function (barcode) {
                alert(barcode);
            },
            error: function () {
                alert("ERROR");
            }
        });
    }

下一步,我们需要在服务器上分析图像并读取其条形码。我正在使用Aspose.BarCode库:
HomeController.cs
        public JsonResult Scan(HttpPostedFileBase file)
    {
        string barcode = "";
        try
        {
            string path = "";
            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                path = Path.Combine(Server.MapPath("~/App_Data"), fileName);
                file.SaveAs(path);
            }

            // Now we try to read the barcode
            // Instantiate BarCodeReader object
            BarCodeReader reader = new BarCodeReader(path, BarCodeReadType.Code39Standard);
            System.Drawing.Image img = System.Drawing.Image.FromFile(path);
            System.Diagnostics.Debug.WriteLine("Width:" + img.Width + " - Height:" + img.Height);

            try
            {
                // read Code39 bar code
                while (reader.Read())
                {

                    // detect bar code orientation
                    ViewBag.Title = reader.GetCodeText();
                    barcode = reader.GetCodeText();
                }
                reader.Close();
            }

            catch (Exception exp)
            {

                System.Console.Write(exp.Message);
            }


        }
        catch (Exception ex)
        {
            ViewBag.Title = ex.Message;
        }
        return Json(barcode);


    }
}

现在解码后的条形码已返回到视图中。

1
你好Mo Prog,你用什么来读取条形码?在这行代码中:BarCodeReader reader = new BarCodeReader(path, BarCodeReadType.Code39Standard); 请问你能提供任何相关的参考资料吗? - Trupti K. K.
@TruptiK.K。我使用了Aspose.Barcode for C#,它运行良好。 - Code Pope
@CodePope,你使用了这个库的授权版本吗?我也遇到了上面提到的评论中的一个错误。BarCodeReaderType.Code39Standard无法识别。 - dave317
@TruptiK.K. 你最终解决了这个问题吗?我也遇到了完全相同的问题,我想知道是否是因为我需要一个有许可证的 aspose.barcode 库版本的缘故... - dave317
@dave317 或许回复有点晚了,但是当然可以。对我来说完全有效。而且我没有任何授权版本。 - Code Pope

0

我已经编辑了代码,以使用Aspose.BarCode包;

   public JsonResult ScanDelivered(HttpPostedFileBase file)
    {
        string readedBarcode = "";
        try
        {
            string path = "";
            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                path = Path.Combine(Server.MapPath("~/App_Data"), fileName);
                file.SaveAs(path);
            }

          
            System.Drawing.Image img = System.Drawing.Image.FromFile(path);
            System.Diagnostics.Debug.WriteLine("Width:" + img.Width + " - Height:" + img.Height);

            try
            {
                // Initialize barcode reader
                using (BarCodeReader reader = new BarCodeReader(path, DecodeType.AllSupportedTypes))
                {
                    // Recognize barcodes on the image
                    foreach (var barcode in reader.ReadBarCodes())
                    {
                       
                        readedBarcode = barcode.CodeText;
                    }
                }

            }

            catch (Exception exp)
            {

                System.Console.Write(exp.Message);
            }


        }
        catch (Exception ex)
        {
            ViewBag.Title = ex.Message;
        }
        return Json(readedBarcode);


    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接