Victor Image Processing Library How-to Tips

Print an Image using Java



This example loads an image from a BMP file and sends it to the default Windows printer. The sequence of function calls used is:
            bmpinfo     to determine the size of the image in the file
            allocimage  to allocate space for the image
            loadbmp     to load the image
            printimage  to send the image to the printer
            freeimage   to release the image buffer memory
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.

Load and print an image

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 bmpprint.java
java -classpath x:\java;x:\java\vic bmpprint

 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
import vic.*;                 // The Victor Java Native Interface

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

class bmpprint {
   public static void main(String args[]) {
   String loadfilename;
   int rcode = 0; 
   vic.imgdes timage = new vic.imgdes();
   vic.BITMAPINFOHEADER filedata = new vic.BITMAPINFOHEADER();
   rcode = vic.vic32jni.enableunicode(1);  // Turn on Unicode if available
            // load bmp 
   loadfilename = "test.bmp";
   rcode = vic.vic32jni.bmpinfo(loadfilename, filedata);
   if(rcode == vic.vic32jni.NO_ERROR) {
      rcode = vic.vic32jni.allocimage(timage, filedata.biWidth, filedata.biHeight, (int)filedata.biBitCount);
      if(rcode == vic.vic32jni.NO_ERROR) {
         System.out.println("Allocation successful, loading bmp file");
         System.out.println("  image width in pixels = " + filedata.biWidth);
         System.out.println("  image length in pixels = " + filedata.biHeight);
         System.out.println("  buffer width in bytes = " + timage.buffwidth);
         rcode = vic32jni.loadbmp(loadfilename, timage);
         displayimgdes(timage);  // Put image info on the screen
         if(rcode == vic.vic32jni.NO_ERROR) {
            int hWnd;
            int hdcprn;
            int prtmode;
            RECT prtrect = new vic.RECT();
            int bwidth;
            DSPFCT prtfunc= new vic.DSPFCT();
            System.out.println("File load successful, sending to printer");
            hWnd = vic.vic32jni.GetDesktopWindow();
            hdcprn = vic.vic32jni.GetPrinterDC();
            prtmode = vic.vic32jni.PRTDEFAULT;
            prtrect.left = 0;
            prtrect.top = 0;
            prtrect.right = 3000;  // 3 inches wide
            prtrect.bottom = (prtrect.right - prtrect.left) * timage.biheight / timage.biwidth + prtrect.top;
            bwidth = 0;            // border width
            rcode = vic.vic32jni.printimage(hWnd, hdcprn, prtmode, timage, prtrect, bwidth, prtfunc);
            System.out.println("Printimage rcode = " + rcode);
            }
         vic.vic32jni.freeimage(timage);
         }
      }
   } // end of main()
   public static void displayimgdes(imgdes rimage) {
   System.out.println("     Image buffer address        " + rimage.ibuff);        
   System.out.println("     Image area to be processed  " + rimage.stx +", "+ rimage.sty +", "+ rimage.endx +", "+ rimage.endy); 
   System.out.println("     Image buffer width in bytes " + rimage.buffwidth);    
   System.out.println("     Address of palette          " + rimage.palette);      
   System.out.println("     Palette colors              " + rimage.colors);       
   System.out.println("     Image type                  " + rimage.imgtype);      
   System.out.println("     BITMAPINFOHEADER address    " + rimage.bmh);          
   System.out.println("     DIB handle                  " + rimage.hBitmap);      
   System.out.println("     Pixel width of image        " + rimage.biwidth);      
   System.out.println("     Pixel length of image       " + rimage.biheight);     
   System.out.println("     Pixel depth of image        " + rimage.bibitcount);   
   }
}

Victor Image Processing Library

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




Copyright 1997 - 2005 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.