⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 annotationtoolkit.java

📁 图像检索的代码b
💻 JAVA
字号:
/*
 * This file is part of Caliph & Emir.
 *
 * Caliph & Emir is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Caliph & Emir is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Caliph & Emir; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * --------------------
 * (c) 2002-2004 by Mathias Lux (mathias@juggle.at)
 * http://www.juggle.at
 */

package at.lux.fotoannotation;

/*
 * Created by Mathias Lux, mathias@juggle.at
 * User: Mathias Lux, mathias@juggle.at
 * Date: 12.09.2002
 * Time: 22:01:22
 */

import org.jaxen.XPath;
import org.jaxen.jdom.JDOMXPath;
import org.jdom.Element;
import org.jdom.Namespace;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.ImageWriteParam;
import javax.imageio.IIOImage;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;

import com.sun.imageio.plugins.jpeg.JPEG;

/**
 * Toolkit class for Caliph, static variables, etc.
 *
 * @author Mathias Lux, mathias@juggle.at
 */

public class AnnotationToolkit {
    public static String AGENTS_FILE = "./agents.mp7.xml";
    public static String PROGRAM_NAME = "Caliph";
    public static String VERSION_NUMBER = "v0.9.13";

    public static List xpathQuery(org.jdom.Element document, String xpathQuery1, Namespace xNs) {
        List returnValue = new Vector();
        String xpathQuery = xpathQuery1;
        XPath xPath;
        try {
            // if there is a xmlns namespace wihtout a prefix, then
            // a default one has to be added. Otherwise jaxen does not work
            // Note: the prefix "x" has to be used in the xpath-query as well.
            Namespace ns = document.getNamespace();
            if (ns.getPrefix().length() == 0) {
                StringTokenizer tokens = new StringTokenizer(xpathQuery, "/", true);
                xpathQuery = "";
                while (tokens.hasMoreTokens()) {
                    String token = tokens.nextToken();
                    if (!token.equalsIgnoreCase("/")) {
                        token = "x:" + token;
                    }
                    xpathQuery += token;
                }

                xPath = new JDOMXPath(xpathQuery);
                xPath.addNamespace("x", ns.getURI());
            } else {
                xPath = new JDOMXPath(xpathQuery);
            }

            if (xNs != null) {
                xPath.addNamespace(xNs.getPrefix(), xNs.getURI());
            }

            // console.echo("OLD: \"" + xpathQuery1 + "\", NEW: \"" + xpathQuery + "\"");

            Object jdomNode = document; // Document, Element etc.
            //XPath path = new JDOMXPath("/*"); //--> das root element

            returnValue = xPath.selectNodes(jdomNode); // entries are from the type "org.jdom.Element"
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnValue;
    }

    /**
     * loest den Pfad des Files in eine URI auf und liefert den MediaLocator DS zur點k
     *
     * @param file java.io.File, zu dem die URI gefunden werden soll.
     * @return JDOM Element mit dem MediaLocator DS oder null wenn die URI zum File nicht bestimmt werden kann.
     */
    public static Element getMpeg7MediaInstance(File file) {
        Namespace mpeg7 = Namespace.getNamespace("", "urn:mpeg:mpeg7:schema:2001");
        Element inst = new Element("MediaInstance", mpeg7).addContent(new Element("InstanceIdentifier", mpeg7));
        if (file != null) {
            try {
                inst.addContent(new Element("MediaLocator", mpeg7).addContent(new Element("MediaUri",
                        mpeg7).addContent(file.getCanonicalFile().toURI().toString())));
            } catch (IOException e) {
                inst = null;
            }
        } else {
            inst = null;
        }
        return inst;
    }

    public static String getMpeg7Time() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        String t = c.get(Calendar.YEAR) + "-" + to2letters(c.get(Calendar.MONTH) + 1) + "-" + to2letters(c.get(Calendar.DAY_OF_MONTH))
                + "T" + to2letters(c.get(Calendar.HOUR_OF_DAY)) + ":" + to2letters(c.get(Calendar.MINUTE));
        return t;
    }

    public static String getMpeg7Time(Calendar c) {
        String t = c.get(Calendar.YEAR) + "-" + to2letters(c.get(Calendar.MONTH) + 1) + "-" + to2letters(c.get(Calendar.DAY_OF_MONTH))
                + "T" + to2letters(c.get(Calendar.HOUR_OF_DAY)) + ":" + to2letters(c.get(Calendar.MINUTE));
        return t;
    }

    public static String to2letters(int number) {
        String ret = null;
        // String min = null;
        if (number < 10)
            ret = ("0" + number);
        else
            ret = ("" + number);

        return ret;

    }

    public static File generateThumbnail(File f) {
        File thumb = null;
        try {
            String name = "tn_" + f.getName();
            String path = f.getParentFile().getCanonicalPath();
            thumb = new File(path + "/" + name);
            if (!thumb.exists()) {
                Image image = ImageIO.read(f);
                int x, y, max;
                double factor;
                x = image.getWidth(null);
                y = image.getHeight(null);
                if (x > y) {
                    max = x;
                    factor = 110.0 / x;
                } else {
                    max = y;
                    factor = 110.0 / y;
                }
                x = (int) Math.floor((double) x * factor);
                y = (int) Math.floor((double) y * factor);
                BufferedImage thumbnailImage = new BufferedImage(120, 120, BufferedImage.TYPE_INT_RGB);
                Graphics g2 = thumbnailImage.getGraphics();
                g2.setColor(Color.black);
                g2.fillRect(0, 0, 120, 120);
                g2.drawImage(image, (120 - x) / 2, (120 - y) / 2, x, y, null);
                // Find a jpeg writer
                ImageWriter writer = null;
                Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
                if (iter.hasNext()) {
                    writer = (ImageWriter) iter.next();
                } else {
                    System.err.print("Didn't find JPEG Writer -> could not create thumbnail");
                }

                // Prepare output file
                ImageOutputStream ios = ImageIO.createImageOutputStream(thumb);
                writer.setOutput(ios);

                // Set the compression quality
                ImageWriteParam iwparam = writer.getDefaultWriteParam();
                iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                iwparam.setCompressionQuality(0.9f);

                // Write the image
                writer.write(null, new IIOImage(thumbnailImage, null, null), iwparam);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return thumb;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -