网页中 BarcodeReader.js 识别商品条形码

最近要做一个商品挑选码的扫码程序, 我这里做了一个简单的  asp.net mvc 网站例子 ,有兴趣的可以看看。本人菜鸟,写错请见谅。

//参考文档
https://blog.csdn.net/qq_37164847/article/details/103731764


//平时自己常用来生成条形码的工具网站
http://www.t-x-m.com


//别人做的条形码和二维码解析网站(可以用来对比自己做的程序)
https://www.moyublog.com/demo/93_2536.html

我做的例子界面如下,很简单

首先在 visual studio中建立一个简单的 asp.net mvc 项目 ,  homeController里面加入一个 actionResult ,如  Tiaoxm :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace tiaoxmTest1.Controllers
{
    public class HomeController : Controller
    {
       
        public ActionResult Index()
        {
            return View();
        }

        //条形码
        public ActionResult Tiaoxm()
        {
            return View();
        }


    }






}

然后是前端代码


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Tiaoxm</title>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/40076/DecoderWorker.js"></script>
    <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/40076/exif.js"></script>
    <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/40076/BarcodeReader.js"></script>

</head>
<body>
    <div> 
        <p>自己模仿做条形码</p>
        <input type="file" name="barCode" id="barCode" value="select img" />

        <div style="border-color:aquamarine;">
            <img id="img" style="width:350px;height:150px;display:block;" />
        
            <p class="codeInfo mt-12" style="color: red;font-size: 16px;text-align: center;"></p>
        </div>

    </div>

    <script type="text/javascript">

        $(function () {

            BarcodeReader.Init();

            BarcodeReader.SetImageCallback(function (result) {
                console.dir("1.===" + JSON.stringify(result));
                if (!result.length) {
                    $(".codeInfo").html('条形码读取失败');
                    return
                }
                var barcode = result[0];
                if (barcode.Value) {
                    $(".codeInfo").html('条形码信息是:' + barcode.Value);
                }
            });


            $('#barCode').change(function (evt) {
                //图片选择按钮
                var file = evt.target.files[0];
                var reader = new FileReader();
  
                reader.onloadend = function () {
                    var img = new Image();
                    img.src = reader.result;
              
                    BarcodeReader.DecodeImage(img);
                }
                 
                reader.readAsDataURL(file);
                img.src = URL.createObjectURL(file)
            });  //end btn change 

        }); //end page ready

    </script>


</body>
</html>

写到这里就可以运行条形码识别了, 其中head 里面的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值