Victor Image Processing Library How-to Tips

Scan and Print a Stack of Pages Using Java



This example checks the status of the automatic document feeder. As long as the ADF is loaded it scans a page and sends a copy to the printer. The sequence of Victor Library function calls used is:
            TWopen         to begin accessing the Twain device
            TWgetfeeder    to determine if any pages are loaded and ready to scan
            TWsetpixeltype to set the scanner to scan grayscale
            TWscanimageex  to scan an image and hide the user interface
            printimage     to send the image to the printer
            freeimage      to release the image buffer memory
            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 printer the Victor Java Native Interface, "vic." includes functions for getting a Window handle and a printer device context.

The Victor Library printimage function takes a callback function as one of the arguments. The Victor Java Native Interface, "vic." includes the class DSPFCT in which a print progress function, prtprogress, is defined. In this example a variable of class DSPFCT is declared and used as the callback function:

            DSPFCT prtfunc= new vic.DSPFCT();
            . . .
            rcode = vic.vic32jni.printimage(hWnd, hdcprn, prtmode, timage, prtrect, bwidth, prtfunc);
The prtprogress function displays the progress of the data being sent to the printer. You may alter this function to do nothing, to allow the user to cancel printing, or to do whatever you wish. Do not change the name of the function.

Scan and Print a Stack of 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 scanprintstack.java
java -classpath x:\java;x:\java\vic scanprintstack

 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
import vic.*;

/*
   javac -classpath e:\java scanprintstack.java
   java -classpath e:\java;e:\java\vic scanprintstack
*/

class scanprintstack {
   public static void main(String args[]) {
   int rcode = 0; 
   vic.imgdes cimage = new vic.imgdes();
   vic.RECT scanrect = new vic.RECT();
   int showUI = 0;             // 0 = hide, 1 = show
   int hwnd;
   int hdcprn;
   int prtmode;
   vic.RECT prtrect = new vic.RECT();
   int bwidth;
   vic.DSPFCT prtfunc= new vic.DSPFCT();
   vic.refvar feederEnabled = new vic.refvar();
   vic.refvar feederLoaded  = new vic.refvar();
   int count;
   rcode   = vic.vic32jni.enableunicode(1);  // Turn on Unicode if available
   hwnd    = vic.vic32jni.GetDesktopWindow();
   hdcprn  = vic.vic32jni.GetPrinterDC();
   prtmode = vic.vic32jni.PRTDEFAULT;
   scanrect.left   = 0;
   scanrect.top    = 0;
   scanrect.right  = 8000;  //  8 inches wide
   scanrect.bottom = 10000; // 10 inches high
   prtrect.left   = scanrect.left;  
   prtrect.top    = scanrect.top;   
   prtrect.right  = scanrect.right; 
   prtrect.bottom = scanrect.bottom;
   bwidth         = 0;      // border width
   count          = 0;
   rcode = vic.vic32jni.TWopen(hwnd);  // Make sure there's a corresponding TWclose()
   if(rcode == vic.vic32jni.NO_ERROR) {
            // Scan grayscale images  
      rcode = setTWpixeltypetograyscale(hwnd);
      do {
         vic.vic32jni.TWgetfeeder(hwnd, feederEnabled, feederLoaded);
         if((feederEnabled.value != 0) && (feederLoaded.value !=0)) {
            rcode = vic.vic32jni.TWscanimageex(hwnd, cimage, scanrect, showUI);
            count++;
            if(rcode == vic.vic32jni.NO_ERROR) {
               System.out.println("Scan successful, sending page " + count + " to printer");
               rcode = vic.vic32jni.printimage(hwnd, hdcprn, prtmode, cimage, prtrect, bwidth, prtfunc);
               vic.vic32jni.freeimage(cimage);
               }
            }
         } while((rcode == vic.vic32jni.NO_ERROR) && ((feederEnabled.value != 0) && (feederLoaded.value !=0)));
      System.out.println("Scanner document feeder empty or printer not ready");
      vic.vic32jni.TWclose();
      }
   else
      System.out.println("Scanner not ready");
   } // 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 the device 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.