exp

Location:NSW Australia
Flowers: 362, Eggs: 25

Comments

Java - How to Print An Image file directly to your printer in Java

The following is java code to print an image directly to your printer, this is suitable for printing barcodes, which are images created by java programme.

import java.io.FileInputStream;
import java.io.IOException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;

public class PrintImage {
static public void main(String args[]) throws Exception {
try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));

PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);

if (pss.length == 0)
throw new RuntimeException("No printer services available.");

PrintService ps = pss[0];
System.out.println("Printing to " + ps);

DocPrintJob job = ps.createPrintJob();

FileInputStream fin = new FileInputStream("YOurImageFileName.PNG");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);

job.print(doc, pras);

fin.close();
} catch (IOException ie) {
ie.printStackTrace();
} catch (PrintException pe) {
pe.printStackTrace();
}
}
}
2012-06-28 21:45:06, views: 1568, Comments: 1
   
0
1
`

More articles

1. crazy domain's reseller account is useless (PIC) (6 comments) (13 21:37:55)
2. Small shopping street at night (PIC) (1 comments) (13 18:44:38)
3. Rugby race in Kogarah Park (PIC) (2 comments) (13 14:34:18)
5. Dymo LabelWriter 450 (PIC) (3 comments) (28 19:44:40)
6. Paypal return policy should change (4 comments) (17 15:11:47)
1 - 7 [ 7]
zebratrade