方正高拍仪接口开发WEB上传图片到服务器(.NET)

1、安装插件程序【方正影像采集插件安装包_I_v3.0.74.0.exe】
2、根据技术人员提供的HTMLdemo进行开发


34.jpg

增加摄像预览控件,客户不需要预览,因此我进行了隐藏

<object id="Capture" type="application/ZCaptureVideo" hidden="hidden" />

增加按钮事件方法,另外由于初始化需要一定的时间,技术人员说是3秒,因此我设置了一个等待3秒再执行拍摄的方法。最后得到图像的Base64字符流传至后台进行上传到服务器

function sleep(numberMillis) {
            var now = new Date();
            var exitTime = now.getTime() + numberMillis;
            while (true) {
                now = new Date();
                if (now.getTime() > exitTime)
                    return;
            }
        }
function onCaptureClick(event) {
            var Capture;//必须得获取object对象
            var szDeviceIndex = "0";//设备的编号;   0:文档摄像头;1:人像摄像头

            var today = new Date();
            var month = today.getMonth() + 1;
            month = month < 10 ? '0' + month : month;
            var day = today.getDate() < 10 ? '0' + today.getDate() : today.getDate();
            var hours = today.getHours() < 10 ? '0' + today.getHours() : today.getHours();
            var mins = today.getMinutes() < 10 ? '0' + today.getMinutes() : today.getMinutes();
            var secs = today.getSeconds() < 10 ? '0' + today.getSeconds() : today.getSeconds();
            var imgeId = today.getFullYear() + month + day + hours + mins + secs;

            var strFilePath = "D:\\DocImage\\";
            var szPostfix = ".jpg";

            var result;

            //判断是否为ie浏览器
            //var IEVersion = IEVersion();
            //var isie = isIE();
            //if (isie == "1") {
            //    alert("请切换到IE兼容模式或使用IE浏览器!");
            //    return;
            //}

            Capture = document.getElementById("Capture");//根据js的脚本内容,必须先获取object对象      
            result = Capture.InitDevice();//初始化 0-成功
            //alert("初始化:" + result.toString());
            result = Capture.StartDevice(szDeviceIndex);//打开文档摄像头   0-成功
            //alert("打开摄像头:" + result.toString());
            result = Capture.SetResolution(szDeviceIndex, "3742", "2806");//设置分辨率  0-成功
            //alert("设置分辨率:" + result.toString());
            result = Capture.SetCutPageType(szDeviceIndex, "1");//自动切边  0-成功
            //alert("自动切边:" + result.toString());

            var strFileName = strFilePath + imgeId.toString() + szPostfix;
            //setTimeout(Capture.CaptureImage(szDeviceIndex , strFileName), 3000); //
            sleep(3000); //3秒后再进行拍摄 太快 反应不过来
            result = Capture.CaptureImage(szDeviceIndex, strFileName); //拍摄 0 - 成功

            if (result.toString() != "0") {
                alert("拍摄失败!");
                return;
            }
            var strBase64 = Capture.EncodeBase64(strFileName);//获取图像的Base64字符流;
            var VoucherCode = F("<%=txtVoucherCode.ClientID%>").getValue(); //出库单号

            //上传到服务器
            var pars = {
                strBase64: strBase64,
                VoucherCode: VoucherCode
            };
            $.ajax({
                url: "/Common/commonServices.ashx?Module=OutStockCapture&r=" + Math.random(),
                type: 'post',
                cache: false,
                dataType: 'json',
                data: pars,
                success: function (data) {
                    if (data.d.success == 1) {
                        //刷新附件列表
                        __doPostBack('', 'btnCaptureClick');
                        alert("上传成功!");
                        return;
                    }
                    else {
                        //alert("上传失败!");
                        return;
                    }
                }
            });


            //Capture.ReleaseDevice(); //最后释放设备
        }

commonServices.ashx 文件上传图片方法

private void OutStockImgUpload(HttpContext context)
        {
            JObject obj = null;
            string ms = "";
            //bool result = true;

            string VoucherCode = context.Request.Form["VoucherCode"];
            string strBase64 = context.Request.Form["strBase64"];

            byte[] bt = Convert.FromBase64String(strBase64);//获取图片base64
            string fileName = DateTime.Now.ToString("yyyyMMdd");
            string imageName = System.DateTime.Now.ToString("yyyyHHddHHmmss");
            string ImageFilePath = "/upload" + "/" + fileName;

            if (System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(ImageFilePath)) == false)//如果不存在就创建文件夹
            {
                System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(ImageFilePath));
            }
            string ImagePath = HttpContext.Current.Server.MapPath(ImageFilePath) + "/" + imageName;//定义图片名称
            File.WriteAllBytes(ImagePath + ".jpg", bt); //保存图片到服务器,然后获取路径  

            //保存路径
            T_Oper_OutStockVoucherService service = new T_Oper_OutStockVoucherService();
            T_Annex_OutStockVoucher item = new T_Annex_OutStockVoucher();
            item.FilePath = ImageFilePath + "/" + imageName + ".jpg";
            item.VoucherCode = VoucherCode;
            int result = service.InserOutStockVoucherAnnex(item);

            if (result == 1)
            {
                obj = new JObject(new JProperty("d", new JObject(
                            new JProperty("msg", ms),
                            new JProperty("success", "1"))));
                context.Response.Write(obj.ToString());
                return;
            }
            else
            {
                obj = new JObject(new JProperty("d", new JObject(
                            new JProperty("msg", ms),
                            new JProperty("success", "0"))));
                context.Response.Write(obj.ToString());
                return;
            }

        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。