コード例 #1
0
 @Ignore
 @Test
 public void formats() {
   for (String formatName : ImageIO.getReaderFormatNames()) {
     System.out.println(formatName);
   }
 }
コード例 #2
0
ファイル: ImageIOTest.java プロジェクト: ljx407/test
  @Test
  public void test() {
    String[] readerFormatNames = ImageIO.getReaderFormatNames();
    System.out.println(Arrays.asList(readerFormatNames));
    // jpg bmp jpeg wbmp png

    String[] writerFormatNames = ImageIO.getWriterFormatNames();
    System.out.println(Arrays.asList(writerFormatNames));
  }
コード例 #3
0
  /**
   * Method to get the list of supported file extensions for reading
   *
   * @return List of supported file extensions for reading
   * @throws OpenStegoException
   */
  public List<String> getReadableFileExtensions() throws OpenStegoException {
    if (readFormats != null) {
      return readFormats;
    }

    String format = null;
    String[] formats = null;
    List<String> formatList = new ArrayList<String>();

    formats = ImageIO.getReaderFormatNames();
    for (int i = 0; i < formats.length; i++) {
      format = formats[i].toLowerCase();
      if (format.indexOf("jpeg") >= 0 && format.indexOf("2000") >= 0) {
        format = "jp2";
      }
      if (!formatList.contains(format)) {
        formatList.add(format);
      }
    }

    Collections.sort(formatList);
    readFormats = formatList;
    return readFormats;
  }
コード例 #4
0
ファイル: SaveAction.java プロジェクト: mporter-gre/TrackMate
  public void actionPerformed(ActionEvent e) {

    final mxGraphComponent graphComponent = trackScheme.getGUI().graphComponent;
    final mxGraph graph = trackScheme.getGraph();
    FileFilter selectedFilter = null;
    DefaultFileFilter xmlPngFilter = new DefaultFileFilter(".png", "PNG+XML file (.png)");
    FileFilter vmlFileFilter = new DefaultFileFilter(".html", "VML file (.html)");
    String filename = null;
    boolean dialogShown = false;

    String wd;

    if (lastDir != null) {
      wd = lastDir;
    } else {
      wd = System.getProperty("user.dir");
    }

    JFileChooser fc = new JFileChooser(wd);

    // Adds the default file format
    FileFilter defaultFilter = xmlPngFilter;
    fc.addChoosableFileFilter(defaultFilter);

    // Adds special vector graphics formats and HTML
    fc.addChoosableFileFilter(new DefaultFileFilter(".pdf", "PDF file (.pdf)"));
    fc.addChoosableFileFilter(new DefaultFileFilter(".svg", "SVG file (.svg)"));
    fc.addChoosableFileFilter(new DefaultFileFilter(".html", "HTML file (.html)"));
    fc.addChoosableFileFilter(vmlFileFilter);
    fc.addChoosableFileFilter(new DefaultFileFilter(".txt", "Graph Drawing file (.txt)"));
    fc.addChoosableFileFilter(new DefaultFileFilter(".mxe", "mxGraph Editor file (.mxe)"));

    // Adds a filter for each supported image format
    Object[] imageFormats = ImageIO.getReaderFormatNames();

    // Finds all distinct extensions
    HashSet<String> formats = new HashSet<String>();

    for (int i = 0; i < imageFormats.length; i++) {
      String ext = imageFormats[i].toString().toLowerCase();
      formats.add(ext);
    }

    imageFormats = formats.toArray();

    for (int i = 0; i < imageFormats.length; i++) {
      String ext = imageFormats[i].toString();
      fc.addChoosableFileFilter(
          new DefaultFileFilter("." + ext, ext.toUpperCase() + " File  (." + ext + ")"));
    }

    // Adds filter that accepts all supported image formats
    fc.addChoosableFileFilter(new DefaultFileFilter.ImageFileFilter("All Images"));
    fc.setFileFilter(defaultFilter);
    int rc = fc.showDialog(null, "Save");
    dialogShown = true;

    if (rc != JFileChooser.APPROVE_OPTION) {
      return;
    } else {
      lastDir = fc.getSelectedFile().getParent();
    }

    filename = fc.getSelectedFile().getAbsolutePath();
    selectedFilter = fc.getFileFilter();

    if (selectedFilter instanceof DefaultFileFilter) {
      String ext = ((DefaultFileFilter) selectedFilter).getExtension();

      if (!filename.toLowerCase().endsWith(ext)) {
        filename += ext;
      }
    }

    if (new File(filename).exists()
        && JOptionPane.showConfirmDialog(graphComponent, "Overwrite existing file?")
            != JOptionPane.YES_OPTION) {
      return;
    }

    try {
      String ext = filename.substring(filename.lastIndexOf('.') + 1);

      if (ext.equalsIgnoreCase("svg")) {
        mxSvgCanvas canvas =
            (mxSvgCanvas)
                mxCellRenderer.drawCells(
                    graph,
                    null,
                    1,
                    null,
                    new CanvasFactory() {
                      public mxICanvas createCanvas(int width, int height) {
                        TrackSchemeSvgCanvas canvas =
                            new TrackSchemeSvgCanvas(mxDomUtils.createSvgDocument(width, height));
                        canvas.setEmbedded(true);
                        return canvas;
                      }
                    });

        mxUtils.writeFile(mxXmlUtils.getXml(canvas.getDocument()), filename);

      } else if (selectedFilter == vmlFileFilter) {
        mxUtils.writeFile(
            mxXmlUtils.getXml(
                mxCellRenderer.createVmlDocument(graph, null, 1, null, null).getDocumentElement()),
            filename);

      } else if (ext.equalsIgnoreCase("html")) {
        mxUtils.writeFile(
            mxXmlUtils.getXml(
                mxCellRenderer.createHtmlDocument(graph, null, 1, null, null).getDocumentElement()),
            filename);

      } else if (ext.equalsIgnoreCase("mxe") || ext.equalsIgnoreCase("xml")) {
        mxCodec codec = new mxCodec();
        String xml = mxXmlUtils.getXml(codec.encode(graph.getModel()));
        mxUtils.writeFile(xml, filename);

      } else if (ext.equalsIgnoreCase("txt")) {
        String content = mxGdCodec.encode(graph); // .getDocumentString();
        mxUtils.writeFile(content, filename);

      } else if (ext.equalsIgnoreCase("pdf")) {
        exportGraphToPdf(graph, filename);

      } else {
        Color bg = null;

        if ((!ext.equalsIgnoreCase("gif") && !ext.equalsIgnoreCase("png"))
            || JOptionPane.showConfirmDialog(graphComponent, "Transparent Background?")
                != JOptionPane.YES_OPTION) {
          bg = graphComponent.getBackground();
        }

        if (selectedFilter == xmlPngFilter || (ext.equalsIgnoreCase("png") && !dialogShown)) {
          saveXmlPng(trackScheme.getGUI(), filename, bg);
        } else {
          BufferedImage image =
              mxCellRenderer.createBufferedImage(
                  graph,
                  null,
                  1,
                  bg,
                  graphComponent.isAntiAlias(),
                  null,
                  graphComponent.getCanvas());

          if (image != null) {
            ImageIO.write(image, ext, new File(filename));
          } else {
            JOptionPane.showMessageDialog(graphComponent, "No Image Data");
          }
        }
      }

    } catch (Throwable ex) {
      ex.printStackTrace();
      JOptionPane.showMessageDialog(
          graphComponent, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
    }
  }
コード例 #5
0
  private void savePlotAsImage() {
    // get the possible image name.
    String imageName = "ScreePlot.png";

    // Ask the user to specify a file name for saving the histo.
    String pathSep = File.separator;
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
    // fc.setCurrentDirectory(new File(workingDirectory + pathSep + imageName + ".png"));
    fc.setAcceptAllFileFilterUsed(false);

    // File f = new File(workingDirectory + pathSep + imageName + ".png");
    // fc.setSelectedFile(f);

    // set the filter.
    ArrayList<ExtensionFileFilter> filters = new ArrayList<ExtensionFileFilter>();
    String[] extensions = ImageIO.getReaderFormatNames(); // {"PNG", "JPEG", "JPG"};
    String filterDescription = "Image Files (" + extensions[0];
    for (int i = 1; i < extensions.length; i++) {
      filterDescription += ", " + extensions[i];
    }
    filterDescription += ")";
    ExtensionFileFilter eff = new ExtensionFileFilter(filterDescription, extensions);
    fc.setFileFilter(eff);

    int result = fc.showSaveDialog(this);
    File file = null;
    if (result == JFileChooser.APPROVE_OPTION) {
      file = fc.getSelectedFile();
      // see if file has an extension.
      if (file.toString().lastIndexOf(".") <= 0) {
        String fileName = file.toString() + ".png";
        file = new File(fileName);
      }

      String fileDirectory = file.getParentFile() + pathSep;
      //            if (!fileDirectory.equals(workingDirectory)) {
      //                workingDirectory = fileDirectory;
      //            }

      // see if the file exists already, and if so, should it be overwritten?
      if (file.exists()) {
        Object[] options = {"Yes", "No"};
        int n =
            JOptionPane.showOptionDialog(
                this,
                "The file already exists.\n" + "Would you like to overwrite it?",
                "Whitebox GAT Message",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null, // do not use a custom Icon
                options, // the titles of buttons
                options[0]); // default button title

        if (n == JOptionPane.YES_OPTION) {
          file.delete();
        } else if (n == JOptionPane.NO_OPTION) {
          return;
        }
      }
      if (!saveToImage(file.toString())) {
        //                showFeedback("An error occurred while saving the map to the image file.");
      }
    }
  }
コード例 #6
0
 @Override
 public String[] getFileExtensions() {
   return ImageIO.getReaderFormatNames();
 }