使用Java进行图像转码(JPEG到PNG)

时间:2022-04-03 17:20:19

In my Java application I would like to download a JPEG, transfer it to a PNG and do something with the resulting bytes.

在我的Java应用程序中,我想下载一个JPEG,将其传输到PNG并对结果字节执行某些操作。

I am almost certain I remember a library to do this exists, I cannot remember its name.

我几乎可以肯定我记得有一个库存在这个,我不记得它的名字。

4 个解决方案

#1


11  

ImageIO can be used to load JPEG files and save PNG files (also into a ByteArrayOutputStream if you don't want to write to a file).

ImageIO可用于加载JPEG文件并保存PNG文件(如果您不想写入文件,也可以保存到ByteArrayOutputStream中)。

#2


24  

This is what I ended up doing, I was thinking toooo far outside of the box when I asked the question..

这就是我最终做的事情,当我问这个问题时,我的想法太远了。

// these are the imports needed
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;

// read a jpeg from a inputFile
BufferedImage bufferedImage = ImageIO.read(new File(inputFile));

// write the bufferedImage back to outputFile
ImageIO.write(bufferedImage, "png", new File(outputFile));

// this writes the bufferedImage into a byte array called resultingBytes
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOut);
byte[] resultingBytes = byteArrayOut.toByteArray();

#3


13  

javax.imageio should be enough. Put your JPEG to BufferedImage, then save it with:

javax.imageio应该就够了。将您的JPEG放到BufferedImage,然后保存:

File file = new File("newimage.png");
ImageIO.write(myJpegImage, "png", file);

#4


-1  

BufferedImage bufferGambar;
try {

    bufferGambar = ImageIO.read(new File("ImagePNG.png"));
    // pkai type INT karna bertipe integer RGB bufferimage
    BufferedImage newBufferGambar = new BufferedImage(bufferGambar.getWidth(), bufferGambar.getHeight(), BufferedImage.TYPE_INT_RGB);

    newBufferGambar.createGraphics().drawImage(bufferGambar, 0, 0, Color.white, null);
    ImageIO.write(newBufferGambar, "jpg", new File("Create file JPEG.jpg"));

    JOptionPane.showMessageDialog(null, "Convert to JPG succes YES");

} catch(Exception e) {
    JOptionPane.showMessageDialog(null, e);
}

#1


11  

ImageIO can be used to load JPEG files and save PNG files (also into a ByteArrayOutputStream if you don't want to write to a file).

ImageIO可用于加载JPEG文件并保存PNG文件(如果您不想写入文件,也可以保存到ByteArrayOutputStream中)。

#2


24  

This is what I ended up doing, I was thinking toooo far outside of the box when I asked the question..

这就是我最终做的事情,当我问这个问题时,我的想法太远了。

// these are the imports needed
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;

// read a jpeg from a inputFile
BufferedImage bufferedImage = ImageIO.read(new File(inputFile));

// write the bufferedImage back to outputFile
ImageIO.write(bufferedImage, "png", new File(outputFile));

// this writes the bufferedImage into a byte array called resultingBytes
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOut);
byte[] resultingBytes = byteArrayOut.toByteArray();

#3


13  

javax.imageio should be enough. Put your JPEG to BufferedImage, then save it with:

javax.imageio应该就够了。将您的JPEG放到BufferedImage,然后保存:

File file = new File("newimage.png");
ImageIO.write(myJpegImage, "png", file);

#4


-1  

BufferedImage bufferGambar;
try {

    bufferGambar = ImageIO.read(new File("ImagePNG.png"));
    // pkai type INT karna bertipe integer RGB bufferimage
    BufferedImage newBufferGambar = new BufferedImage(bufferGambar.getWidth(), bufferGambar.getHeight(), BufferedImage.TYPE_INT_RGB);

    newBufferGambar.createGraphics().drawImage(bufferGambar, 0, 0, Color.white, null);
    ImageIO.write(newBufferGambar, "jpg", new File("Create file JPEG.jpg"));

    JOptionPane.showMessageDialog(null, "Convert to JPG succes YES");

} catch(Exception e) {
    JOptionPane.showMessageDialog(null, e);
}