Java 图片绘制的方法

long beginTime = System.currentTimeMillis();
		int width = 560;
		int height = 250;
		//绘制的图片的大小
		BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_BGR);  
		//创建画布
		Graphics graphics = image.createGraphics();
		graphics.setColor(Color.WHITE);
		//设置大小
		graphics.fillRect(0, 0, width, height);
		graphics.setColor(Color.BLACK);
		//斜体Font.LAYOUT_NO_START_CONTEXT
		graphics.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 12));
		try {
			//读取文件
			String uri = "udtfile/8607/P05.000000.8607";
			File file = new File(uri);
//			List<String> lists = FileUtils.readLines(file, "GBK");
			//文件流
			List<String> lists = IOUtils.readLines(new FileInputStream(file), "GBK");
			lists.remove(0);
			//			 lists.remove(0);
			//在固定位置绘制图片
			//小图片的路径  可以放一个byte
			ImageIcon imgIcon = new ImageIcon("D:/temp/temp.jpg");   
			//得到Image对象。  
			Image img = imgIcon.getImage();  
			//将小图片绘到大图片上。  
			//5,300 .表示你的小图片在大图片上的位置。  
			graphics.drawImage(img, 310, 75, 110, 110, null);
			//设置颜色。  
			graphics.setColor(Color.BLACK);  
			for (int i = 0; i < lists.size(); i++) {
				graphics.drawString(lists.get(i), 2, 12*i);
			}
			graphics.dispose();
//			ByteArrayOutputStream out = new ByteArrayOutputStream();
//			boolean flag = ImageIO.write(image, "JPEG", out);
//			byte[] binaryData = out.toByteArray();
//			String base64BYTE =  org.apache.commons.net.util.Base64.encodeBase64String(binaryData);
			ImageIO.write(image, "JPEG", new File("D:/temp/qwer.jpg"));
			System.out.println("生图片成功");
			long endTime = System.currentTimeMillis();
			long times = endTime - beginTime;
			System.out.println(times);

			//			//构建打印请求属性集  
			//			HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
			//			//设置打印格式,因为未确定类型,所以选择autosense  
			//			DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; 
			//			//定位默认的打印服务  
			//			PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); 
			//			DocPrintJob job = defaultService.createPrintJob(); //创建打印作业  
			//			DocAttributeSet das = new HashDocAttributeSet();
			//			Doc doc = new SimpleDoc(new FileInputStream(new File("D:/qwer.jpg")), flavor, das);  
			//			job.print(doc, pras);  
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值