簡體   English   中英

javax.imageio.ImageIO.read(new ByteArrayInputStream(data)) 在讀取字節數組圖像數據時拋出實例化異常

[英]javax.imageio.ImageIO.read(new ByteArrayInputStream(data)) throws instantiation exception while reading byte array image data

public static void createImage2() {
        try {
            BufferedImage bImage = ImageIO.read(new File("input.jpg"));
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(bImage, "jpg", bos );
            byte [] data = bos.toByteArray();
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            BufferedImage bImage2 = ImageIO.read(bis);
            ImageIO.write(bImage2, "jpg", new File("output.jpg") );
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("image created");
    }

ImageIO.read(bis)方法拋出此異常:

Exception in thread "main" java.util.ServiceConfigurationError: javax.imageio.spi.ImageReaderSpi: Provider com.aware.j2k.imageio.J2KImageReaderSpi could not be instantiated

如果您的應用程序拋出帶有 SPI 消息的異常 - java.util.ServiceLoader 找不到庫,對於添加庫,您必須選擇並添加依賴項 fe group: com.twelvemonkeys.imageio或其他實現

對於將圖像讀取到BufferedImage我建議使用

ImageIO.read(new MemoryCacheImageInputStream(new ByteArrayInputStream(file.getBytes())))

如果您需要創建新圖像,我建議使用

ImageTypeSpecifier.createFromRenderedImage(source).createBufferedImage(width, height)

注意力
許多處理圖像的功能默認使用磁盤存儲作為緩存或 tmp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM