gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / viewexport / Export.java @ 3749
History | View | Annotate | Download (10.8 KB)
1 |
package org.gvsig.raster.tools.app.basic.tool.viewexport; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.awt.geom.AffineTransform; |
5 |
import java.awt.image.BufferedImage; |
6 |
import java.io.File; |
7 |
import java.io.IOException; |
8 |
import java.util.Hashtable; |
9 |
import java.util.Iterator; |
10 |
|
11 |
import javax.swing.JFileChooser; |
12 |
import javax.swing.JOptionPane; |
13 |
import javax.swing.filechooser.FileFilter; |
14 |
|
15 |
import org.gvsig.andami.PluginServices; |
16 |
import org.gvsig.andami.plugins.Extension; |
17 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
18 |
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
19 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
20 |
import org.gvsig.fmap.dal.coverage.RasterManager; |
21 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
22 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
23 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
24 |
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
25 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
26 |
import org.gvsig.fmap.dal.coverage.store.DataServerWriter; |
27 |
import org.gvsig.fmap.dal.coverage.store.RasterWriter; |
28 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
import com.sun.jimi.core.Jimi; |
33 |
import com.sun.jimi.core.JimiException; |
34 |
|
35 |
|
36 |
/**
|
37 |
* Plugin to export the view to image
|
38 |
*/
|
39 |
public class Export extends Extension { |
40 |
private Logger log = LoggerFactory.getLogger(Export.class); |
41 |
private String lastPath = null; |
42 |
private Hashtable<String, MyFileFilter> cmsExtensionsSupported = null; |
43 |
private Hashtable<String, MyFileFilter> jimiExtensionsSupported = null; |
44 |
|
45 |
public boolean isEnabled() { |
46 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
47 |
|
48 |
if (f == null || !(f instanceof AbstractViewPanel)) { |
49 |
return false; |
50 |
} |
51 |
|
52 |
AbstractViewPanel avp = (AbstractViewPanel)f; |
53 |
FLayers layers = avp.getMapControl().getMapContext().getLayers(); |
54 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
55 |
if(layers.getLayer(i) != null) |
56 |
return layers.getLayer(i).isAvailable();
|
57 |
} |
58 |
return false; |
59 |
} |
60 |
|
61 |
public boolean isVisible() { |
62 |
IWindow f = PluginServices.getMDIManager().getActiveWindow(); |
63 |
if (f == null) { |
64 |
return false; |
65 |
} |
66 |
|
67 |
return (f instanceof AbstractViewPanel); |
68 |
} |
69 |
|
70 |
public void initialize() { |
71 |
} |
72 |
|
73 |
public void postInitialize() { |
74 |
cmsExtensionsSupported = new Hashtable<String, MyFileFilter>(); |
75 |
jimiExtensionsSupported = new Hashtable<String, MyFileFilter>(); |
76 |
cmsExtensionsSupported.put("jpg", new MyFileFilter("jpg", |
77 |
PluginServices.getText(this, "jpeg_file"), "cms")); |
78 |
jimiExtensionsSupported.put("png",new MyFileFilter("png", |
79 |
PluginServices.getText(this, "png_file"), "cms")); |
80 |
jimiExtensionsSupported.put("bmp",new MyFileFilter("bmp", |
81 |
PluginServices.getText(this, "bmp_file"), "cms")); |
82 |
cmsExtensionsSupported.put("tif",new MyFileFilter("tif", |
83 |
PluginServices.getText(this, "tif_file"), "cms")); |
84 |
} |
85 |
|
86 |
public static boolean saveImageCMS(File fileDst,BufferedImage srcImage) throws RasterDriverException, IOException { |
87 |
RasterManager manager = (RasterManager)RasterLocator.getManager(); |
88 |
DataServerWriter data = new RasterizerImage(srcImage);
|
89 |
RasterWriter writer = null;
|
90 |
try {
|
91 |
/*DataServerExplorerParameters eparams = DALLocator.getDataManager().createServerExplorerParameters("FilesystemExplorer");
|
92 |
eparams.setDynValue("initialpath", fileDst.getAbsolutePath());
|
93 |
DataServerExplorer serverExplorer = DALLocator.getDataManager().openServerExplorer(eparams.getExplorerName(), eparams);
|
94 |
NewRasterStoreParameters sparams = (NewRasterStoreParameters)serverExplorer.getAddParameters("Gdal Store");
|
95 |
sparams.setDataServer((DataServerWriter)processIncrement);
|
96 |
sparams.setDestination(path, file);
|
97 |
sparams.setBuffer(buffer);
|
98 |
sparams.setColorInterpretation(new String[]{ColorInterpretation.GRAY_BAND});
|
99 |
sparams.setWktProjection(dstoreCopy.getWktProjection());
|
100 |
sparams.setBand(i);
|
101 |
sparams.setAffineTransform(affineTransform);
|
102 |
sparams.setDriverParams(params);
|
103 |
serverExplorer.add(provider, sparams, true);*/
|
104 |
|
105 |
Params params = manager.createWriter(fileDst.getAbsolutePath()).getParams(); |
106 |
params.changeParamValue("blocksize", "7"); //posici?n 7 del array -> 512 |
107 |
params.changeParamValue("tfw", "false"); |
108 |
params.changeParamValue("interleave", new Integer(1));//posici?n 1 del array -> PIXEL |
109 |
writer = manager.createWriter(data, |
110 |
fileDst.getAbsolutePath(), |
111 |
3,
|
112 |
new AffineTransform(1, 0, 0, -1, 0, 0), |
113 |
srcImage.getWidth(), |
114 |
srcImage.getHeight(), |
115 |
Buffer.TYPE_IMAGE,
|
116 |
params, |
117 |
null);
|
118 |
if (writer == null) { |
119 |
PluginServices.getLogger().error("No supported Format: " + fileDst.getAbsolutePath());
|
120 |
return false; |
121 |
} |
122 |
} catch (NotSupportedExtensionException e) {
|
123 |
throw new RasterDriverException(""); |
124 |
} |
125 |
try {
|
126 |
writer.dataWrite(); |
127 |
} catch (ProcessInterruptedException e) {
|
128 |
} |
129 |
writer.writeClose(); |
130 |
return true; |
131 |
} |
132 |
|
133 |
|
134 |
|
135 |
public static boolean saveImageJimi(File fileDst,BufferedImage srcImage) throws Exception{ |
136 |
try {
|
137 |
|
138 |
Jimi.putImage(srcImage, fileDst.getAbsolutePath()); |
139 |
|
140 |
} catch (JimiException e) {
|
141 |
throw new Exception(fileDst.getAbsolutePath(),e); |
142 |
} |
143 |
return true; |
144 |
|
145 |
} |
146 |
|
147 |
/**
|
148 |
* @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
|
149 |
*/
|
150 |
public void execute(String actionCommand) { |
151 |
JFileChooser jfc = new JFileChooser(lastPath); |
152 |
|
153 |
jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter()); |
154 |
|
155 |
Iterator<MyFileFilter> iter = cmsExtensionsSupported.values().iterator();
|
156 |
while (iter.hasNext()){
|
157 |
jfc.addChoosableFileFilter((FileFilter)iter.next());
|
158 |
} |
159 |
|
160 |
iter = jimiExtensionsSupported.values().iterator(); |
161 |
while (iter.hasNext()){
|
162 |
jfc.addChoosableFileFilter((FileFilter)iter.next());
|
163 |
} |
164 |
|
165 |
jfc.setFileFilter((FileFilter)jimiExtensionsSupported.get("png")); |
166 |
if (jfc.showSaveDialog(
|
167 |
(Component) PluginServices.getMainFrame()
|
168 |
) == JFileChooser.APPROVE_OPTION) {
|
169 |
|
170 |
BufferedImage tempImage;
|
171 |
|
172 |
tempImage = ((AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow()).getImage(); |
173 |
|
174 |
File f = jfc.getSelectedFile();
|
175 |
|
176 |
lastPath = f.getParent(); |
177 |
|
178 |
MyFileFilter filter = (MyFileFilter)jfc.getFileFilter(); |
179 |
f = filter.normalizeExtension(f); |
180 |
|
181 |
|
182 |
if (f.exists()){
|
183 |
int resp = JOptionPane.showConfirmDialog( |
184 |
(Component) PluginServices.getMainFrame(),
|
185 |
PluginServices.getText(this,
|
186 |
"fichero_ya_existe_seguro_desea_guardarlo")+
|
187 |
"\n"+
|
188 |
f.getAbsolutePath(), |
189 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
190 |
if (resp != JOptionPane.YES_OPTION) { |
191 |
return;
|
192 |
} |
193 |
|
194 |
} |
195 |
|
196 |
if (filter.getInfo().equalsIgnoreCase("cms")){ |
197 |
|
198 |
try {
|
199 |
saveImageCMS(f, tempImage); |
200 |
} catch (IOException e) { |
201 |
log.debug("Error exportando la imagen", e);
|
202 |
} catch (RasterDriverException e) {
|
203 |
log.debug("Error exportando la imagen", e);
|
204 |
} |
205 |
}else if (filter.getInfo().equalsIgnoreCase("jimi")) { |
206 |
try {
|
207 |
saveImageJimi(f, tempImage); |
208 |
} catch (Exception e) { |
209 |
log.debug("Error exportando la imagen", e);
|
210 |
} |
211 |
|
212 |
} |
213 |
|
214 |
|
215 |
} |
216 |
} |
217 |
} |
218 |
|
219 |
/**
|
220 |
* Servidor de datos desde un BufferedImage. Cada petici?n es de un tama?o
|
221 |
* de bloque indicado por el escritor en los par?metros de la llamada por lo
|
222 |
* que en la siguiente petici?n habr? que escribir el bloque siguiente.
|
223 |
*/
|
224 |
class RasterizerImage implements DataServerWriter { |
225 |
private BufferedImage source = null; |
226 |
private int[] data = null; |
227 |
private int y = 0; |
228 |
|
229 |
public RasterizerImage(BufferedImage source) { |
230 |
this.source = source;
|
231 |
} |
232 |
|
233 |
/**
|
234 |
* Solo necesita implementar el m?todo RGB porque solo se
|
235 |
* utiliza para exportar la vista.
|
236 |
*/
|
237 |
public int[] readARGBData(int sX, int sY, int nBand){ |
238 |
return readData( sX, sY, nBand);
|
239 |
} |
240 |
|
241 |
public int[] readData(int sizeX, int sizeY, int nBand) { |
242 |
if(nBand == 0) { //Con nBand==0 se devuelven las 3 bandas |
243 |
this.data = this.source.getRGB(0, y, sizeX, sizeY, this.data, 0, sizeX); |
244 |
y += sizeY; |
245 |
return this.data; |
246 |
} |
247 |
return null; |
248 |
} |
249 |
|
250 |
public byte[][] readByteData(int sizeX, int sizeY) { |
251 |
return null; |
252 |
} |
253 |
|
254 |
public double[][] readDoubleData(int sizeX, int sizeY) { |
255 |
return null; |
256 |
} |
257 |
|
258 |
public float[][] readFloatData(int sizeX, int sizeY) { |
259 |
return null; |
260 |
} |
261 |
|
262 |
public int[][] readIntData(int sizeX, int sizeY) { |
263 |
return null; |
264 |
} |
265 |
|
266 |
public short[][] readShortData(int sizeX, int sizeY) { |
267 |
return null; |
268 |
} |
269 |
|
270 |
public int getPercent() { |
271 |
return 0; |
272 |
} |
273 |
|
274 |
public void setAlphaBuffer(Buffer alphaBuffer) { |
275 |
|
276 |
} |
277 |
|
278 |
public void setBuffer(Buffer buffer, int nband) { |
279 |
|
280 |
} |
281 |
|
282 |
public Buffer getBuffer() { |
283 |
return null; |
284 |
} |
285 |
|
286 |
public void setBand(int nband) { |
287 |
|
288 |
} |
289 |
|
290 |
public void dispose() { |
291 |
// TODO Auto-generated method stub
|
292 |
|
293 |
} |
294 |
|
295 |
public Buffer getSource() { |
296 |
return null; |
297 |
} |
298 |
|
299 |
public void setPercent(int value) { |
300 |
// TODO Auto-generated method stub
|
301 |
|
302 |
} |
303 |
} |
304 |
|
305 |
class MyFileFilter extends FileFilter{ |
306 |
|
307 |
private String[] extensiones = new String[1]; |
308 |
private String description = null; |
309 |
private boolean dirs = true; |
310 |
private String info = null; |
311 |
|
312 |
public MyFileFilter(String[] ext, String desc) { |
313 |
extensiones = ext; |
314 |
description = desc; |
315 |
} |
316 |
|
317 |
public MyFileFilter(String[] ext, String desc,String info) { |
318 |
extensiones = ext; |
319 |
description = desc; |
320 |
this.info = info;
|
321 |
} |
322 |
|
323 |
public MyFileFilter(String ext, String desc) { |
324 |
extensiones[0] = ext;
|
325 |
description = desc; |
326 |
} |
327 |
|
328 |
public MyFileFilter(String ext, String desc,String info) { |
329 |
extensiones[0] = ext;
|
330 |
description = desc; |
331 |
this.info = info;
|
332 |
} |
333 |
|
334 |
public MyFileFilter(String ext, String desc, boolean dirs) { |
335 |
extensiones[0] = ext;
|
336 |
description = desc; |
337 |
this.dirs = dirs;
|
338 |
} |
339 |
|
340 |
public MyFileFilter(String ext, String desc, boolean dirs,String info) { |
341 |
extensiones[0] = ext;
|
342 |
description = desc; |
343 |
this.dirs = dirs;
|
344 |
this.info = info;
|
345 |
} |
346 |
|
347 |
public boolean accept(File f) { |
348 |
if (f.isDirectory()) {
|
349 |
if (dirs) {
|
350 |
return true; |
351 |
} else {
|
352 |
return false; |
353 |
} |
354 |
} |
355 |
for (int i = 0; i < extensiones.length; i++) { |
356 |
if (extensiones[i].equals("")) { |
357 |
continue;
|
358 |
} |
359 |
if (getExtensionOfAFile(f).equalsIgnoreCase(extensiones[i])) {
|
360 |
return true; |
361 |
} |
362 |
} |
363 |
|
364 |
return false; |
365 |
} |
366 |
|
367 |
/**
|
368 |
* @see javax.swing.filechooser.FileFilter#getDescription()
|
369 |
*/
|
370 |
public String getDescription() { |
371 |
return description;
|
372 |
} |
373 |
|
374 |
public String[] getExtensions() { |
375 |
return extensiones;
|
376 |
} |
377 |
|
378 |
public boolean isDirectory(){ |
379 |
return dirs;
|
380 |
} |
381 |
|
382 |
private String getExtensionOfAFile(File file){ |
383 |
String name;
|
384 |
int dotPos;
|
385 |
name = file.getName(); |
386 |
dotPos = name.lastIndexOf(".");
|
387 |
if (dotPos < 1){ |
388 |
return ""; |
389 |
} |
390 |
return name.substring(dotPos+1); |
391 |
} |
392 |
|
393 |
public File normalizeExtension(File file){ |
394 |
String ext = getExtensionOfAFile(file);
|
395 |
if (ext.equals("") || !(this.accept(file))){ |
396 |
return new File(file.getAbsolutePath() + "." + extensiones[0]); |
397 |
} |
398 |
return file;
|
399 |
} |
400 |
|
401 |
public String getInfo(){ |
402 |
return this.info; |
403 |
} |
404 |
|
405 |
public void setInfo(String info){ |
406 |
this.info = info;
|
407 |
} |
408 |
|
409 |
} |