JQuery扫描仪编程示例代码

时间:2022-11-21 11:01:38


 1.js

$(function() {
$('#btnScan').click(function() {
document.getElementById('scaner1').scan();
}),
$('#btnUpload1').click(function() {
ajax_post_1();

}),
$('#btnUpload2').click(function() {
ajax_post_2();
})
});

function getServerUrl(){
if ($('#asp').attr("checked")==true) {
return "./ajax.asp";
}else if ($('#jsp').attr("checked")==true) {
return "http://localhost:8080/pages/ajax.jsp";
}else if ($('#php').attr("checked")==true) {
return "./ajax.php";
}else if ($('#aspnet').attr("checked")==true) {
return "./ajax.aspx";
} else{
alert('请选择服务器端技术类型!');
return null;
}
}


function ajax_post_1() {
var base64_data = document.getElementById('scaner1').jpegBase64Data;
// alert("data length:"+base64_data.length);
// var s=getServerUrl();
// alert(s);
$.ajax({
url : getServerUrl(),
type : 'POST',
dataType : 'jason',
data : {
picData : "'" + base64_data + "'",
picExt:".jpg"
},
timeout : 1000,
success : callbackfun1
});
}

function ajax_post_2() {
var base64_data = document.getElementById('scaner1').allImageAsPDFData;
// alert("data length:"+base64_data.length);
// var s=getServerUrl();
// alert(s);
$.ajax({
url : getServerUrl(),
type : 'POST',
dataType : 'jason',
data : {
picData : "'" + base64_data + "'",
picExt:".pdf"
},
timeout : 1000,
success : callbackfun1
});
}


function callbackfun1(data) {
var obj = eval('(' + data + ')');

if ('ok' == obj.savestatus) {
alert('success!');
}

}


 

2.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ScanOnWeb控件JQuery技术示例页面</title>
<script language="javascript" type="text/javascript" src="../js/jquery-1.4.2.min.js" ></script>
<script language="javascript" type="text/javascript" src="./ajax.js" ></script>
</head>

<body>
<form action="">
<p>请选择服务器端技术:
<label>
<input type="radio" name="radio" value="jsp" />
jsp</label>    
<label>
<input type="radio" name="radio" value="asp.net" />
asp.net</label>    
</p>
<p>
<input type="button" value="扫描" />
<input type="button" value="上传单页结果" />
<input type="button" value="上传全部结果" />
</p>
<object classid="clsid:15D142CD-E529-4B01-9D62-22C9A6C00E9B" width="100%" height="600" codebase="../cabs/ScanOnWeb.cab#version=1,0,0,10">
<param name="Visible" value="0">
<param name="AutoScroll" value="0">
<param name="AutoSize" value="0">
<param name="AxBorderStyle" value="1">
<param name="Caption" value="scaner">
<param name="Color" value="4278190095">
<param name="Font" value="宋体">
<param name="KeyPreview" value="0">
<param name="PixelsPerInch" value="96">
<param name="PrintScale" value="1">
<param name="Scaled" value="-1">
<param name="DropTarget" value="0">
<param name="HelpFile" value>
<param name="PopupMode" value="0">
<param name="ScreenSnap" value="0">
<param name="SnapBuffer" value="10">
<param name="DockSite" value="0">
<param name="DoubleBuffered" value="0">
<param name="ParentDoubleBuffered" value="0">
<param name="UseDockManager" value="0">
<param name="Enabled" value="-1">
<param name="AlignWithMargins" value="0">
<param name="ParentCustomHint" value="-1">
</object>
</form>
</body>
</html>

 

3.jsp代码

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.io.*" errorPage="" %><%
//注意一行html内容也不要出现,防止被编译为serlvet以后有写html到客户端的行为
String savePath=config.getServletContext().getRealPath("/")+"//";

File tmp_path=new File(savePath);
tmp_path.mkdirs();
System.out.println("照片数据保存路径:"+savePath);

String pic_base_64_data=request.getParameter("picData");
//System.out.println("图片数据:"+pic_base_64_data);

//如果下面的代码输出true则说明需要调整服务器软件工作参数,解决接受post数据的大小限制问题,例如
//tomcat的话需要在server.xml中配置maxPostSize="0"来解除上传数据的大小限制 <Connector port="8080" protocol="HTTP/1.1"
// connectionTimeout="20000"
// redirectPort="8443" maxPostSize="0"/>
//
System.out.println(null==pic_base_64_data);
System.out.println("base64 string length:"+pic_base_64_data.length());
String fileFormat=request.getParameter("picExt");
sun.misc.BASE64Decoder decode=new sun.misc.BASE64Decoder();

byte[] datas=decode.decodeBuffer(pic_base_64_data.substring(1, pic_base_64_data.length()-2));
String filename=String.valueOf(System.currentTimeMillis())+fileFormat;
File file=new File(savePath+filename);
OutputStream fos=new FileOutputStream(file);
System.out.println("图片文件名称:"+filename);
fos.write(datas);
fos.close();

response.setContentType("application/x-json");
response.setCharacterEncoding("utf-8");
out.print("{'savestatus':'ok'}");
out.flush();
out.close();
%>