ImageIcon Image Animator 3 linux cpu使用率特别高

汪雷_007 发布于 2016/01/05 13:08
阅读 87
收藏 0

ImageIcon Image Animator 3


压缩代码

private static BufferedImage getImage(String srcImgPath) {
Image image = Toolkit.getDefaultToolkit().getImage(srcImgPath);
if (image instanceof BufferedImage) {
return (BufferedImage)image;
}
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) {
logger.error(e.getMessage());
}
if (bimage == null) {
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}



try {
String imgSuffix = getImageSuffix(srcImgPath);
File dstImgFile = new File(dstImgPath); // 缩略图文件
BufferedImage srcImgBuf = getImage(srcImgPath);
int width = srcImgBuf.getWidth();// 原图宽度
int height = srcImgBuf.getHeight();// 原图高度
// 是否需要缩放
if (width > scalePix || height > scalePix) {
int scaleWidth = scalePix;// 缩略图宽度
int scaleHeight = (int) (height * scaleWidth / width);// 缩略图高度,以宽度为基准,等比例缩放图片
if (scaleHeight > scalePix) {
scaleHeight = scalePix;
scaleWidth = (int) (width * scaleHeight / height);// 以高度为基准,等比例缩放图片
}
double sx = (double) scaleWidth / width;
double sy = (double) scaleHeight / height;
BufferedImage dstImgBuf = null;
int type = srcImgBuf.getType();
if (type == BufferedImage.TYPE_CUSTOM) {
ColorModel cm = srcImgBuf.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(
scaleWidth, scaleHeight);
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
dstImgBuf = new BufferedImage(cm, raster,
isAlphaPremultiplied, null);
} else {
dstImgBuf = new BufferedImage(scaleWidth, scaleHeight, type);
Graphics2D g = dstImgBuf.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(srcImgBuf, AffineTransform
.getScaleInstance(sx, sy));
g.dispose();
}
String imgType = "jpeg";
if ("png".equalsIgnoreCase(imgSuffix)) {
imgType = "png";
}
ImageIO.write(dstImgBuf, imgType, dstImgFile);
}
} catch (IOException e) {
logger.info(e);
}

加载中
返回顶部
顶部