Is there a 100% Java alternative to ImageIO for reading JPEG files?

后端 未结 3 1403
再見小時候
再見小時候 2020-12-02 18:01

We are using Java2D to resize photos uploaded to our website, but we run into an issue (a seemingly old one, cf.: http://forums.sun.com/thread.jspa?threadID=5425569) - a few

3条回答
  •  再見小時候
    2020-12-02 18:08

    I faced the same issue. I was reluctant to use JAI as it is outdated but it looks like it's the shortest solution.

    This code converts an InputStream to a BufferedImage, using sun's ImageIO (fast) or in the few cases where this problem occur, using JAI:

    public static BufferedImage read(InputStream is) throws IOException {
        try {
            // We try it with ImageIO
            return ImageIO.read(ImageIO.createImageInputStream(is));
        } catch (CMMException ex) {
            // If we failed...
            // We reset the inputStream (start from the beginning)
            is.reset();
            // And use JAI
            return JAI.create("stream", SeekableStream.wrapInputStream(is, true)).getAsBufferedImage();
        }
    }
    

提交回复
热议问题