在我的Java应用程序中,我想下载一个JPEG文件,将其转换为PNG格式并对结果字节进行一些操作。使用Java对图像进行代码转换(JPEG转PNG)
我几乎肯定我记得一个图书馆做到这一点存在,我不记得它的名字。
在我的Java应用程序中,我想下载一个JPEG文件,将其转换为PNG格式并对结果字节进行一些操作。使用Java对图像进行代码转换(JPEG转PNG)
我几乎肯定我记得一个图书馆做到这一点存在,我不记得它的名字。
ImageIO可以用来加载JPEG文件,并保存PNG文件(也为ByteArrayOutputStream
,如果你不希望写入文件)。
javax.imageio应该足够了。 把你的JPEG到BufferedImage后,再与保存:
File file = new File("newimage.png");
ImageIO.write(myJpegImage, "png", file);
这是我落得这样做,我想TOOOO远远框外,当我问的问题..
// 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();
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);
}
这个有什么问题? – 2017-04-06 14:02:50
代码示例是有帮助这里。很高兴不写出一个新文件。 – clay 2016-11-04 18:28:42