scan.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. Dynamsoft.DWT.RegisterEvent('OnWebTwainReady', Dynamsoft_OnReady);
  2. var DWObject;
  3. function Dynamsoft_OnReady() {
  4. DWObject = Dynamsoft.DWT.GetWebTwain('dwtcontrolContainer');
  5. }
  6. function AcquireImage() {
  7. if (DWObject) {
  8. DWObject.SelectSource(function () {
  9. var OnAcquireImageSuccess, OnAcquireImageFailure;
  10. OnAcquireImageSuccess = OnAcquireImageFailure = function () {
  11. UploadImage()
  12. console.log(DWObject.CurrentImageIndexInBuffer)
  13. DWObject.CloseSource();
  14. };
  15. DWObject.OpenSource();
  16. DWObject.IfFeederEnabled = false
  17. DWObject.IfShowUI = false; //Disable scanner UI.
  18. DWObject.IfDisableSourceAfterAcquire = true;//Scanner source will be disabled automatically after scan.
  19. // if (document.getElementById("ADF").checked && DWObject.IfFeederEnabled == true) // if paper is NOT loaded on the feeder
  20. if (DWObject.IfFeederEnabled && DWObject.IfFeederEnabled == true)
  21. {
  22. if (DWObject.IfFeederLoaded != true && DWObject.ErrorCode == 0) {
  23. alert("No paper detected! Please load papers and try again!");
  24. return;
  25. }
  26. }
  27. DWObject.AcquireImage(OnAcquireImageSuccess, OnAcquireImageFailure);
  28. }, function () {
  29. console.log('SelectSource failed!');
  30. });
  31. }
  32. }
  33. function UploadImage() {
  34. if (DWObject) {
  35. // If no image in buffer, return the function
  36. if (DWObject.HowManyImagesInBuffer == 0)
  37. return;
  38. var strHTTPServer = location.hostname; //The name of the HTTP server. For example: "www.dynamsoft.com";
  39. var CurrentPathName = unescape(location.pathname);
  40. var CurrentPath = CurrentPathName.substring(0, CurrentPathName.lastIndexOf("/") + 1);
  41. var strActionPage = CurrentPath + "SaveToFile.aspx";
  42. var strHTTPServer = "118.190.145.217"
  43. var strActionPage = "/api/admin/uploadfile"
  44. DWObject.IfSSL = false; // Set whether SSL is used
  45. DWObject.HTTPPort = 8000//location.port == "" ? 80 : location.port;
  46. var Digital = new Date();
  47. var uploadfilename = Digital.getMilliseconds(); // Uses milliseconds according to local time as the file name
  48. DWObject.HTTPUploadThroughPost(strHTTPServer, DWObject.CurrentImageIndexInBuffer, strActionPage, uploadfilename + ".jpg", OnHttpUploadSuccess, OnHttpUploadFailure);
  49. }
  50. }
  51. function OnHttpUploadSuccess() {
  52. console.log('successful');
  53. }
  54. function OnHttpUploadFailure(errorCode, errorString, sHttpResponse) {
  55. alert(errorString + sHttpResponse);
  56. }