簡體   English   中英

在沒有 ImageIO.read() 的情況下從 JPG 轉換為 PNG 圖像

[英]Conversion from JPG to PNG image without ImageIO.read()

我試圖在 Java 中將 JPG 圖像轉換為 PNG 7. 假設圖像“損壞”,因為 ImageIO.read() 拋出異常

java.lang.IllegalArgumentException: Numbers of source Raster bands and source color space components do not match

因此,它不能以這種方式加載。 所以我需要在沒有 ImageIO.read() 的情況下加載/讀取圖像

我的代碼:

public byte[] convertJpgToPng(byte[] originalPhotoData) throws IOException {

    ByteArrayInputStream bais = new ByteArrayInputStream(originalPhotoData);
    
    BufferedImage bufferedImage = ImageIO.read(bais); //This line throws an exception, so I need to replace the ImageIO.read() method

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "png", baos);
    byte[] resultingBytes = baos.toByteArray();

    return resultingBytes;
}

可以更改讀數,還是我需要更改整個轉換方法?

您可以使用外部程序,如ImageMagick ,從 Java 調用它,如此所述。

在某些情況下,外部程序可能具有更好的功能,也許它實際上是 16 位 JPEG XT,誰知道呢。

暫無
暫無

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

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