日期:2014-05-20 浏览次数:21136 次
BufferedImage img = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
Graphics g = img.createGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, 300, 300);
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.JPEG;
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
try {
ImageIO.write(img, "jpg", outstream);
}
catch (IOException e) {
e.printStackTrace();
}
byte[] buf = outstream.toByteArray();
InputStream stream = new ByteArrayInputStream(buf);
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrinterResolution printerResolution = new PrinterResolution(300, 300, PrinterResolution.DPI);
PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
attr.add(printerResolution);
attr.add(PrintQuality.HIGH);
PrintService services = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = services.createPrintJob();
try {
job.print(myDoc, aset);
}
catch (PrintException pe) {
pe.printStackTrace();
}