代码之家  ›  专栏  ›  技术社区  ›  Kevin Hakanson

从PNG和JPEG文件中提取图像尺寸(高度、宽度)的Ant任务?

  •  5
  • Kevin Hakanson  · 技术社区  · 14 年前

    Specify image dimensions 以“优化浏览器渲染”

    图像允许通过 回流和重绘。

    我正在研究如何遍历静态内容项目中的图像文件(PNG、JPEG),并输出一个文件,其中包含每个图像文件的路径和文件名以及高度和宽度(以像素为单位)。然后,我将使用它来帮助我构造标记,方法是使用src属性数据查找要用于height和width属性的值。

    \images\logo.png,100,25
    

    我的第一个想法是寻找一个ANT任务,因为我们的静态内容构建使用 Ant 用于其他目的(如 YUI Compressor

    3 回复  |  直到 14 年前
        1
  •  4
  •   matt    14 年前

    你可以试试这个 https://github.com/mattwildig/image-size-report-task ,这是我为这个问题做的。

        2
  •  1
  •   Community CDub    8 年前

    这是我到目前为止实现的(需要测试和清理)。基本上,使用 Tutorial: Tasks using Properties, Filesets & Paths 让我开始一项蚂蚁任务 How to get image height and width using java?

    来自我的项目的测试生成脚本:

    <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>
    

    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;
        }
    }
    
        3
  •  0
  •   maximdim    14 年前

    我不知道这样的ant任务是现成的,但是写一个应该比较简单。在PNG格式中,图像大小存储在IHDR头文件的开头。Google上有很多PNG解析器的示例-例如 this . 在蚂蚁任务中完成它,你就完成了。