|
|
@@ -15,23 +15,23 @@ |
|
|
|
*/
|
|
|
|
package cn.afterturn.easypoi.cache;
|
|
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
import org.apache.poi.util.IOUtils;
|
|
|
|
|
|
|
|
import cn.afterturn.easypoi.cache.manager.POICacheManager;
|
|
|
|
import org.apache.poi.util.IOUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 图片缓存处理
|
|
|
|
*
|
|
|
|
* @author JueYue
|
|
|
|
* 2016年1月8日 下午4:16:32
|
|
|
|
* 2016年1月8日 下午4:16:32
|
|
|
|
*/
|
|
|
|
public class ImageCache {
|
|
|
|
|
|
|
@@ -39,10 +39,16 @@ public class ImageCache { |
|
|
|
.getLogger(ImageCache.class);
|
|
|
|
|
|
|
|
public static byte[] getImage(String imagePath) {
|
|
|
|
InputStream is = POICacheManager.getFile(imagePath);
|
|
|
|
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
|
|
|
|
InputStream is = POICacheManager.getFile(imagePath);
|
|
|
|
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
|
|
|
|
final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
|
|
|
|
try {
|
|
|
|
BufferedImage bufferImg = ImageIO.read(is);
|
|
|
|
int ch;
|
|
|
|
while ((ch = is.read()) != -1) {
|
|
|
|
swapStream.write(ch);
|
|
|
|
}
|
|
|
|
Image image = Toolkit.getDefaultToolkit().createImage(swapStream.toByteArray());
|
|
|
|
BufferedImage bufferImg = toBufferedImage(image);
|
|
|
|
ImageIO.write(bufferImg,
|
|
|
|
imagePath.substring(imagePath.lastIndexOf(".") + 1, imagePath.length()),
|
|
|
|
byteArrayOut);
|
|
|
@@ -52,9 +58,42 @@ public class ImageCache { |
|
|
|
return null;
|
|
|
|
} finally {
|
|
|
|
IOUtils.closeQuietly(is);
|
|
|
|
IOUtils.closeQuietly(swapStream);
|
|
|
|
IOUtils.closeQuietly(byteArrayOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static BufferedImage toBufferedImage(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;
|
|
|
|
}
|
|
|
|
}
|