Victor Image Processing Library How-to Tips

Scan and Save 3 Pages Using Java



This example scans three pages and saves them into a multipage TIFF file. The sequence of Victor Library function calls used is:
            TWopen             to begin accessing the Twain device
            TWscancountimages  to scan three images
            TWclose            to release the Twain device
Using Victor Library functions in Java is identical to using them in C but you must add the prefix "vic.vic32jni." to each function and defined constant.

To make it easy to use a Windows Twain-compliant scanner the Victor Java Native Interface, "vic." includes functions for getting a Window handle.

The Victor Library function TWscancountimages (and each of the other functions for scanning multiple images) takes a callback function as one of the arguments. The Victor Java Native Interface, "vic." includes the class SCNFCT in which a function, savescan, is defined. In this example a variable of class SCNFCT is declared and used as the callback function:

            vic.SCNFCT scanfunc = new vic.SCNFCT();

            . . .

            rcode = vic.vic32jni.TWscancountimages(hwnd, cimage, scanrect, showUI, 3, scanfunc);
The savescan function saves each scanned page into a multipage TIFF file, "test.tif." You may alter this function to do nothing, to allow the user to cancel scanning, or to do whatever you wish. Do not change the name of the function.

Scan and Save 3 Pages

Requires Victor Image Processing Library v 5.3 or higher, the Victor Java Native Interface vic.zip and Java 2.

Place the dlls:

vic32.dll in x:\java\bin
victw32.dll in x:\java\bin
vic32jni.dll in the same subdirectory as the application

To compile and run bmp2jpeg.class:

javac -classpath x:\java scan3pages.java
java -classpath x:\java;x:\java\vic scan3pages

 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

import vic.*;


/*

   javac -classpath e:\java scan3pages.java
   java -classpath e:\java;e:\java\vic scan3pages

*/

class scan3pages {

   public static void main(String args[]) {
   String jsavefilename;
   int rcode = 0; 
   vic.imgdes cimage = new vic.imgdes();
   vic.RECT scanrect = new vic.RECT();
   int showUI = 0;             // 0 = hide, 1 = show
   int hwnd;
   vic.SCNFCT scanfunc = new vic.SCNFCT();

   rcode = vic.vic32jni.enableunicode(1);  // Turn on Unicode if available
   hwnd = vic.vic32jni.GetDesktopWindow();

   if(rcode == vic.vic32jni.NO_ERROR) {
      scanrect.left = 0;
      scanrect.top = 0;
      scanrect.right = 3000;
      scanrect.bottom = 2000;

      rcode = vic.vic32jni.TWopen(hwnd);
      if(rcode == vic.vic32jni.NO_ERROR) {

         rcode = setTWpixeltypetograyscale(hwnd);

         rcode = vic.vic32jni.TWscancountimages(hwnd, cimage, scanrect, showUI, 3, scanfunc);
         // scanfunc will automatically save the pages into multipage file test.tif
         // To change it edit SCNFCT.JAVA.

         System.out.println(" scanimage rcode = " + rcode);
         vic.vic32jni.TWclose();
         }
      }
   } // end of main()

   static int setTWpixeltypetograyscale(int hwnd)
   {
   int rcode;
   vic.TWAIN_CAP_DATA newPT = new vic.TWAIN_CAP_DATA();
   short TWON_ONEVALUE = 5;

   // Pixel type definitions
   short TWPT_BW   = 0;
   short TWPT_GRAY = 1;
   short TWPT_RGB  = 2;

   newPT.oneValue_val = TWPT_GRAY;  
   newPT.conType      = TWON_ONEVALUE;        // Container type
   rcode = vic.vic32jni.TWsetpixeltype(hwnd, newPT);
   // Keep in mind that not all twain devices allow you to set the pixel type,
   // a return code of NO_ERROR only means the function successfully found the
   // device driver, not that it accepted the setting.

   return(rcode);
   }
}

Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code




Copyright © 2001 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.