poi导出图片失真变为红色和图片背景变为黑色
插入图片:
private void setPicture(HSSFWorkbook wb, HSSFPatriarch patriarch, String pic, int iRowStart, int iColStart, int iRowStop, int iColStop) throws IOException {// 判断文件是否存在File imageFile = new File(pic);Boolean flag = false;String suffix = pic.substring(pic.lastIndexOf(".") + 1);//网络文件先下载到本地,再删除if (pic.startsWith("http")) {flag = true;//new一个URL对象URL url = new URL(pic);//打开链接HttpURLConnection conn = (HttpURLConnection) url.openConnection();//设置请求方式为"GET"conn.setRequestMethod("GET");//超时响应时间为5秒conn.setConnectTimeout(5 * 1000);//通过输入流获取图片数据InputStream inStream = conn.getInputStream();//得到图片的二进制数据,以二进制封装得到数据,具有通用性byte[] data = this.readInputStream(inStream);//new一个文件对象用来保存图片,默认保存当前工程根目录String[] split = pic.split("/");if (null != split && split.length > 0) {pic = split[split.length - 1].substring(0, split[split.length - 1].indexOf("?"));}imageFile = new File(pic);//创建输出流FileOutputStream outStream = new FileOutputStream(imageFile);//写入数据outStream.write(data);//关闭输出流outStream.close();}if (imageFile.exists()) {// 图片处理ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();//BufferedImage bufferImg = ImageIO.read(imgFile);java.awt.Image src = Toolkit.getDefaultToolkit().getImage(imageFile.getPath());BufferedImage bufferImg = this.toBufferedImage(src);//Image to BufferedImageImageIO.write(bufferImg, "jpg", byteArrayOut);//默认int pictureIndex = HSSFWorkbook.PICTURE_TYPE_JPEG;// 左,上(0-255),右(0-1023),下HSSFClientAnchor anchor = new HSSFClientAnchor(20, 1, 1018, 0, (short) (iColStart), iRowStart, (short) (iColStop), iRowStop);patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), pictureIndex));if (flag) {imageFile.delete();}}}private void setTitlePicture(HSSFWorkbook wb, HSSFPatriarch patriarch, String pic, int iRowStart, int iColStart, int iRowStop, int iColStop) throws IOException {// 判断文件是否存在File imageFile = new File(pic);Boolean flag = false;String suffix = pic.substring(pic.lastIndexOf(".") + 1);//网络文件先下载到本地,再删除if (pic.startsWith("http")) {flag = true;//new一个URL对象URL url = new URL(pic);//打开链接HttpURLConnection conn = (HttpURLConnection) url.openConnection();//设置请求方式为"GET"conn.setRequestMethod("GET");//超时响应时间为5秒conn.setConnectTimeout(5 * 1000);//通过输入流获取图片数据InputStream inStream = conn.getInputStream();//得到图片的二进制数据,以二进制封装得到数据,具有通用性byte[] data = this.readInputStream(inStream);//new一个文件对象用来保存图片,默认保存当前工程根目录String[] split = pic.split("/");if (null != split && split.length > 0) {pic = split[split.length - 1].substring(0, split[split.length - 1].indexOf("?"));}imageFile = new File(pic);//创建输出流FileOutputStream outStream = new FileOutputStream(imageFile);//写入数据outStream.write(data);//关闭输出流outStream.close();}if(null==pic||"".equals(pic.trim())){/*imageFile = new File("temp.jpg");//创建输出流InputStream is = this.getClass().getClassLoader().getResourceAsStream("/logo.jpg");if(null==is) {is = this.getClass().getResourceAsStream("/logo.jpg");}byte[] data = this.readInputStream(is);FileOutputStream outStream = new FileOutputStream(imageFile);//写入数据outStream.write(data);//关闭输出流outStream.close();*///创建输出流URL url = this.getClass().getClassLoader().getResource("/logo.jpg");if(null==url) {url = this.getClass().getResource("/logo.jpg");}imageFile = new File(url.getPath());}if (imageFile.exists()) {// 图片处理ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();//java.awt.Image src = Toolkit.getDefaultToolkit().getImage(imageFile.getPath());//BufferedImage bufferImg = this.toBufferedImage(src);//Image to BufferedImageBufferedImage bufferImg = this.toBufferedImage1(imageFile);ImageIO.write(bufferImg, "png", byteArrayOut);//默认int pictureIndex = HSSFWorkbook.PICTURE_TYPE_PNG;// 左,上(0-255),右(0-1023),下HSSFClientAnchor anchor = new HSSFClientAnchor(20, 1, 1018, 0, (short) (iColStart), iRowStart, (short) (iColStop), iRowStop);patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), pictureIndex));if (flag) {imageFile.delete();}}}
解决图片问题:
//解决图片失真,变为红色private BufferedImage toBufferedImage(java.awt.Image image) {if (image instanceof BufferedImage) {return (BufferedImage) image;}// This code ensures that all the pixels in the image are loadedimage = new ImageIcon(image).getImage();BufferedImage bimage = null;GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();try {int transparency = Transparency.OPAQUE;GraphicsDevice gs = ge.getDefaultScreenDevice();GraphicsConfiguration gc = gs.getDefaultConfiguration();bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);} catch (HeadlessException e) {// The system does not have a screen}if (bimage == null) {// Create a buffered image using the default color modelint type = BufferedImage.TYPE_INT_RGB;bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);}// Copy image to buffered imageGraphics g = bimage.createGraphics();// Paint the image onto the buffered imageg.drawImage(image, 0, 0, null);g.dispose();return bimage;}//解决图片背景变为黑色private BufferedImage toBufferedImage1(File file) {try{BufferedImage bimage = ImageIO.read(file);int width = bimage.getWidth();int height = bimage.getHeight();double pid = (double) 140 / (double) width;width = (int) (width * pid);height = (int) (height * pid);BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics2D g = buffer.createGraphics();buffer = g.getDeviceConfiguration().createCompatibleImage(width,height,Transparency.TRANSLUCENT);g.dispose();g = buffer.createGraphics();java.awt.Image small = bimage.getScaledInstance(width, height, java.awt.Image.SCALE_AREA_AVERAGING);g.drawImage(small, 0, 0, null);g.dispose();return buffer;}catch(Exception e){e.printStackTrace();return null;}}
poi导出图片失真变为红色和图片背景变为黑色相关推荐
- JAVA根据word模版使用poi导出word文档,包含图片、文字
模版word文件,内容和表格都已处理,保留了字体和样式,图片可以指定大小,docx类型的模版字段有问题的话,整个字段复制进去即可,不要手敲${} 生成的word文件 /*** word工具类** @a ...
- poi导出数据到word,带图片且图片数量不确定(能确定数量范围,这里是3-20张)
注:有更好解决方法,参考:https://blog.csdn.net/m0_49605579/article/details/122583318 1.导入依赖 maven版: <dependen ...
- Adobe Illustrator(ai)导出emf/wmf渐变图片失真色块
参考文献:https://jingyan.baidu.com/album/b87fe19e935c9052183568fc.html?picindex=1 软件环境:Adobe Illustrator ...
- java利用poi导出excel功能-附带图片导出
java利用poi导出excel功能-附带图片导出 写在前面 最近刚离职,闲来无事,于是把上两家公司都有碰到过的需求但都没有去研究实现:即导出带图片的excel报表.于是就折腾了一下这个功能,研究出来 ...
- matlab图片导出无失真库export_fig介绍(半透明效果)
matlab图片导出无失真半透明等功能的库export_fig介绍 首先,感谢export_fig的作者Yair Altman为相关方面做了很多介绍,本文主要结合新版本matlab,对作者的内容进行搬 ...
- java中使用poi导出ppt(图片和表格)
java使用POI导出PPT(超简单方法,包含图片和表格) 在做项目中遇到一个需求,将职员的信息导出成一个形式固定的ppt文档,poi有许多方法可以实现,因为我是一名Java小白,于是便想用最简单的方 ...
- poi导出Excel+图片
引入依赖 <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifa ...
- java使用POI导出图片到Excel
个人学习记录 目录 个人学习记录 1. 使用POI导出图片到Excel中,Excel格式为xls 2. 使用POI导出图片到Excel中,Excel格式为xlsx,图片设置边距 3. 获取图片,生成B ...
- POI 导出Excel 带图片导出 使用XSSFWorkbook
参考链接:POI导出图片到Excel不生效 (2007以上版本)_rj_han的博客-CSDN博客 //数据源 查询库 List<FormMt> mtList = formMtMapper ...
最新文章
- 关于不同的MySQL复制解决方案概述
- linux 内核参数 杨,Linux 内核参数
- boost第 4 章 事件处理
- 今天成功的将一个对1,000,000条记录的查询从30'提升到1'以下,庆祝一下
- SQL---------表的约束
- Javascript 中的非空判断 undefined,null, NaN的区别
- python 在window 系统 连接并操作远程 oracle 数据库
- 3.in_array低性能问题
- 实现斗地主洗牌、发牌、看牌
- wps中的相交_如何在wps中添加交叉引用 - 卡饭网
- 3步快速彻底卸载MySQL
- 了解工作分解结构(WBS)
- mysql索引失效的原因
- A problem occurred configuring root project ‘xxx‘.
- H3C MSR3020路由NQA实例配置
- 程序员职业发展路线图(完整版+珍藏版)
- LaTex绘制跨行跨列的三线表
- C# WebForm
- 2023-04-03 Linux中杀死进程kill和killall命令的区别,着重介绍killall
- 如何注册登录Google浏览器