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();
}
Java 图片绘制的方法
于 2016-10-24 14:41:30 首次发布