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;
}
}