方正高拍儀接口開發(fā)WEB上傳圖片到服務(wù)器(.NET)

1、安裝插件程序【方正影像采集插件安裝包_I_v3.0.74.0.exe】
2、根據(jù)技術(shù)人員提供的HTMLdemo進(jìn)行開發(fā)


34.jpg

增加攝像預(yù)覽控件,客戶不需要預(yù)覽,因此我進(jìn)行了隱藏

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

增加按鈕事件方法,另外由于初始化需要一定的時(shí)間,技術(shù)人員說是3秒,因此我設(shè)置了一個(gè)等待3秒再執(zhí)行拍攝的方法。最后得到圖像的Base64字符流傳至后臺(tái)進(jìn)行上傳到服務(wù)器

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對(duì)象
            var szDeviceIndex = "0";//設(shè)備的編號(hào);   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("請(qǐng)切換到IE兼容模式或使用IE瀏覽器!");
            //    return;
            //}

            Capture = document.getElementById("Capture");//根據(jù)js的腳本內(nèi)容,必須先獲取object對(duì)象      
            result = Capture.InitDevice();//初始化 0-成功
            //alert("初始化:" + result.toString());
            result = Capture.StartDevice(szDeviceIndex);//打開文檔攝像頭   0-成功
            //alert("打開攝像頭:" + result.toString());
            result = Capture.SetResolution(szDeviceIndex, "3742", "2806");//設(shè)置分辨率  0-成功
            //alert("設(shè)置分辨率:" + result.toString());
            result = Capture.SetCutPageType(szDeviceIndex, "1");//自動(dòng)切邊  0-成功
            //alert("自動(dòng)切邊:" + result.toString());

            var strFileName = strFilePath + imgeId.toString() + szPostfix;
            //setTimeout(Capture.CaptureImage(szDeviceIndex , strFileName), 3000); //
            sleep(3000); //3秒后再進(jìn)行拍攝 太快 反應(yīng)不過來
            result = Capture.CaptureImage(szDeviceIndex, strFileName); //拍攝 0 - 成功

            if (result.toString() != "0") {
                alert("拍攝失??!");
                return;
            }
            var strBase64 = Capture.EncodeBase64(strFileName);//獲取圖像的Base64字符流;
            var VoucherCode = F("<%=txtVoucherCode.ClientID%>").getValue(); //出庫單號(hào)

            //上傳到服務(wù)器
            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(); //最后釋放設(shè)備
        }

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)//如果不存在就創(chuàng)建文件夾
            {
                System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(ImageFilePath));
            }
            string ImagePath = HttpContext.Current.Server.MapPath(ImageFilePath) + "/" + imageName;//定義圖片名稱
            File.WriteAllBytes(ImagePath + ".jpg", bt); //保存圖片到服務(wù)器,然后獲取路徑  

            //保存路徑
            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;
            }

        }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容