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.docattributeset;import Javax.print.attribute.hashdocattributeset;import Javax.print.attribute.hashprintrequestattributeset;import Javax.print.attribute.printrequestattributeset;import Javax.print.attribute.standard.copies;import Javax.print.attribute.standard.mediaprintablearea;import javax.print.attribute.standard.OrientationRequested; Import Javax.print.attribute.standard.printquality;public class Printimage {public void DrawImage (String fileName, int Count) {try {docflavor DOF = null;if (Filename.endswith (". gif")) {DOF = Docflavor.input_stream. GIF;} else if (Filename.endswith (". jpg")) {DOF = Docflavor.input_stream. JPEG;} else if (Filename.endswith (". png")) {DOF = Docflavor.input_stream.PNG;} Printservice PS = Printservicelookup.lookupdefaultprintservice (); Printrequestattributeset Pras = new Hashprintrequestattributeset ();p ras.add (orientationrequested.portrait); Pras.add (New Copies (count));p Ras.add (Printquality.high);D ocattributeset das = new Hashdocattributeset ();// Sets the size of the printed paper (in millimeters) das.add (new Mediaprintablearea (0, 0,, 296, mediaprintablearea.mm)); FileInputStream fin = new FileInputStream (fileName);D oc Doc = new Simpledoc (Fin, DOF, das);D ocprintjob job = Ps.createprin Tjob (); Job.print (Doc, Pras); Fin.close ();} catch (IOException IE) {ie.printstacktrace ();} catch (Printexception pe) {pe.printstacktrace ()}} public static void Main (string[] args) {new Printimage (). DrawImage ("C:\\Documents and Settings\\administrator\\my Documents\\my PICTURES\\IMAGE\\CMMI3 Certificate Scan 1.jpg ", 1);}}
Java calls the printer to print pictures