Html Ant任务从PNG和JPEG文件中提取图像尺寸(高度、宽度)?
根据谷歌页面速度建议,我想“优化浏览器渲染” 指定所有对象的宽度和高度 图像允许通过以下方式更快地渲染: 消除不必要的 回流和重新绘制 我正在研究如何遍历静态内容项目中的图像文件(PNG、JPEG),并输出一个包含每个图像文件的路径和文件名以及高度和宽度(以像素为单位)的文件。然后,通过使用src属性数据查找要用于高度和宽度属性的值,我将使用它来帮助我构造标记Html Ant任务从PNG和JPEG文件中提取图像尺寸(高度、宽度)?,html,optimization,ant,browser,Html,Optimization,Ant,Browser,根据谷歌页面速度建议,我想“优化浏览器渲染” 指定所有对象的宽度和高度 图像允许通过以下方式更快地渲染: 消除不必要的 回流和重新绘制 我正在研究如何遍历静态内容项目中的图像文件(PNG、JPEG),并输出一个包含每个图像文件的路径和文件名以及高度和宽度(以像素为单位)的文件。然后,通过使用src属性数据查找要用于高度和宽度属性的值,我将使用它来帮助我构造标记 \images\logo.png,100,25 我的第一个想法是寻找一个ANT任务,因为我们的静态内容构建用于其他目的(比如在Java
\images\logo.png,100,25
我的第一个想法是寻找一个ANT任务,因为我们的静态内容构建用于其他目的(比如在JavaScript和CSS文件上使用)。我也愿意接受其他想法,包括解决这个问题的其他方法。我希望不必手动完成这项工作。我不知道有这样的ant任务随时可用,但编写一个任务应该相对简单。在PNG格式中,图像大小存储在IHDR头文件的开头。例如,Google上有许多PNG解析器示例。在ant任务中完成它。您可以尝试这个,我就是为了这个问题而做的。这里是我迄今为止实现的(需要测试和清理)。基本上,用于让我开始一项Ant任务并提取图像维度。在部署之前,我将与matt的答案进行比较 我的项目中的测试生成脚本:
<project name="ImagesTask" basedir="." default="test">
<target name="init">
<taskdef name="images" classname="ImageInfoTask" classpath="..\dist\ImageTask.jar"/>
</target>
<target name="test" depends="init">
<images outputFile="data/images.xml">
<fileset dir="data" includes="images/**/*.jpg"/>
<fileset dir="data" includes="images/**/*.gif"/>
<fileset dir="data" includes="images/**/*.png"/>
</images>
</target>
</project>
Java源代码(不带导入):
公共类ImageInfoTask扩展了任务{
私有字符串输出文件;
private List fileSetList=new ArrayList();
私有打印流输出文件流;
public void setOutputFile(字符串outputFile){
this.outputFile=outputFile.replace(“/”,File.separator);
}
public void addFileset(文件集文件集){
添加(文件集);
}
受保护的void validate(){
if(outputFile==null){
抛出新的BuildException(“未设置文件”);
}
if(fileSetList.size()<1){
抛出新的BuildException(“未设置文件集”);
}
}
受保护的void openOutputFile()引发IOException{
FileOutputStream out=新的FileOutputStream(this.outputFile);
//将打印流连接到输出流
this.outputFileStream=新的打印流(out,true,“UTF-8”);
this.outputFileStream.println(“”);
}
受保护的void writeImgToOutputFile(字符串文件名,维度dim){
字符串imgTag=“”;
this.outputFileStream.println(imgTag);
}
受保护的void closeOutputFile(){
this.outputFileStream.println(“”);
this.outputFileStream.close();
}
@凌驾
public void execute(){
验证();
试一试{
openOutputFile();
for(迭代器itFSets=fileSetList.Iterator();itFSets.hasNext();){
FileSet fs=(FileSet)itFSets.next();
DirectoryScanner ds=fs.getDirectoryScanner(getProject());
字符串[]includedFiles=ds.getIncludedFiles();
对于(int i=0;i
我已经实现了这一点,但在您回答之前没有更新此问题。我把你的回答标为已被接受。谢谢。我使用了相同的ant教程来了解如何使用文件集,所以我认为这两种实现在这方面非常相似。我对ImageIO了解不多,所以我只是将文件读入一个BuffereImage并从中获取尺寸。这就是为什么我需要设置java.awt.headless——我在运行脚本时看到一个java菜单栏闪烁。我怀疑您使用的“纯”ImageIO方法可能会比我的版本快一点,即使代码有点冗长。无论如何,这是一个有趣的周日晚上的小项目。
public class ImageInfoTask extends Task {
private String outputFile;
private List fileSetList = new ArrayList();
private PrintStream outputFileStream;
public void setOutputFile(String outputFile) {
this.outputFile = outputFile.replace("/", File.separator);
}
public void addFileset(FileSet fileset) {
fileSetList.add(fileset);
}
protected void validate() {
if (outputFile == null) {
throw new BuildException("file not set");
}
if (fileSetList.size() < 1) {
throw new BuildException("fileset not set");
}
}
protected void openOutputFile() throws IOException {
FileOutputStream out = new FileOutputStream(this.outputFile);
// Connect print stream to the output stream
this.outputFileStream = new PrintStream(out, true, "UTF-8");
this.outputFileStream.println("<images>");
}
protected void writeImgToOutputFile(String filename, Dimension dim) {
String imgTag = " <img src=\"/" + filename.replace("\\", "/")
+ "\" height=\"" + dim.height + "\" width=\"" + dim.width
+ "\" />";
this.outputFileStream.println(imgTag);
}
protected void closeOutputFile() {
this.outputFileStream.println("</images>");
this.outputFileStream.close();
}
@Override
public void execute() {
validate();
try {
openOutputFile();
for (Iterator itFSets = fileSetList.iterator(); itFSets.hasNext();) {
FileSet fs = (FileSet) itFSets.next();
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
String[] includedFiles = ds.getIncludedFiles();
for (int i = 0; i < includedFiles.length; i++) {
String filename = includedFiles[i];
Dimension dim = getImageDim(ds.getBasedir() + File.separator + filename);
if (dim != null) {
writeImgToOutputFile(filename, dim);
}
}
}
closeOutputFile();
} catch (IOException ex) {
log(ex.getMessage());
}
}
private Dimension getImageDim(final String path) {
Dimension result = null;
String suffix = this.getFileSuffix(path);
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try {
ImageInputStream stream = new FileImageInputStream(new File(path));
reader.setInput(stream);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
result = new Dimension(width, height);
} catch (IOException e) {
log(path + ": " + e.getMessage());
} finally {
reader.dispose();
}
}
return result;
}
private String getFileSuffix(final String path) {
String result = null;
if (path != null) {
result = "";
if (path.lastIndexOf('.') != -1) {
result = path.substring(path.lastIndexOf('.'));
if (result.startsWith(".")) {
result = result.substring(1);
}
}
}
return result;
}
}