jsp扫描仪编程

好多的OA系统等项目需要把文稿通过扫描仪扫描成PDF等格式,然后在OA系统中流转发布,在 c/s架构中问题不大,但是在web环境中如何通过网页去读取扫描仪数据呢?由于浏览器的安全性限制,使用html和javascript是无法直接操控扫描仪的,解决的办法就是使用activex来扩展浏览器功能。
这里在jsp页面中通过加载ScanOnWeb扫描控件的方式来解决jsp扫描仪编程问题。
代码如下(完整的代码下载附件):

<!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>控件测试页面</title>
<script language="javascript" type="text/javascript" src="../js/jquery-1.4.2.min.js" ></script>
<script type="text/javascript">
$(function() {
   $('#btnScan').click(function() {
document.getElementById('scaner1').scan();
}),
$('#btnUpload1').click(function() {
submit_1();

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

function auto_select_server_action(){
var server_action_url;
    if ($('#asp').attr("checked")==true) {
  server_action_url="./submit.asp";
}else if ($('#jsp').attr("checked")==true) {
  server_action_url="http://localhost:8080/pages/submit.jsp";
    }else if ($('#php').attr("checked")==true) {
  server_action_url="./submit.php";
    }else if ($('#aspnet').attr("checked")==true) {
  server_action_url="./submit.aspx";
    } else{
      alert('请选择服务器端技术类型!');
  return ;
    }
   document.forms[0].action=server_action_url;
}

function submit_1(){
  auto_select_server_action();
  var base64_data = document.getElementById('scaner1').jpegBase64Data; 
  document.getElementById('picData').value=base64_data;
  document.getElementById('picExt').value='.jpg';
  document.forms[0].submit();
}

function submit_2(){
  auto_select_server_action();
  var base64_data = document.getElementById('scaner1').allImageAsPDFData;
  document.getElementById('picData').value=base64_data;
  alert(base64_data.length);
  document.getElementById('picExt').value='.pdf';  
  document.forms[0].submit();
}

</script>
</head>
<body>
<form action="" name="form1" id="form1" method="post" target="_blank">
  <input type="hidden" id="picData" name="picData"/>
  <input type="hidden" id="picExt"  name="picExt"/>
  <p>请选择服务器端技术:
    <label>
      <input name="radio" type="radio" id="asp" value="asp" checked="checked" />
      asp</label>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <label>
      <input type="radio" name="radio" id="jsp" value="jsp" />
      jsp</label>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <label>
      <input type="radio" name="radio" id="aspnet" value="asp.net" />
      asp.net</label>
    &nbsp;&nbsp;&nbsp;&nbsp;
    <label>
      <input type="radio" name="radio" id="php" value="php" />
      php</label>
  </p>
  <p>注意上传pdf格式文件时尽量使用黑白扫描格式,否则数据量过大会造成某些未调优参数的服务器端技术处理失败。</p>
  <p>
    <input type="button" value="扫描"  id="btnScan"/>
    <input type="button" value="上传单页结果"  id="btnUpload1"/>
    <input type="button" value="上传全部结果"  id="btnUpload2"/>
  </p>
  <object classid="clsid:15D142CD-E529-4B01-9D62-22C9A6C00E9B" id="scaner1" 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>
---------------------------------------------------------------
jsp文件:

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.io.*" errorPage="" %>
<%
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");

//如果下面的代码输出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);

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();

out.print("<a href='" + filename + "'>click here</a>");
out.flush();
out.close();
%>

以上代码下载见下面的链接。

你可能感兴趣的:(编程,jsp,SQL Server,asp.net,asp)