1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.common.media;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.InputStream;
|
13
|
import java.net.URI;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.Map;
|
16
|
|
17
|
import org.apache.commons.imaging.ImageInfo;
|
18
|
import org.apache.commons.imaging.ImageReadException;
|
19
|
import org.apache.commons.imaging.Imaging;
|
20
|
import org.apache.commons.imaging.common.GenericImageMetadata.GenericImageMetadataItem;
|
21
|
import org.apache.commons.imaging.common.ImageMetadata;
|
22
|
import org.apache.commons.imaging.common.ImageMetadata.ImageMetadataItem;
|
23
|
import org.apache.commons.lang.StringUtils;
|
24
|
import org.apache.http.HttpException;
|
25
|
import org.apache.http.client.ClientProtocolException;
|
26
|
import org.apache.log4j.Logger;
|
27
|
|
28
|
import eu.etaxonomy.cdm.common.UriUtils;
|
29
|
|
30
|
/**
|
31
|
* @author k.luther
|
32
|
* @author a.mueller
|
33
|
* @since 27.11.2009
|
34
|
*/
|
35
|
public class CdmImageInfo extends MediaInfo {
|
36
|
private static Logger logger = Logger.getLogger(CdmImageInfo.class);
|
37
|
|
38
|
private int width;
|
39
|
private int height;
|
40
|
private int bitPerPixel;
|
41
|
private final URI imageUri;
|
42
|
|
43
|
private Map<String, String> metaData;
|
44
|
|
45
|
//********************** Factory Methods ******************************/
|
46
|
|
47
|
public static CdmImageInfo NewInstance(URI imageUri, Integer timeOut) throws IOException, HttpException {
|
48
|
CdmImageInfo instance = new CdmImageInfo(imageUri);
|
49
|
instance.readSuffix();
|
50
|
instance.readImageLength();
|
51
|
instance.readImageInfo(timeOut);
|
52
|
return instance;
|
53
|
}
|
54
|
|
55
|
public static CdmImageInfo NewInstanceWithMetaData(URI imageUri, Integer timeOut) throws IOException, HttpException {
|
56
|
CdmImageInfo instance = NewInstance(imageUri, timeOut);
|
57
|
|
58
|
instance.readMetaData(timeOut);
|
59
|
|
60
|
return instance;
|
61
|
}
|
62
|
|
63
|
//*********************** CONSTRUCTOR **************************************/
|
64
|
|
65
|
private CdmImageInfo(URI imageUri){
|
66
|
this.imageUri = imageUri;
|
67
|
}
|
68
|
|
69
|
//*************************** GETTER /SETTER *******************************/
|
70
|
|
71
|
public URI getUri() {
|
72
|
return imageUri;
|
73
|
}
|
74
|
|
75
|
public int getWidth() {
|
76
|
return width;
|
77
|
}
|
78
|
|
79
|
public int getHeight() {
|
80
|
return height;
|
81
|
}
|
82
|
|
83
|
public int getBitPerPixel() {
|
84
|
return bitPerPixel;
|
85
|
}
|
86
|
|
87
|
public Map<String, String> getMetaData(){
|
88
|
return metaData;
|
89
|
}
|
90
|
|
91
|
//**************************** METHODS *****************************/
|
92
|
|
93
|
private void readSuffix(){
|
94
|
String path = imageUri.getPath();
|
95
|
|
96
|
String suffix = path.substring(StringUtils.lastIndexOf(path, '.') + 1);
|
97
|
setSuffix(suffix);
|
98
|
}
|
99
|
|
100
|
private void readImageLength() throws ClientProtocolException, IOException, HttpException{
|
101
|
try {
|
102
|
long length = UriUtils.getResourceLength(imageUri, null);
|
103
|
setLength(length);
|
104
|
} catch (HttpException e) {
|
105
|
if (e.getMessage().equals("Could not retrieve Content-Length")){
|
106
|
InputStream inputStream = UriUtils.getInputStream(imageUri);
|
107
|
int n = 0;
|
108
|
while(inputStream.read() != -1){
|
109
|
n++;
|
110
|
}
|
111
|
inputStream.close();
|
112
|
logger.info("Content-Length not available in http header. Image size computed via input stream size: " + imageUri);
|
113
|
setLength(n);
|
114
|
}else{
|
115
|
throw e;
|
116
|
}
|
117
|
}
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Reads the image infos (width, height, bitPerPixel, metadata, format, mime type)
|
122
|
* @param timeOut
|
123
|
* @throws IOException
|
124
|
* @throws HttpException
|
125
|
*/
|
126
|
private void readImageInfo(Integer timeOut) throws IOException, HttpException{
|
127
|
|
128
|
InputStream inputStream;
|
129
|
try {
|
130
|
inputStream = UriUtils.getInputStream(imageUri);
|
131
|
ImageInfo imageInfo = Imaging.getImageInfo(inputStream, null);
|
132
|
|
133
|
setFormatName(imageInfo.getFormatName());
|
134
|
setMimeType(imageInfo.getMimeType());
|
135
|
width = imageInfo.getWidth();
|
136
|
height = imageInfo.getHeight();
|
137
|
bitPerPixel = imageInfo.getBitsPerPixel();
|
138
|
inputStream.close();
|
139
|
|
140
|
} catch (ImageReadException e) {
|
141
|
logger.error("Could not read: " + imageUri + ". " + e.getMessage());
|
142
|
throw new IOException(e);
|
143
|
}
|
144
|
}
|
145
|
|
146
|
|
147
|
/**
|
148
|
* @param timeOut TODO is not yet used
|
149
|
* @return
|
150
|
* @throws IOException
|
151
|
* @throws HttpException
|
152
|
*/
|
153
|
public Map<String, String> readMetaData(Integer timeOut) throws IOException, HttpException {
|
154
|
|
155
|
try {
|
156
|
InputStream inputStream = UriUtils.getInputStream(imageUri);
|
157
|
|
158
|
ImageMetadata mediaData = Imaging.getMetadata(inputStream, null);
|
159
|
|
160
|
if (mediaData != null){
|
161
|
metaData = new HashMap<>();
|
162
|
for (ImageMetadataItem item2 : mediaData.getItems()){
|
163
|
if (item2 instanceof GenericImageMetadataItem){
|
164
|
GenericImageMetadataItem item = (GenericImageMetadataItem)item2;
|
165
|
if (item.getKeyword().contains("/")){
|
166
|
String key = item.getKeyword();
|
167
|
//key.replace("/", "");
|
168
|
int index = key.indexOf("/");
|
169
|
key = key.substring(0, index);
|
170
|
metaData.put(key, text(item));
|
171
|
}else{
|
172
|
metaData.put(item.getKeyword(), text(item));
|
173
|
}
|
174
|
}
|
175
|
}
|
176
|
}
|
177
|
}
|
178
|
catch (ImageReadException e) {
|
179
|
logger.error("Could not read: " + imageUri + ". " + e.getMessage());
|
180
|
//throw new IOException(e);
|
181
|
}
|
182
|
return metaData;
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* Wrapper for the Item.getText() method which applies cleaning of the text representation.
|
187
|
*
|
188
|
* <ol>
|
189
|
* <li>Strings are surrounded by single quotes, these must be removed</li>
|
190
|
* </ol>
|
191
|
* @param item
|
192
|
*/
|
193
|
private String text(GenericImageMetadataItem item) {
|
194
|
String text = item.getText();
|
195
|
if(text.startsWith("'") && text.endsWith("'")) {
|
196
|
text = text.substring(1 , text.length() - 1);
|
197
|
}
|
198
|
return text;
|
199
|
}
|
200
|
|
201
|
// ******************* TO STRING **********************************/
|
202
|
|
203
|
@Override
|
204
|
public String toString(){
|
205
|
return getFormatName() + " [" + getMimeType()+ "] w:" + width + " h:" + height + " depth:" + bitPerPixel;
|
206
|
}
|
207
|
}
|