Dependent Javax.print
PackageCom.xgt.util;Importorg.apache.commons.io.IOUtils;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJavax.print.Doc;ImportJavax.print.DocFlavor;ImportJavax.print.DocPrintJob;Importjavax.print.PrintException;ImportJavax.print.PrintService;ImportJavax.print.PrintServiceLookup;ImportJavax.print.SimpleDoc;ImportJavax.print.attribute.DocAttributeSet;ImportJavax.print.attribute.HashDocAttributeSet;ImportJavax.print.attribute.HashPrintRequestAttributeSet;ImportJavax.print.attribute.PrintRequestAttributeSet;Importjavax.print.attribute.standard.Copies;ImportJavax.print.attribute.standard.MediaPrintableArea;Importjavax.print.attribute.standard.OrientationRequested;Importjavax.print.attribute.standard.PrintQuality; Public classPrintutil {Private Static FinalLogger Logger = Loggerfactory.getlogger (printutil.class); Public Static voidDrawImage (String FileName,intcount) {FileInputStream fin=NULL; 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=NewHashprintrequestattributeset (); Pras.add (orientationrequested.portrait); Pras.add (NewCopies (count)); Pras.add (Printquality.high); Docattributeset das=NewHashdocattributeset (); //set the size of the printed paper (in millimeters)Das.add (NewMediaprintablearea (0, 0, 210, 296, mediaprintablearea.mm)); Fin=NewFileInputStream (fileName); Doc Doc=NewSimpledoc (Fin, DOF, das); Docprintjob Job=Ps.createprintjob (); Job.print (Doc, Pras); Fin.close (); Logger.info ("Print successfully!" File: "+filename+" number is: "+count); } Catch(IOException IE) {ie.printstacktrace (); } Catch(printexception pe) {pe.printstacktrace (); }finally{ioutils.closequietly (FIN); } } Public Static voidMain (string[] args) {NewPrintutil (). DrawImage ("d:\\qrcodepng\\ synthesis map. png", 1); }}
Java calls the printer to print the specified path picture