Project

General

Profile

Download (3.96 KB) Statistics
| Branch: | Tag: | Revision:
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.mediaMetaData;
11

    
12
import java.io.File;
13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.net.URI;
16
import java.util.HashMap;
17

    
18
import org.apache.http.HttpException;
19
import org.apache.log4j.Logger;
20
import org.apache.sanselan.ImageInfo;
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
 * @date 27.11.2009
32
 *
33
 */
34
public  class ImageMetaData extends MediaMetaData {
35
	private static Logger logger = Logger.getLogger(ImageMetaData.class);
36
	
37
	protected ImageMetaData(){
38
		this.metaData = new HashMap<String, String>();
39
	}
40
	
41
	protected int width, height, bitPerPixel;
42
	
43
	public int getWidth() {
44
		return width;
45
	}
46

    
47
	public void setWidth(int width) {
48
		this.width = width;
49
	}
50

    
51
	public int getHeight() {
52
		return height;
53
	}
54

    
55
	public void setHeight(int height) {
56
		this.height = height;
57
	}
58

    
59
	public int getBitPerPixel() {
60
		return bitPerPixel;
61
	}
62

    
63
	public void setBitPerPixel(int bitPerPixel) {
64
		this.bitPerPixel = bitPerPixel;
65
	}
66

    
67
	public String getFormatName() {
68
		return formatName;
69
	}
70

    
71
	public void setFormatName(String formatName) {
72
		this.formatName = formatName;
73
	}
74

    
75
	public String getMimeType() {
76
		return mimeType;
77
	}
78

    
79
	public void setMimeType(String mimeType) {
80
		this.mimeType = mimeType;
81
	}
82

    
83
	@Override
84
	public String toString(){
85
        return formatName + " [" + mimeType+ "] w:" + width + " h:" + height + " depth:" + bitPerPixel;
86
	}
87
	
88
	
89
	private  void readImageInfo(ImageInfo imageInfo) {
90
		this.formatName = imageInfo.getFormatName();
91
		this.mimeType = imageInfo.getMimeType();
92
		this.width = imageInfo.getWidth();
93
		this.height = imageInfo.getHeight();
94
		this.bitPerPixel = imageInfo.getBitsPerPixel();
95
		
96
	}
97
	
98

    
99
	public void readImageInfo(URI imageUri, Integer timeOut) throws IOException, HttpException{
100
		
101
		File image = null;
102
		InputStream inputStream;
103
		try {
104
			
105
			inputStream = UriUtils.getInputStream(imageUri);
106

    
107
			ImageInfo imageInfo = Sanselan.getImageInfo(inputStream, null);
108
			
109
			
110
			readImageInfo(imageInfo);
111
		    
112
		} catch (IOException e) {
113
			logger.warn("Could not read: "+ imageUri.toString() + "; reason: "+e.getMessage());
114
			throw e;
115
		} catch (HttpException e) {
116
			logger.warn("Could not open url: "+ imageUri.toString() + "; reason: "+e.getMessage());
117
			throw e;
118
		} catch (ImageReadException e) {
119
			logger.error("Could not open url: " + imageUri + ". " + e.getMessage());
120
			throw new IOException(e);
121
		}
122
				
123
	}
124

    
125
	
126
	public void readMetaData(URI mediaUri, Integer timeOut) throws IOException, HttpException {
127
		readImageInfo(mediaUri, timeOut);
128
		try {
129
			InputStream inputStream = UriUtils.getInputStream(mediaUri);
130
			
131
			IImageMetadata mediaData = Sanselan.getMetadata(inputStream, null);
132
			
133
			if (mediaData != null){
134
				for (Object object : mediaData.getItems()){
135
					Item item = (Item) object;
136
					if (item.getKeyword().contains("/")){
137
						String key = item.getKeyword();
138
						//key.replace("/", "");
139
						int index = key.indexOf("/");
140
						key = key.substring(0, index);
141
						metaData.put(key, item.getText());
142
						
143
					}else{
144
						metaData.put(item.getKeyword(), item.getText());
145
						
146
					}
147
					
148
				}
149
			}
150
		} catch (ImageReadException e) {
151
			logger.warn(e.getLocalizedMessage());
152
			throw new IOException(e);
153
		} catch (IOException e) {
154
			logger.warn("The image server is not available!");
155
			throw e;
156
		}
157
		
158
	}
159

    
160
	public static ImageMetaData newInstance() {
161
		
162
		return new ImageMetaData();
163
	}
164

    
165
}
(2-2/7)