2

I've a desktop java application that get images from the user clipboard ("paste" action), and I need to check their mime type before further processing (only jpg, png and gif are admitted).

I'm using this code to get the image from the clipboard:

  Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
  if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
    try {
      Image pastedImage = (Image) transferable.getTransferData(DataFlavor.imageFlavor);
      // how to get mime type?
    } catch (Exception e) {/*cutted for brevity*/}
  }

Now, I know how to get the mime type from a file using apache Tika and I've tried writing the image to disk in order to reuse this technique, but also the ImageIO.write method requires a format name:

BufferedImage bi = convertImageToBI(img);
File outputfile = File.createTempFile("img_", "_upload");
ImageIO.write(bi, "jpg", outputfile);

To convert an image to a BufferedImage I'm using this method. The only difference is that I'm using TYPE_INT_RGB instead of TYPE_INT_ARGB.

How can I get the pasted image's mime type?

Community
  • 1
  • 1
lifeisfoo
  • 15,478
  • 6
  • 74
  • 115
  • It's complicated. Like, super complicated. If you're doing real Swing programming, I'd recommend an O'Reilly book *Swing Hacks* which has information on all the different things you have to check to actually get a mime type from the clipboard. You also might be able to find the same info on the web, but any solution should have at least 20 to 30 lines of nothing but interrogating the clip. – markspace Feb 14 '17 at 16:06
  • 1
    `DataFlavor.imageFlavor` *always has the mime-type* `"image/x-java-image"`. This is a custom mime-type, and describes clipboard data represented by the `java.awt.Image` class. I.e. it's already decoded to pixels, and is not in a file format like JPEG, PNG or GIF. You can choose to store it in any format you like. Maybe you should instead have a look at [`DataFlavor.javaFileListFlavor`](https://docs.oracle.com/javase/7/docs/api/java/awt/datatransfer/DataFlavor.html#javaFileListFlavor)? – Harald K Feb 15 '17 at 14:35

1 Answers1

0

I tried a crazy approach to check whether it's JPG/GIF or PNG. When there's transparency, I use PNG, other than that I always use JPG, and this proven to work also with GIF, just the GIF lost it's animation tho

    protected BufferedImage img;

    protected void loadImage() {
        try {
            // Well we read the temp file first, I test it using local file
            File f = new File(loc);
            // Read the file as Image 
            this.img = ImageIO.read(f);
        } catch(Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
        this.revalidate();
        this.repaint();
    }

    public byte[] toBlob() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            // Check if this image contains transparency? If no then write as JPG
            if (this.img.getTransparency() == java.awt.Transparency.OPAQUE) {
                ImageIO.write((BufferedImage)this.img, "jpg", baos);
            } else { // If it does contain transparency then write as PNG
                ImageIO.write((BufferedImage)this.img, "png", baos);
            }
        } catch(Exception e) {
            JOptionPane.showMessageDialog(this, e.toString());
        }
        byte[] data = baos.toByteArray();
        return data;
    }
    
    public void saveImageToBlob(String location) {
        try {
            File f = new File(location);
            FileOutputStream o = new FileOutputStream(f);
            byte[] d = this.toBlob();
            o.write(d, 0, d.length);
            o.close();
        } catch(Exception e) {
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }
Benyamin Limanto
  • 775
  • 10
  • 24