Project

General

Profile

Download (5 KB) Statistics
| Branch: | Tag: | Revision:
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.lang.StringUtils;
18
import org.apache.http.HttpException;
19
import org.apache.http.client.ClientProtocolException;
20
import org.apache.log4j.Logger;
21
import org.apache.sanselan.ImageReadException;
22
import org.apache.sanselan.Sanselan;
23
import org.apache.sanselan.common.IImageMetadata;
24
import org.apache.sanselan.common.ImageMetadata.Item;
25

    
26
import eu.etaxonomy.cdm.common.UriUtils;
27

    
28

    
29
/**
30
 * @author k.luther
31
 \* @since 27.11.2009
32
 */
33
public  class ImageInfo extends MediaInfo {
34
	private static Logger logger = Logger.getLogger(ImageInfo.class);
35

    
36
	private int width;
37
	private int height;
38
	private int bitPerPixel;
39
	private final URI imageUri;
40

    
41
	private Map<String, String> metaData;
42

    
43
//********************** Factory Methods ******************************/
44

    
45
	public static ImageInfo NewInstance(URI imageUri, Integer timeOut) throws IOException, HttpException {
46
		ImageInfo instance = new ImageInfo(imageUri);
47
		instance.readSuffix();
48
		instance.readImageLength();
49
		instance.readImageInfo(timeOut);
50
		return instance;
51
	}
52

    
53
	public static ImageInfo NewInstanceWithMetaData(URI imageUri, Integer timeOut) throws IOException, HttpException {
54
		ImageInfo instance = NewInstance(imageUri, timeOut);
55
		instance.readMetaData(timeOut);
56
		return instance;
57
	}
58

    
59
//*********************** CONSTRUCTOR **************************************/
60

    
61
	private ImageInfo(URI imageUri){
62
		this.imageUri = imageUri;
63
	}
64

    
65
//*************************** GETTER /SETTER *******************************/
66

    
67
	public URI getUri() {
68
		return imageUri;
69
	}
70

    
71
	public int getWidth() {
72
		return width;
73
	}
74

    
75
	public int getHeight() {
76
		return height;
77
	}
78

    
79
	public int getBitPerPixel() {
80
		return bitPerPixel;
81
	}
82

    
83
	public Map<String, String> getMetaData(){
84
		return metaData;
85
	}
86

    
87
//**************************** METHODS *****************************/
88

    
89
	private void readSuffix(){
90
		String path = imageUri.getPath();
91

    
92
		String suffix = path.substring(StringUtils.lastIndexOf(path, '.') + 1);
93
		setSuffix(suffix);
94
	}
95

    
96
	private void readImageLength() throws ClientProtocolException, IOException, HttpException{
97
		long length = UriUtils.getResourceLength(imageUri, null);
98
		setLength(length);
99
	}
100

    
101
	/**
102
	 * Reads the image infos (width, height, bitPerPixel, metadata, format, mime type)
103
	 * @param timeOut
104
	 * @throws IOException
105
	 * @throws HttpException
106
	 */
107
	private void readImageInfo(Integer timeOut) throws IOException, HttpException{
108

    
109
		InputStream inputStream;
110
		try {
111
			inputStream = UriUtils.getInputStream(imageUri);
112
			org.apache.sanselan.ImageInfo imageInfo = Sanselan.getImageInfo(inputStream, null);
113

    
114
			setFormatName(imageInfo.getFormatName());
115
			setMimeType(imageInfo.getMimeType());
116
			width = imageInfo.getWidth();
117
			height = imageInfo.getHeight();
118
			bitPerPixel = imageInfo.getBitsPerPixel();
119
			inputStream.close();
120

    
121
		} catch (ImageReadException e) {
122
			logger.error("Could not read: " + imageUri + ". " + e.getMessage());
123
			throw new IOException(e);
124
		}
125
	}
126

    
127

    
128
	public Map<String, String> readMetaData(Integer timeOut) throws IOException, HttpException {
129

    
130
		try {
131
			InputStream inputStream = UriUtils.getInputStream(imageUri);
132

    
133
			 IImageMetadata mediaData = Sanselan.getMetadata(inputStream, null);
134

    
135
			if (mediaData != null){
136
				metaData = new HashMap<>();
137
				for (Object object : mediaData.getItems()){
138
					Item item = (Item) object;
139
					if (item.getKeyword().contains("/")){
140
						String key = item.getKeyword();
141
						//key.replace("/", "");
142
						int index = key.indexOf("/");
143
						key = key.substring(0, index);
144
						metaData.put(key, text(item));
145
					}else{
146
						metaData.put(item.getKeyword(), text(item));
147
					}
148
				}
149
			}
150
		} catch (ImageReadException e) {
151
			logger.error("Could not read: " + imageUri + ". " + e.getMessage());
152
			throw new IOException(e);
153
		}
154
		return metaData;
155
	}
156

    
157
    /**
158
     * Wrapper for the Item.getText() method which applies cleaning of the text representation.
159
     *
160
     * <ol>
161
     * <li>Strings are surrounded by single quotes, these must be removed</li>
162
     * </ol>
163
     * @param item
164
     * @return
165
     */
166
    private String text(Item item) {
167
        String  text = item.getText();
168
        if(text.startsWith("'") && text.endsWith("'")) {
169
            text = text.substring(1 , text.length() - 1);
170
        }
171
        return text;
172
    }
173

    
174
// ******************* TO STRING **********************************/
175

    
176
	@Override
177
	public String toString(){
178
        return getFormatName() + " [" + getMimeType()+ "] w:" + width + " h:" + height + " depth:" + bitPerPixel;
179
	}
180

    
181

    
182
}
(2-2/4)