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

📄 imageloader.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 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-2005 by Mathias Lux (mathias@juggle.at)
 * http://www.juggle.at, http://caliph-emir.sourceforge.net
 */

package at.lux.fotoannotation;

import at.knowcenter.caliph.objectcatalog.semanticscreator.IMBeeApplicationPanel;
import at.lux.components.ImageThumbPanel;
import at.lux.fotoannotation.panels.*;
import at.lux.imageanalysis.ColorLayout;
import at.lux.imageanalysis.EdgeHistogram;
import at.lux.imageanalysis.ScalableColor;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifDirectory;
import com.drew.metadata.exif.ExifReader;
import com.drew.metadata.iptc.IptcDirectory;
import com.drew.metadata.iptc.IptcReader;
import org.jaxen.JaxenException;
import org.jaxen.XPath;
import org.jaxen.jdom.JDOMXPath;
import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * ImageLoader
 * Date: 14.09.2002
 * Time: 21:28:49
 *
 * @author Mathias Lux, mathias@juggle.at
 */
public class ImageLoader extends Thread {
    public static DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();

    private JLabel status;
    private JFrame parent;
    private File img;
    private ImageThumbPanel imgPanel;
    private CreationPanel creationPanel;
    private TextDescriptionPanel textPanel;
    MetadataDescriptionPanel mdpanel;
    QualityPanel qPanel;
    IMBeeApplicationPanel bee;
    ColorLayoutPanel colorPanel;
    ShapePanel shapePanel;

    public ImageLoader(JLabel status, AnnotationFrame parent, File img, ImageThumbPanel imgPanel,
                       CreationPanel creationPanel, TextDescriptionPanel textPanel, MetadataDescriptionPanel mdpanel,
                       QualityPanel qPanel, IMBeeApplicationPanel bee, ColorLayoutPanel colorPanel, ShapePanel shapePanel) {
        this.status = status;
        this.parent = parent;
        this.img = img;
        this.imgPanel = imgPanel;
        this.creationPanel = creationPanel;
        this.textPanel = textPanel;
        this.mdpanel = mdpanel;
        this.qPanel = qPanel;
        this.bee = bee;
        this.colorPanel = colorPanel;
        this.shapePanel = shapePanel;
    }

    public void run() {
        parent.setEnabled(false);
        imgPanel.setWait(true);
        status.setText("Please wait while loading image " + img.toString());
        try {
            // loading the image
            BufferedImage image = ImageIO.read(new FileInputStream(img));
            imgPanel.setImage(image);
            imgPanel.repaint();
            // extracting information
            extractInformation(image);
            status.setText("Finished");
        } catch (IOException e) {
            status.setText("Error: " + e.toString());
        } finally {
            imgPanel.setWait(false);
            parent.setEnabled(true);
            imgPanel.repaint();
        }
        // Aufputzen ...
//        collectGarbage();

    }

    private void extractInformation(BufferedImage image) {
        // Standardwerte wiederherstellen:
        mdpanel.setToCurrentTime();
        creationPanel.resetTable();

        debug("Generating thumbnail ...");
        AnnotationToolkit.generateThumbnail(img);
        debug("Extracing information from the image itself");
        creationPanel.setBitsPerPixel(image.getColorModel().getPixelSize() + "");
        creationPanel.setImageSize(image.getWidth() + "", image.getHeight() + "");
        creationPanel.setFileSize(img.length() + "");
        if (img.getName().toLowerCase().endsWith(".jpg") || img.getName().toLowerCase().endsWith(".jpeg"))
            creationPanel.setFileFormat("JPEG");
        String textPanelContent = null;
        try {
            // ----------------------------
            // Reading XMP (Adobe Photoshop
            // specific metadata format)
            // ----------------------------
            File xmpFile;
            xmpFile = new File(img.getCanonicalPath() + ".xmp");
            if (xmpFile.exists()) {
                SAXBuilder builder;
                builder = new SAXBuilder();
                try {
                    Element xmpRoot;
                    String suppCat = "", xmpDesc = "", xmpWords = "";
                    xmpRoot = builder.build(xmpFile).getRootElement();
                    // Create the whole bunch of needed namespaces:
                    XPath path = null;
                    try {
                        // Supplemental Categories:
                        path = new JDOMXPath("//photoshop:SupplementalCategories/rdf:Bag/rdf:li");
                        path.addNamespace("dc", "http://purl.org/dc/elements/1.1/");
                        path.addNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
                        path.addNamespace("photoshop", "http://ns.adobe.com/photoshop/1.0/");
                        List results = path.selectNodes(xmpRoot);
                        if (results.size() > 0) {
                            StringBuffer buff = new StringBuffer();
                            for (Iterator i = results.iterator(); i.hasNext();) {
                                Element elem = (Element) i.next();
                                if (elem.getTextTrim().length() > 0) {
                                    buff.append(elem.getTextTrim());
                                    if (i.hasNext())
                                        buff.append(", ");
                                }
                            }
                            debug(buff.toString() + " found as supplemental categories");
                            suppCat = buff.toString();
                        } else {
                            debug("no supplementalCategories found ...");
                        }
                        // Description:
                        path = new JDOMXPath("//dc:description/rdf:Alt/rdf:li");
                        path.addNamespace("dc", "http://purl.org/dc/elements/1.1/");
                        path.addNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
                        results = path.selectNodes(xmpRoot);
                        if (results.size() > 0) {
                            StringBuffer buff = new StringBuffer();
                            for (Iterator i = results.iterator(); i.hasNext();) {
                                Element elem = (Element) i.next();
                                if (elem.getTextTrim().length() > 0) {
                                    buff.append(elem.getTextTrim());
                                    if (i.hasNext())
                                        buff.append(", ");
                                }
                            }
                            debug(buff.toString() + " found as dc description");
                            xmpDesc = buff.toString();
                        } else {
                            debug("no dc description found ...");
                        }
                        if (suppCat.length() > 0) {
                            suppCat = suppCat + ", ";
                        }
                        if (xmpDesc.length() > 0) {
                            xmpDesc = xmpDesc + ", ";
                        }
                        if (xmpWords.length() > 0) {
                            xmpWords = xmpWords + ", ";
                        }
                        textPanel.setDescriptionText(suppCat + xmpDesc + xmpWords);
                        textPanelContent = suppCat + xmpDesc + xmpWords;
                        // Description:
                        path = new JDOMXPath("//dc:subject/rdf:Bag/rdf:li");
                        path.addNamespace("dc", "http://purl.org/dc/elements/1.1/");
                        path.addNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
                        results = path.selectNodes(xmpRoot);
                        if (results.size() > 0) {
                            StringBuffer buff = new StringBuffer();
                            for (Iterator i = results.iterator(); i.hasNext();) {
                                Element elem = (Element) i.next();
                                if (elem.getTextTrim().length() > 0) {
                                    buff.append(elem.getTextTrim());
                                    if (i.hasNext())
                                        buff.append(", ");
                                }
                            }
                            debug(buff.toString() + " found as dc keywords");
                            xmpDesc = buff.toString();
                        } else {
                            debug("no dc keywords found ...");
                        }
                    } catch (JaxenException e) {
                        debug("Error handling XPath: " + e.toString());
                    }
                } catch (JDOMException e) {
                    // XMP not processable
                    debug("XMP not processable: " + e.toString());
                }
            } else {
                debug("XMP file " + xmpFile.getName() + "not present ... ");
            }
            // ----------------------------
            // Reading EXIF
            // ----------------------------
            debug("Extracing EXIF");
            status.setText("Extracting EXIF ...");
            // reading out EXIF and IPTC:
            Metadata metadata = new Metadata();
            new ExifReader(img).extract(metadata);
            new IptcReader(img).extract(metadata);

            // getting exif tags:
            Directory exifDirectory = metadata.getDirectory(ExifDirectory.class);
            try {
                Date exifdate = null;
                if (exifDirectory.containsTag(ExifDirectory.TAG_DATETIME_ORIGINAL))
                    exifdate = exifDirectory.getDate(ExifDirectory.TAG_DATETIME_ORIGINAL);
                else if (exifDirectory.containsTag(ExifDirectory.TAG_DATETIME))
                    exifdate = exifDirectory.getDate(ExifDirectory.TAG_DATETIME);
                else if (exifDirectory.containsTag(ExifDirectory.TAG_DATETIME_DIGITIZED))
                    exifdate = exifDirectory.getDate(ExifDirectory.TAG_DATETIME_DIGITIZED);
                if (exifdate != null) {
                    Calendar c = Calendar.getInstance();
                    c.setTime(exifdate);
                    String time = c.get(Calendar.YEAR)
                            + "-" + AnnotationToolkit.to2letters(c.get(Calendar.MONTH) + 1)
                            + "-" + AnnotationToolkit.to2letters(c.get(Calendar.DAY_OF_MONTH))
                            + "T" + AnnotationToolkit.to2letters(c.get(Calendar.HOUR_OF_DAY))
                            + ":" + AnnotationToolkit.to2letters(c.get(Calendar.MINUTE))
                            + ":" + AnnotationToolkit.to2letters(c.get(Calendar.SECOND));
                    creationPanel.setTime(time);
                    debug("Time set with Date-object: " + exifdate.toString());
                }
            } catch (MetadataException e) {
                e.printStackTrace();
            }
            Iterator tagIterator = exifDirectory.getTagIterator();
            while (tagIterator.hasNext()) {
                try {
                    Tag currentTag = (Tag) tagIterator.next();

⌨️ 快捷键说明

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