2008-12-10 4 views

ответ

9

ImageIO может быть использован для загрузки файлов JPEG и сохранить PNG файлы (также в ByteArrayOutputStream, если вы не хотите, чтобы записать в файл).

12

javax.imageio должно быть достаточно. Поместите JPEG в BufferedImage, а затем сохранить его:

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

Это то, что я в конечном итоге делает, я думал 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(); 
+1

пример кода был полезным здесь. Рад не писать в новый файл. – clay 2016-11-04 18:28:42

0
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); 
} 
+0

Что не так с этим? – 2017-04-06 14:02:50

Смежные вопросы