插入图片:
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 BufferedImage
ImageIO.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 BufferedImage
BufferedImage 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 loaded
image = 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 model
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.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;
}
}