1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.common.media;
|
11
|
|
12
|
import java.io.IOException;
|
13
|
import java.io.InputStream;
|
14
|
import java.net.URI;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.Map;
|
17
|
|
18
|
import org.apache.commons.lang.StringUtils;
|
19
|
import org.apache.http.HttpException;
|
20
|
import org.apache.http.client.ClientProtocolException;
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.apache.sanselan.ImageReadException;
|
23
|
import org.apache.sanselan.Sanselan;
|
24
|
import org.apache.sanselan.common.IImageMetadata;
|
25
|
import org.apache.sanselan.common.ImageMetadata.Item;
|
26
|
|
27
|
import eu.etaxonomy.cdm.common.UriUtils;
|
28
|
|
29
|
|
30
|
/**
|
31
|
* @author k.luther
|
32
|
* @date 27.11.2009
|
33
|
*
|
34
|
*/
|
35
|
public class ImageInfo extends MediaInfo {
|
36
|
private static Logger logger = Logger.getLogger(ImageInfo.class);
|
37
|
|
38
|
private int width;
|
39
|
private int height;
|
40
|
private int bitPerPixel;
|
41
|
private URI imageUri;
|
42
|
|
43
|
private Map<String, String> metaData;
|
44
|
|
45
|
//********************** Factory Methods ******************************/
|
46
|
|
47
|
public static ImageInfo NewInstance(URI imageUri, Integer timeOut) throws IOException, HttpException {
|
48
|
ImageInfo instance = new ImageInfo(imageUri);
|
49
|
instance.readSuffix();
|
50
|
instance.readImageLength();
|
51
|
instance.readImageInfo(timeOut);
|
52
|
return instance;
|
53
|
}
|
54
|
|
55
|
public static ImageInfo NewInstanceWithMetaData(URI imageUri, Integer timeOut) throws IOException, HttpException {
|
56
|
ImageInfo instance = NewInstance(imageUri, timeOut);
|
57
|
instance.readMetaData(timeOut);
|
58
|
return instance;
|
59
|
}
|
60
|
|
61
|
//*********************** CONSTRUCTOR **************************************/
|
62
|
|
63
|
private ImageInfo(URI imageUri){
|
64
|
this.imageUri = imageUri;
|
65
|
}
|
66
|
|
67
|
//*************************** GETTER /SETTER *******************************/
|
68
|
|
69
|
public URI getUri() {
|
70
|
return imageUri;
|
71
|
}
|
72
|
|
73
|
public int getWidth() {
|
74
|
return width;
|
75
|
}
|
76
|
|
77
|
public int getHeight() {
|
78
|
return height;
|
79
|
}
|
80
|
|
81
|
public int getBitPerPixel() {
|
82
|
return bitPerPixel;
|
83
|
}
|
84
|
|
85
|
public Map<String, String> getMetaData(){
|
86
|
return metaData;
|
87
|
}
|
88
|
|
89
|
//**************************** METHODS *****************************/
|
90
|
|
91
|
private void readSuffix(){
|
92
|
String path = imageUri.getPath();
|
93
|
|
94
|
String suffix = path.substring(StringUtils.lastIndexOf(path, '.') + 1);
|
95
|
setSuffix(suffix);
|
96
|
}
|
97
|
|
98
|
private void readImageLength() throws ClientProtocolException, IOException, HttpException{
|
99
|
long length = UriUtils.getResourceLength(imageUri, null);
|
100
|
setLength(length);
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Reads the image infos (width, height, bitPerPixel, metadata, format, mime type)
|
105
|
* @param timeOut
|
106
|
* @throws IOException
|
107
|
* @throws HttpException
|
108
|
*/
|
109
|
private void readImageInfo(Integer timeOut) throws IOException, HttpException{
|
110
|
|
111
|
InputStream inputStream;
|
112
|
try {
|
113
|
inputStream = UriUtils.getInputStream(imageUri);
|
114
|
org.apache.sanselan.ImageInfo imageInfo = Sanselan.getImageInfo(inputStream, null);
|
115
|
|
116
|
setFormatName(imageInfo.getFormatName());
|
117
|
setMimeType(imageInfo.getMimeType());
|
118
|
width = imageInfo.getWidth();
|
119
|
height = imageInfo.getHeight();
|
120
|
bitPerPixel = imageInfo.getBitsPerPixel();
|
121
|
|
122
|
} catch (ImageReadException e) {
|
123
|
logger.error("Could not read: " + imageUri + ". " + e.getMessage());
|
124
|
throw new IOException(e);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
|
129
|
public Map<String, String> readMetaData(Integer timeOut) throws IOException, HttpException {
|
130
|
|
131
|
try {
|
132
|
InputStream inputStream = UriUtils.getInputStream(imageUri);
|
133
|
|
134
|
IImageMetadata mediaData;
|
135
|
mediaData = Sanselan.getMetadata(inputStream, null);
|
136
|
|
137
|
if (mediaData != null){
|
138
|
metaData = new HashMap<String, String>();
|
139
|
for (Object object : mediaData.getItems()){
|
140
|
Item item = (Item) object;
|
141
|
if (item.getKeyword().contains("/")){
|
142
|
String key = item.getKeyword();
|
143
|
//key.replace("/", "");
|
144
|
int index = key.indexOf("/");
|
145
|
key = key.substring(0, index);
|
146
|
metaData.put(key, item.getText());
|
147
|
}else{
|
148
|
metaData.put(item.getKeyword(), item.getText());
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
} catch (ImageReadException e) {
|
153
|
logger.error("Could not read: " + imageUri + ". " + e.getMessage());
|
154
|
throw new IOException(e);
|
155
|
}
|
156
|
return metaData;
|
157
|
}
|
158
|
|
159
|
// ******************* TO STRING **********************************/
|
160
|
|
161
|
@Override
|
162
|
public String toString(){
|
163
|
return getFormatName() + " [" + getMimeType()+ "] w:" + width + " h:" + height + " depth:" + bitPerPixel;
|
164
|
}
|
165
|
|
166
|
|
167
|
}
|