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