📄 imageloader.java
字号:
int tagType = exifDirectory.getInt(currentTag.getTagType());
status.setText("Extracting EXIF: " + currentTag.getTagName());
debug(exifDirectory.getTagName(tagType) + ": " + exifDirectory.getString(tagType));
creationPanel.addKeyValuePair(currentTag.getTagName(), currentTag.getDescription());
} catch (MetadataException e) {
System.err.println("Exception parsing tag .. " + e.toString());
}
}
// ----------------------------
// Reading IPTC
// ----------------------------
Directory iptcDirectory = metadata.getDirectory(IptcDirectory.class);
tagIterator = iptcDirectory.getTagIterator();
StringBuffer buff = new StringBuffer();
if (iptcDirectory.containsTag(IptcDirectory.TAG_CAPTION)) {
buff.append(iptcDirectory.getString(IptcDirectory.TAG_CAPTION) + "\n");
}
if (iptcDirectory.containsTag(IptcDirectory.TAG_SUPPLEMENTAL_CATEGORIES)) {
buff.append(iptcDirectory.getString(IptcDirectory.TAG_SUPPLEMENTAL_CATEGORIES) + "\n");
}
if (iptcDirectory.containsTag(IptcDirectory.TAG_KEYWORDS)) {
buff.append(iptcDirectory.getString(IptcDirectory.TAG_KEYWORDS) + "\n");
}
if (textPanelContent != null) {
textPanel.setDescriptionText(textPanelContent + "\n" + buff.toString());
} else {
textPanel.setDescriptionText(buff.toString());
}
while (tagIterator.hasNext()) {
try {
Tag currentTag = (Tag) tagIterator.next();
// int tagType = iptcDirectory.getInt(currentTag.getTagType());
status.setText("Extracting IPTC: " + currentTag.getTagName());
System.out.println("Extracting IPTC: " + currentTag.getTagName());
debug(currentTag.getTagName() + ": " + currentTag.getTagName());
creationPanel.addKeyValuePair("(IPTC) " + currentTag.getTagName(), currentTag.getDescription());
} catch (MetadataException e) {
System.err.println("Exception parsing tag .. " + e.toString());
}
}
creationPanel.updateTable();
} catch (IOException e) {
debug("IOException reading EXIF: " + e.getMessage());
} catch (JpegProcessingException e) {
e.printStackTrace();
}
/*
catch (ExifProcessingException e) {
// Keine EXIF Info?
debug("ExifProcessingException: " + e.getMessage());
// creating std tags ... (werden manuell bef黮lt)
creationPanel.addKeyValuePair("Make", "");
creationPanel.addKeyValuePair("Model", "");
// Setting time from file ... weil ja keine Info da ..
Calendar c = Calendar.getInstance();
c.setTime(new Date(img.lastModified()));
String time = AnnotationToolkit.getMpeg7Time(c);
creationPanel.setTime(time);
} catch (MetadataException e) {
e.printStackTrace();
}
*/
try {
String filePath = img.getCanonicalPath();
String mp7name = filePath.substring(0, filePath.lastIndexOf('.')) + ".mp7.xml";
File mp7file = new File(mp7name);
if (mp7file.exists()) {
debug("Reading existing MPEG-7 information " + mp7name);
status.setText("Reading existing MPEG-7 information");
// ----------------------------
// Reading from the MPEG-7 File
// ----------------------------
SAXBuilder builder = new SAXBuilder();
Element root = builder.build(mp7file).getRootElement();
// in case of the shape panel the whole document is
// used to set the shapes ...
shapePanel.setDescriptor(root);
// textual description of the File ..
String text = getSingleValue(root, "//Image/TextAnnotation/FreeTextAnnotation");
if (text != null) textPanel.setDescriptionText(text);
text = getSingleValue(root, "//Image/TextAnnotation/StructuredAnnotation/Who/Name");
if (text != null) textPanel.setWho(text);
text = getSingleValue(root, "//Image/TextAnnotation/StructuredAnnotation/When/Name");
if (text != null) textPanel.setWhen(text);
text = getSingleValue(root, "//Image/TextAnnotation/StructuredAnnotation/Where/Name");
if (text != null) textPanel.setWhere(text);
text = getSingleValue(root, "//Image/TextAnnotation/StructuredAnnotation/Why/Name");
if (text != null) textPanel.setWhy(text);
text = getSingleValue(root, "//Image/TextAnnotation/StructuredAnnotation/How/Name");
if (text != null) textPanel.setHow(text);
text = getSingleValue(root, "//Image/TextAnnotation/StructuredAnnotation/WhatAction/Name");
if (text != null) textPanel.setWhatAction(text);
text = getSingleValue(root, "//Image/TextAnnotation/StructuredAnnotation/WhatObject/Name");
if (text != null) textPanel.setWhatObject(text);
// description version
text = getSingleValue(root, "//DescriptionMetadata/Version");
if (text != null) mdpanel.setVersion(text);
// description tool
text = getSingleValue(root, "//DescriptionMetadata/Instrument/Tool/Name");
if (text != null) mdpanel.setInstrument(text);
// description date
text = getSingleValue(root, "//DescriptionMetadata/CreationTime");
if (text != null) mdpanel.setTime(text);
// description comment
text = getSingleValue(root, "//DescriptionMetadata/Comment/FreeTextAnnotation");
if (text != null) mdpanel.setTextDescription(text);
// quality & definition ob der Descriptor gebraucht wird ...
text = getSingleValue(root, "//Image/MediaInformation/MediaProfile/MediaQuality/QualityRating/RatingValue");
if (text != null) {
qPanel.setQuality(Integer.parseInt(text));
qPanel.setIncludeQuality(true);
} else {
qPanel.setIncludeQuality(false);
}
// quality agent
text = getSingleValue(root, "//Image/MediaInformation/MediaProfile/MediaQuality/RatingSource/Name/GivenName");
String text2 = getSingleValue(root, "//Image/MediaInformation/MediaProfile/MediaQuality/RatingSource/Name/FamilyName");
if (text != null && text2 != null) {
debug("setting quality agent: " + text2 + ", " + text);
qPanel.setAgent(text2 + ", " + text);
}
// description metadata agent
text = getSingleValue(root, "//DescriptionMetadata/Creator/Agent/Name/GivenName");
text2 = getSingleValue(root, "//DescriptionMetadata/Creator/Agent/Name/FamilyName");
if (text != null && text2 != null) {
debug("setting description metadata agent: " + text2 + ", " + text);
mdpanel.setAgent(text2 + ", " + text);
}
// creation agent
text = getSingleValue(root, "//CreationInformation/Creation/Creator/Agent/Name/GivenName");
text2 = getSingleValue(root, "//CreationInformation/Creation/Creator/Agent/Name/FamilyName");
if (text != null && text2 != null) {
debug("setting creation agent: " + text2 + ", " + text);
creationPanel.setAgent(text2 + ", " + text);
}
// semantics ...
java.util.List results = AnnotationToolkit.xpathQuery(root, "//Image/Semantic", null);
if (results.size() > 0) {
debug("setting bee semantic description ...");
bee.setSemantics((Element) ((Element) results.get(0)).detach());
}
// Scalable Color
boolean ccset = false, clset = false, ehset = false;
results = AnnotationToolkit.xpathQuery(root, "//VisualDescriptor", null);
if (results.size() > 0) {
for (Iterator it1 = results.iterator(); it1.hasNext();) {
Element elem = (Element) it1.next();
java.util.List atts = elem.getAttributes();
for (Iterator it2 = atts.iterator(); it2.hasNext();) {
Attribute att = (Attribute) it2.next();
if (att.getValue().equals("ScalableColorType")) {
debug("setting Scalable Color ...");
colorPanel.setScalableColor(new ScalableColor((Element) elem.detach()));
ccset = true;
} else if (att.getValue().equals("ColorLayoutType")) {
debug("setting Color Layout ...");
colorPanel.setColorLayout(new ColorLayout((Element) elem.detach()));
clset = true;
} else if (att.getValue().equals("EdgeHistogramType")) {
debug("setting EdgeHistogram ...");
try {
colorPanel.setEdgeHistogram(new EdgeHistogram((Element) elem.detach()));
ehset = true;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
if (!ccset) {
debug("Did not get the descriptor from XML: Extracing visual descriptor ScalableColor");
status.setText("Extracing visual descriptor ScalableColor ...");
colorPanel.setScalableColor(new ScalableColor(image));
}
if (!clset) {
debug("Did not get the descriptor from XML: Extracing visual descriptor ColorLayout");
status.setText("Extracing visual descriptor ColorLayout ...");
colorPanel.setColorLayout(new ColorLayout(image));
}
if (!ehset) {
debug("Did not get the descriptor from XML: Extracing visual descriptor EdgeHistogram");
status.setText("Extracing visual descriptor EdgeHistogram ...");
colorPanel.setEdgeHistogram(new EdgeHistogram(image));
}
debug("Finished reading MPEG-7 description");
AnnotationFrame.setDirty(false);
parent.setTitle(AnnotationFrame.TITLE_BAR + ": " + img.getName());
} else {
debug("No MPEG-7 Description found, i've searched for " + mp7name);
// here we check if the image is too big and scale it down:
// todo: configure via properties.
int maxSideLength = 640;
if (Math.max(image.getWidth(), image.getHeight()) > maxSideLength) {
status.setText("Scaling down image for extracing Descriptors ...");
double ow = image.getWidth();
double oh = image.getHeight();
double scale = 0.0;
if (ow > oh) {
scale = ((double) maxSideLength / ow);
}
else {
scale = ((double) maxSideLength / oh);
}
// create smaller image
BufferedImage img = new BufferedImage((int) (ow * scale), (int) (oh * scale), BufferedImage.TYPE_INT_RGB);
// fast scale
Graphics g = img.getGraphics();
g.drawImage(image, 0, 0, img.getWidth(), img.getHeight(), null);
// switch images
image = img;
}
debug("Extracing visual descriptor ColorLayout");
status.setText("Extracing visual descriptor ColorLayout ...");
colorPanel.setColorLayout(new ColorLayout(image));
debug("Extracing visual descriptor ScalableColor");
status.setText("Extracing visual descriptor ScalableColor ...");
colorPanel.setScalableColor(new ScalableColor(image));
status.setText("Extracing visual descriptor EdgeHistogram ...");
colorPanel.setEdgeHistogram(new EdgeHistogram(image));
shapePanel.setImage(image);
AnnotationFrame.DIRTY = true;
}
} catch (IOException e) {
debug("IOException while searching and reading existing MPEG-7 description " + e.toString());
} catch (JDOMException e) {
debug("Exception parsing existing MPEG-7 description" + e.toString());
}
}
private void debug(String message) {
if (AnnotationFrame.DEBUG)
System.out.println("[at.lux.fotoannotation.ImageLoader] " + message);
}
// private void collectGarbage() {
// debug("Mem: "
// + df.format(Runtime.getRuntime().freeMemory() / (1024.0 * 1024.0)) + "MB of "
// + df.format(Runtime.getRuntime().totalMemory() / (1024.0 * 1024.0)) + "MB free");
//
// debug("starting garbage collector ...");
// System.gc();
// debug("finished collecting garbage!");
//
// debug("Mem: "
// + df.format(Runtime.getRuntime().freeMemory() / (1024.0 * 1024.0)) + "MB of "
// + df.format(Runtime.getRuntime().totalMemory() / (1024.0 * 1024.0)) + "MB free");
// status.setText("Garbage collection finished: "
// + df.format(Runtime.getRuntime().freeMemory() / (1024.0 * 1024.0)) + "MB of "
// + df.format(Runtime.getRuntime().totalMemory() / (1024.0 * 1024.0)) + "MB free");
//
// }
private String getSingleValue(Element root, String xpath) {
String text = null;
java.util.List results = AnnotationToolkit.xpathQuery(root, xpath, null);
if (results.size() > 0) {
text = ((Element) results.get(0)).getTextTrim();
}
return text;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -