600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > poi导出图片失真变为红色和图片背景变为黑色

poi导出图片失真变为红色和图片背景变为黑色

时间:2020-04-13 00:08:52

相关推荐

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。