java调用打印机打印自定义的图片

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;
	}

}

你可能感兴趣的:(java)