ImageIO省略步骤后为什么反而更慢了?

原先的逻辑:首先调用drawBasic()在文件夹里写入一张区域图片(_tmp.jpg)。然后调用drawContourByClip()时再将底图读入内存,组合等值面图像写入文件夹,最后删除区域图片(_tmp.jpg)。
我的想法:每一次执行代码都会生成一张新的区域图片。是不是可以把这一步省略(判断_tmp.jpg文件是否存在,若存在直接return)以节省时间。
出现的问题:修改完代码之后,drawBasic()方法确实跳过了,但是组合区域图像和等值面图像之后,ImageIO.write()的执行时间变长了,这是什么原因?

方法入口:

    public String draw() {

String fileRealPath = "";

if (this.clipBounds) {

this.outLine = MapUtils.readMapData(mapDataPath);

}

String tmpPath = this.filePath + "_tmp";

try {

logger.info("paint basic picture ...");

// 绘制区域图像

drawBasic(tmpPath);

logger.info("paint contour picture ...");

// 绘制等值面

if (this.clipBounds) {

fileRealPath = drawContourByClip(filePath, tmpPath);

} else {

fileRealPath = drawContour(filePath, tmpPath);

}

} catch (IOException e) {

e.printStackTrace();

}

return fileRealPath;

}

绘制区域图像:

    public void drawBasic(String basicFile) throws IOException {

BufferedImage base = transparencyImage(Transparency.BITMASK);

Graphics2D gBase = base.createGraphics();

// 填充地图边界线并填充为白色

if (outLine != null && outLine.size() > 0) {

borderPolygon(gBase, outLine, true);

}

OutputStream tmpStream = new FileOutputStream(basicFile + ".jpg");

ImageIO.write(base, "png", tmpStream);

tmpStream.close();

gBase.dispose();

base.flush();

}

合成等值面并绘制:

    public String drawContour(String realPath, String tmpPath) throws IOException {

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2 = image.createGraphics();

// 抗锯齿处理

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

// 绘制等值面以及等值线

if ((fillContour || lineStyle.show) && contourPolygons.size() > 0) {

drawPolygon(g2, contourPolygons);

}

// 点值

if (stationStyle.show) {

drawStation(g2);

}

// 重新打开等值面区域图像

File file = new File(tmpPath + ".jpg");

String fileRealPath = realPath + fileName + ".jpg";

// 图片装入内存

BufferedImage src = ImageIO.read(file);

g2.drawImage(src, 0, 0, width, height, null);

src.flush();

// 删除临时文件

boolean isDelTmp = file.delete();

if (!isDelTmp) {

logger.error("临时文件删除失败");

}

// 释放对象

g2.dispose();

// 保存文件

OutputStream out = new FileOutputStream(fileRealPath);

ImageIO.write(image, "png", out);

// 画出图例并合并

if (drawLegendStyle.show) {

drawAndMergeLegend(fileName);

}

// 释放对象

g2.dispose();

src.flush();

out.close();

image.flush();

logger.info("图片路径: " + fileRealPath);

return fileRealPath;

}

修改后的逻辑

public void drawBasic(String basicFilePath) throws IOException {

if (new File(basicFilePath + ".jpg").exists()) {

return;

}

// ......

}

public String drawContourByClip(String realPath, String tmpPath) throws IOException {

// ......

// 不删除临时文件

// boolean isDelTmp = file.delete();

// if (!isDelTmp) {

// logger.error("临时文件删除失败");

// }

// ......

}

修改前的执行结果:

修改后的执行结果:

以上是 ImageIO省略步骤后为什么反而更慢了? 的全部内容, 来源链接: utcz.com/p/944597.html

回到顶部