1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
---|
2 | package cadastre_fr;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.image.BufferedImage;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.IOException;
|
---|
11 |
|
---|
12 | import javax.imageio.ImageIO;
|
---|
13 | import javax.swing.JFileChooser;
|
---|
14 | import javax.swing.filechooser.FileFilter;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
18 |
|
---|
19 | public class MenuActionSaveRasterAs extends JosmAction {
|
---|
20 |
|
---|
21 | public static String name = marktr("Save image as PNG");
|
---|
22 |
|
---|
23 | private static final long serialVersionUID = 1L;
|
---|
24 |
|
---|
25 | private WMSLayer wmsLayer;
|
---|
26 |
|
---|
27 | public class FiltrePng extends FileFilter {
|
---|
28 | @Override
|
---|
29 | public boolean accept(File file) {
|
---|
30 | if (file.isDirectory()) {
|
---|
31 | return true;
|
---|
32 | }
|
---|
33 | return file.getName().toLowerCase().endsWith(".png");
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public String getDescription() {
|
---|
38 | return tr("PNG files (*.png)");
|
---|
39 | }
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 | FiltrePng filtrePng = new FiltrePng();
|
---|
44 |
|
---|
45 | public MenuActionSaveRasterAs(WMSLayer wmsLayer) {
|
---|
46 | super(tr(name), "save", tr("Export as PNG format (only raster images)"), null, false);
|
---|
47 | this.wmsLayer = wmsLayer;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void actionPerformed(ActionEvent arg0) {
|
---|
51 | File file;
|
---|
52 | JFileChooser fc = new JFileChooser();
|
---|
53 | fc.setFileFilter(filtrePng);
|
---|
54 | int returnVal = fc.showSaveDialog(Main.parent);
|
---|
55 | if (returnVal == JFileChooser.APPROVE_OPTION) {
|
---|
56 | file = fc.getSelectedFile();
|
---|
57 | if (!file.getName().endsWith(".png"))
|
---|
58 | file = new File(file.getParent(), file.getName()+".png");
|
---|
59 | BufferedImage bi = wmsLayer.getImage(0).image;
|
---|
60 | try {
|
---|
61 | ImageIO.write(bi, "png", file);
|
---|
62 | /*
|
---|
63 | FileOutputStream flux = new FileOutputStream(file);
|
---|
64 | BufferedOutputStream fluxBuf = new BufferedOutputStream(flux);
|
---|
65 | JPEGImageEncoder codec = JPEGCodec.createJPEGEncoder(fluxBuf, JPEGCodec.getDefaultJPEGEncodeParam(bi));
|
---|
66 | codec.encode(bi);
|
---|
67 | fluxBuf.close();
|
---|
68 | */
|
---|
69 | } catch (IOException e) {
|
---|
70 | e.printStackTrace();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|