最近用aspose words做导出Word的功能,发现图片的导出有点难受,一开始是这样写的 

 1 Document doc = new Document("D:\\Template.docx");
 2 List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
 3  
 4 String imagePath = "D:\\employees.jpg";
 5 //读取一个二进制图片 
 6 FileInputStream fis = new FileInputStream(imagePath);
 7 byte[] image = new byte[fis.available()];
 8 fis.read(image);
 9 fis.close();
10 record.put("PhotoBLOB", image);
11  
12 dataList.add(record);

这样写没错,就是待会会因为图片的大小而把表格撑开

 

就是这样,所以,我们需要把图片压缩到我们想要的结果后再导出来

 1 String a = Global.getConfig("vfs.pr") + MapUtils.getString(map, "NEWFILESPATH");
 2 File f = new File(a);
 3 String newjpg = Global.getConfig("vfs.pr") + System.currentTimeMillis() + "." + f.getName().substring(f.getName().lastIndexOf(".") + 1);
 4 FileOutputStream out = new FileOutputStream(newjpg);
 5 thumbnail(f, out);//压缩图片的函数
 6 out.close();
 7 FileInputStream fileInputStream = new FileInputStream(newjpg);
 8 byte[] image = new byte[fileInputStream.available()];
 9 fileInputStream.read(image);
10 fileInputStream.close();
11 record.put("FILESPATH", image);
 1  /**
 2      * 按照固定宽高原图压缩
 3      *
 4      * @param img
 5      * @param out
 6      * @throws IOException
 7      */
 8     public static void thumbnail(File img, OutputStream out) throws IOException {
 9         BufferedImage BI = ImageIO.read(img);
10         int width = 95;
11         int height = 65;
12         //文件后缀名
13         String a = img.getName().substring(img.getName().lastIndexOf(".") + 1);
14         Image image = BI.getScaledInstance(width, height, Image.SCALE_SMOOTH);
15         BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
16         Graphics g = tag.getGraphics();
17         g.drawImage(image, 0, 0, null); // 绘制处理后的图
18         g.dispose();
19         ImageIO.write(tag, a, out);
20     }

这样图片就会按照95, 65的场合宽压缩出来了,不过到这里需要注意的是,在这里图片后缀是jpg压缩出来顶多一般就是失真问题,png就不一样了,是直接黑底的。

 

 

 所以我需要再添加对png图片的处理

 1  /**
 2      * 按照固定宽高原图压缩
 3      *
 4      * @param img
 5      * @param out
 6      * @throws IOException
 7      */
 8     public static void thumbnail(File img, OutputStream out) throws IOException {
 9         BufferedImage BI = ImageIO.read(img);
10         int width = 95;
11         int height = 65;
12         //文件后缀名
13         String a = img.getName().substring(img.getName().lastIndexOf(".") + 1);
14         if (a.equalsIgnoreCase("jpg")) {
15             Image image = BI.getScaledInstance(width, height, Image.SCALE_SMOOTH);
16             BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
17             Graphics g = tag.getGraphics();
18             g.drawImage(image, 0, 0, null); // 绘制处理后的图
19             g.dispose();
20             ImageIO.write(tag, a, out);
21         }
22         //对png的读取压缩 
23         else if (a.equalsIgnoreCase("png")) {
24             BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
25             Graphics2D g2d = tag.createGraphics();
26             tag = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
27             g2d.dispose();
28             g2d = tag.createGraphics();
29             Image image = BI.getScaledInstance(width, height, BI.SCALE_AREA_AVERAGING);
30             g2d.drawImage(image, 0, 0, null); // 绘制处理后的图
31             g2d.dispose();
32             ImageIO.write(tag, a, out);
33         }
34     }

这样就可以了

我前面有发过对aspose word导出word的简单介绍,可以结合这个和那篇一起了解。

简单介绍aspose-words-18.10-jdk16做导出word ,链接:

https://www.cnblogs.com/a874699615/p/10161842.html

csdn博客链接:

https://blog.csdn.net/qq_32003379/article/details/85255127

 

版权声明:本文为a874699615原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/a874699615/p/10181828.html