简体   繁体   English

Java 无法使用 javax.imageio 或 Sanselan 读取图像

[英]Java can't read an image with javax.imageio or Sanselan

I want to read an image to scale it using awt and Apache Commons Imaging, previously known as Apache Commons Sanselan I can not scale the image but I can see it in the browser properly as jpg without any problem.我想读取图像以使用 awt 和 Apache Commons Imaging(以前称为 Apache Commons Sanselan)缩放它我无法缩放图像,但我可以在浏览器中正确地看到它作为jpg没有任何问题。 Getting the image info using使用获取图像信息

Sanselan.getMetadata(fileData)

I get this info:我得到这个信息:

No Exif metadata.
Photoshop (IPTC) metadata:

The code编码

public static byte[] scale(byte[] fileData, int width, int height) {

        ByteArrayInputStream in = new ByteArrayInputStream(fileData);

        try {

            BufferedImage img = javax.imageio.ImageIO.read(in);

            ....

            return buffer.toByteArray();

        } catch (IOException e1) {

            System.out.println  ("e1 -> " + e1.getMessage());

            try {

                BufferedImage img = Sanselan.getBufferedImage(in);

            } catch (ImageReadException | IOException e2) {

                System.out.println  ("e2 -> " + e2.getMessage());
            }

        }

        return fileData;
    }

but I got this exceptions:但我得到了这个例外:

e1 -> Unsupported Image Type
e2 -> Can't parse this format.

Image scaling can be done without external libraries.无需外部库即可完成图像缩放。

Image img = ImageIO.read(URL);
Image scaledImg = img.getScaledInstance(IMG_WIDTH, IMG_HEIGHT, Image.SCALE_DEFAULT);

See the docs for further inspiration.请参阅 文档以获取更多灵感。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM