Scan.jsp
<HTML>
<HEAD>
<title>Online
Demo of Web Twain</title>
<SCRIPT
language="javascript">
function
btnScan_onclick()
{
frmScan.DynamicWebTwain1.SelectSource();
frmScan.DynamicWebTwain1.AcquireImage();
}
function
btnUpload_onclick()
{
var
strActionPage;
var
strHostIP;
var
CurrentPathName = unescape(location.pathname); //
get current PathName in plain ASCII
var
CurrentPath = CurrentPathName.substring(0,
CurrentPathName.lastIndexOf("/") + 1);
strActionPage
= CurrentPath + "SaveToFile.jsp"; //the ActionPage's file path
strHostIP
= location.hostname;
frmScan.DynamicWebTwain1.HTTPPort
= location.port==""?80:location.port;
frmScan.DynamicWebTwain1.HTTPUploadThroughPost(strHostIP,
0, strActionPage, "imageData.jpg");
if
(frmScan.DynamicWebTwain1.ErrorCode != 0) { //Failed
to upload image
alert(frmScan.DynamicWebTwain1.ErrorString);
if
(frmScan.DynamicWebTwain1.ErrorString == "HTTP process error")
alert
(frmScan.DynamicWebTwain1.HTTPPostResponseString);
}
else
//succeeded
frmScan.submit();
}
</SCRIPT>
</HEAD>
<body
MS_POSITIONING="GridLayout">
<OBJECT
CLASSID = "clsid:5220cb21-c88d-11cf-b347-00aa00a28331" VIEWASTEXT>
<PARAM
NAME="LPKPath" VALUE="DynamicWebTwain.lpk">
</OBJECT>
<form
id="frmScan" action="ShowResult.html">
<TABLE
width="100%">
<TR>
<TD>
<center>
<object
classid="clsid:FFC6F181-A5CF-4EC4-A441-093D7134FBF2"
id="DynamicWebTwain1" width="250" height="350"
CodeBase
= "DynamicWebTwain.cab#version=7,0">
<param
name="_cx" value="847">
<param
name="_cy" value="847">
<param
name="JpgQuality" value="80">
<param
name="Manufacturer" value="DynamSoft Corporation">
<param
name="ProductFamily" value="Dynamic Web TWAIN">
<param
name="ProductName" value="Dynamic Web TWAIN">
<param
name="VersionInfo" value="Dynamic Web TWAIN 7.0">
<param
name="TransferMode" value="0">
<param
name="BorderStyle" value="0">
<param
name="FTPUserName" value>
<param
name="FTPPassword" value>
<param
name="FTPPort" value="21">
<param
name="HTTPUserName" value>
<param
name="HTTPPassword" value>
<param
name="HTTPPort" value="80">
<param
name="ProxyServer" value>
<param
name="IfDisableSourceAfterAcquire" value="0">
<param
name="IfShowUI" value="-1">
<param
name="IfModalUI" value="-1">
<param
name="IfTiffMultiPage" value="0">
<param
name="IfThrowException" value="0">
<param
name="TIFFCompressionType" value="0">
<param
name="IfFitWindow" value="-1">
</object>
</center>
</TD>
</TR>
<TR>
<TD>
<center>
<input
id="btnScan" style="WIDTH: 100;HEIGHT: 30" onclick="return
btnScan_onclick()" type="button" value="Scan">
<input
style="WIDTH: 100;HEIGHT: 30" onclick="return
btnUpload_onclick()" type="button" value="Upload">
</center>
</TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
SaveToFile.jsp
<%@ page
language="java" import="java.io.*"%>
<%!
//find
string
int
Find(byte[] src, int start, int length, String str)
{
if
(start < 0) return -1;
if
(start >= src.length) return -1;
int
end = (start + length - 1);
if
(end < 0) end = 0;
if
(end <= start) end = start;
int
index = 0;
int
firstMatch = -1;
for
(int i = start; i <= end; i++)
{
if
(src[i] == str.charAt(index))
{
if
(firstMatch == -1)
firstMatch
= i; //save first match
index
++;
}
else
{
index
= 0; //try to find again
if
(firstMatch != -1 && (firstMatch + 1)!= i)
i
= firstMatch + 1; //reset index to find
firstMatch
= -1;
}
if
(index == str.length())
return
firstMatch; // found
}
return
-1;
}
int
ReverseFind(byte[] src, int start, int length, String str)
{
if
(start < 0) return -1;
if
(start >= src.length) return -1;
int
end = (start - length + 1);
if
(end < 0) end = 0;
if
(end >= start) end = start;
int
index = str.length() - 1;
int
firstMatch = -1;
for
(int i = start; i >= end; i--)
{
if
(src[i] == str.charAt(index))
{
if
(firstMatch == -1)
firstMatch
= i; //save first match
index
--;
}
else
{
index
= str.length() - 1; //try to find again
if
(firstMatch != -1 && (firstMatch - 1)!= i)
i
= firstMatch -1; // reset index to find
firstMatch
= -1;
}
if
(index < 0)
return
i; //found
}
return
-1;
}
%>
<%
String
contentType = request.getContentType();
if
((contentType != null) &&
(contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream
in = new DataInputStream(request.getInputStream());
int
formDataLength = request.getContentLength();
byte
dataBytes[] = new byte[formDataLength];
int
byteRead = 0;
int
totalBytesRead = 0;
while
(totalBytesRead < formDataLength)
{
byteRead
= in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead
+= byteRead;
}
in.close();
int
endPos = -1;
int
startPos = -1;
//
Found boundary
String
strEOL = "\r\n";
int
findLength = 2048;
int
posEOL = Find(dataBytes, 0, findLength, strEOL);
int
posName = Find(dataBytes, posEOL, findLength, "Content-Disposition:
form-data;");
String
boundary = "";
if
(posName != -1)
boundary
= new String(dataBytes, 0, posEOL);
//Found
filename
posEOL
= Find(dataBytes, posName, findLength, strEOL);
int
posFileNameL = ReverseFind(dataBytes, posEOL, findLength,
"filename=\"");
String
saveFile = "";
if
(posFileNameL != -1)
{
posFileNameL
+= 10; //
int
posFileNameR = ReverseFind(dataBytes, posEOL, findLength, "\"");
saveFile
= new String(dataBytes, posFileNameL, posFileNameR - posFileNameL);
}
//Found
content type
int
posContentType = Find(dataBytes, posEOL, findLength,
"Content-Type:");
if
(posContentType != -1)
posEOL
= Find(dataBytes, posContentType, findLength, strEOL);
//Found
EOL
int
posOldEOL = posEOL;
if
(posEOL != -1)
posEOL
+= strEOL.length();
do
{
posEOL
= Find(dataBytes, posEOL, strEOL.length(), strEOL);
if
(posEOL != -1)
{
posOldEOL
= posEOL;
posEOL
+= strEOL.length();
}
}while
(posEOL != -1);
//get
start pos
startPos
= (posOldEOL + strEOL.length());
//Found
end pos
int
posEndBoundary = ReverseFind(dataBytes, dataBytes.length - 1, findLength,
boundary);
if
(posEndBoundary != -1)
{
posEOL
= ReverseFind(dataBytes, posEndBoundary - 1, strEOL.length(), strEOL);
}
posOldEOL
= posEOL;
if
(posEOL != -1)
posEOL
-= 1;
do
{
posEOL
= ReverseFind(dataBytes, posEOL, strEOL.length(), strEOL);
if
(posEOL != -1)
{
posOldEOL
= posEOL;
posEOL
-= 1;
}
}while
(posEOL != -1);
//get
end pos
endPos
= posOldEOL;
File
fileToRecv = new File(application.getRealPath("/") + saveFile);
if(!fileToRecv.exists())
{
fileToRecv.createNewFile();
}
FileOutputStream
fileOut = new FileOutputStream(fileToRecv);
fileOut.write(dataBytes,
startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
}
%>
Good day-time.
ReplyDeleteCan you please explain how to use this example e.g. in portlet or dynamic web project (Java, JSP)? I cannot get where from objects like "frmScan.DynamicWebTwain1.SelectSource" taken? Mb any lib should be attached or...? Any help is much appreciated.
You must have install lpk_tool.exe and download trial version
ReplyDeletehttp://www.dynamsoft.com/Products/WebTWAIN_Overview.aspx?gclid=CJWMhr7dn7YCFRES6wodBSQAYg