在我的 Java 应用程序中,我想下载 JPEG,将其传输到 PNG 并使用生成的字节做一些事情。
我几乎可以肯定我记得存在一个这样做的图书馆,我不记得它的名字。
在我的 Java 应用程序中,我想下载 JPEG,将其传输到 PNG 并使用生成的字节做一些事情。
我几乎可以肯定我记得存在一个这样做的图书馆,我不记得它的名字。
这就是我最终要做的,当我问这个问题时,我想得太远了。
// 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();
javax.imageio应该足够了。将您的 JPEG 文件放入 BufferedImage,然后使用以下命令保存:
File file = new File("newimage.png");
ImageIO.write(myJpegImage, "png", file);
ImageIO可用于加载 JPEG 文件和保存 PNG 文件(ByteArrayOutputStream
如果您不想写入文件,也可用于保存)。
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);
}