자바 프린터 호출 사용자 정의 그림 인쇄

3656 단어 자바
package test;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
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.MediaPrintableArea;

public class QingPrint {
	public static void main(String[] args) throws Exception {

		//          
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
		//       ,       ,    autosense
		DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
		//             
		PrintService[] printService = PrintServiceLookup.lookupPrintServices(
				flavor, pras);
		//          
		if (printService.length == 0) {
			//     
			return;
		}
		//          
		PrintService defaultService = PrintServiceLookup
				.lookupDefaultPrintService();
		//       
		DocPrintJob job = defaultService.createPrintJob();
		DocAttributeSet das = new HashDocAttributeSet();
		das.add(new MediaPrintableArea(1, 1, 4,3, MediaPrintableArea.INCH));
		//       
		Doc doc = new SimpleDoc(getinputstream(), flavor, das);
		//        
		// PrintService service = ServiceUI.printDialog(null, 200,
		// 00,printService, defaultService, flavor, pras);
		// if (service != null) {
		//     
		job.print(doc, pras);
		getinputstream().close();
		// }
	}

	public static ByteArrayInputStream getinputstream() throws Exception {
		// ByteArrayInputStream inputStream;
		//         
		int width = 300, height = 200;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		//        
		Graphics g = image.getGraphics();
		//      
		g.setColor(new Color(250, 250, 250));
		g.fillRect(0, 0, width, height);
		//     
		g.setFont(new Font("  ", Font.BOLD, 18));
		//     155    ,                  
		g.setColor(new Color(0, 0, 0));

		// g.drawLine(0,0,50,50);

		//          (6   )
		g.drawString("  ", 10, 30);
		g.drawString("  ", 60, 30);
		g.drawString("  ", 110, 30);
		g.drawString("  ", 160, 30);
		g.drawString("  ", 210, 30);
		g.drawString("  ", 10, 60);
		g.drawString("  ", 60, 60);
		g.drawString("  ", 110, 60);
		g.drawString("  ", 160, 60);
		g.drawString("  ", 210, 60);
		g.drawString("  ", 10, 90);
		g.drawString("  ", 60, 90);
		g.drawString("  ", 110, 90);
		g.drawString("  ", 160, 90);
		g.drawString("  ", 210, 90);
		g.drawString("  ", 10, 120);
		g.drawString("  ", 60, 120);
		g.drawString("  ", 110, 120);
		g.drawString("  ", 160, 120);
		g.drawString("  ", 210, 120);
		g.drawString("  ", 10, 150);
		g.drawString("  ", 60, 150);
		g.drawString("  ", 110, 150);
		g.drawString("  ", 160, 150);
		g.drawString("  ", 210, 150);
		//     
		g.dispose();
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
		ImageIO.write(image, "JPEG", imageOut);
		imageOut.close();
		ByteArrayInputStream input = new ByteArrayInputStream(
				output.toByteArray());
		return input;
	}

}

좋은 웹페이지 즐겨찾기