[ Back | Previous | Next ]

How to read/write JPG files using stream?

Package:
java.io.*
Product:
JDK
Release:
1.1.x
Related Links:
General
File
FilenameFilter
FileWriter
JPEGCodec
ObjectInputStream
OutputStream
PipedInputStream
PrintWriter
StreamTokenizer
Comment:
 // in this code, I take back an Image from a Document and save it as JPEG image
 ImageIcon theIcon =
 (ImageIcon)((AbstractDocument.LeafElement)theElement).getAttribute(theObj);
 Image theImage = theIcon.getImage();
 BufferedImage buf = new
 BufferedImage(theIcon.getIconWidth(),theIcon.getIconHeight(),BufferedImage.TYPE_INT_BGR);
 Graphics g = buf.createGraphics();
 g.drawImage(theImage,0,0,null);
 try{
    File theFile = new File(//your filename of the jpeg picture);
    JPEGCodec.createJPEGEncoder(new FileOutputStream(theFile)).encode(buf);
 }catch(java.io.IOException ioe){System.out.println(ioe.getMessage());}
1