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

发布于 2024-07-09 17:55:06 字数 101 浏览 2 评论 0原文

在我的 Java 应用程序中,我想下载 JPEG,将其传输为 PNG 并使用生成的字节执行某些操作。

我几乎可以肯定我记得有一个库可以做到这一点,但我不记得它的名字了。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦初启 2024-07-16 17:55:06

这就是我最终所做的,当我问这个问题时,我想得太离谱了。

// 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();

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();
压抑⊿情绪 2024-07-16 17:55:06

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

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).

烟柳画桥 2024-07-16 17:55:06

javax.imageio 应该足够了。
将 JPEG 放入 BufferedImage,然后使用以下命令保存:

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

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

File file = new File("newimage.png");
ImageIO.write(myJpegImage, "png", file);
小…红帽 2024-07-16 17:55:06
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);
}
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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文