com.sun.image.codec.jpeg

Interface JPEGImageEncoder

public interface JPEGImageEncoder

JPEGImageEncoder encodes buffers of image data into JPEG data streams. Users of this interface are required to provide image data in a Raster or a BufferedImage, set the necessary parameters in the JPEGEncodeParams object and successfully open the OutputStream that is the destination of the encoded JPEG stream. The JPEGImageEncoder interface can encode image data into interchange, and abbreviated JPEG data streams that are written to the OutputStream provided to the encoder.

Note that the classes in the com.sun.image.codec.jpeg package are not part of the core Java APIs. They are a part of Sun's JDK and JRE distributions. Although other licensees may choose to distribute these classes, developers cannot depend on their availability in non-Sun implementations. We expect that equivalent functionality will eventually be available in a core API or standard extension.

See Also:
JPEGCodec, JPEGEncodeParam, Raster, BufferedImage, OutputStream

Method Summary

void
encode(BufferedImage bi)
Encode a BufferedImage as a JPEG data stream.
void
encode(BufferedImage bi, JPEGEncodeParam jep)
Encode a BufferedImage as a JPEG data stream.
void
encode(Raster ras)
Encode a Raster as a JPEG data stream.
void
encode(Raster ras, JPEGEncodeParam jep)
Encode a Raster as a JPEG data stream.
int
getDefaultColorId(ColorModel cm)
Returns the 'default' encoded COLOR_ID for a given ColorModel.
JPEGEncodeParam
getDefaultJPEGEncodeParam(JPEGDecodeParam jdp)
This is a factory method for creating a JPEGEncodeParam from a JPEGDecodeParam.
JPEGEncodeParam
getDefaultJPEGEncodeParam(int numBands, int colorID)
This is a factory method for creating JPEGEncodeParam objects.
JPEGEncodeParam
getDefaultJPEGEncodeParam(BufferedImage bi)
This is a factory method for creating JPEGEncodeParam objects.
JPEGEncodeParam
getDefaultJPEGEncodeParam(Raster ras, int colorID)
This is a factory method for creating JPEGEncodeParam objects.
JPEGEncodeParam
getJPEGEncodeParam()
This returns a copy of the current JPEGEncodeParam object, if you want changes to affect the encoding process you must 'set' it back into the encoder (either through setJPEGEncodeParam or by providing the modified param object in the call to encode.
OutputStream
getOutputStream()
Return the stream the Encoder is currenlt associated with.
void
setJPEGEncodeParam(JPEGEncodeParam jep)
Set the JPEGEncodeParam object that is to be used for future encoding operations.

Method Details

encode

public void encode(BufferedImage bi)
            throws IOException,
                   ImageFormatException
Encode a BufferedImage as a JPEG data stream. Note, some color conversions may takes place. The current JPEGEncodeParam's encoded COLOR_ID should match the value returned by getDefaultColorID when given the BufferedImage's ColorModel.

If no JPEGEncodeParam object has been provided yet a default one will be created by calling getDefaultJPEGEncodeParam with bi.

Parameters:
bi - The BufferedImage to encode.
Usages and Demos :

View More Examples of encode(BufferedImage bi)
   1: import com.sun.image.codec.jpeg.JPEGCodec;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: 
   4:         ...
   5:     ByteArrayOutputStream  os = new ByteArrayOutputStream() ;
   6:     JPEGImageEncoder pp = JPEGCodec.createJPEGEncoder(os);
   7:         ...
   8:     pp.encode(bimage_) ;
   9:     ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()) ;

View Full Code Here
   1: import com.sun.image.codec.jpeg.JPEGCodec;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: public class JPEGServlet extends HttpServlet{
   4:         ...
   5:         graphics.fillRect(0,0,750*per/100,30);
   6:         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   7:         ...
   8:         encoder.encode(image);
   9:         out.close();

View Full Code Here
   1:      
   2:       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   3:         ...
   4:       encoder.encode(image);
   5:       out.flush();
   6: 
   7:     } catch (Exception e) {

View Full Code Here
   1:           FileOutputStream os = new FileOutputStream(file);
   2:           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
   3:           BufferedImage im = gui.finalPost;
   4:         ...
   5:           JPEGEncodeParam par = JPEGCodec.getDefaultJPEGEncodeParam(im);
   6:           encoder.encode(im);
   7:           os.close();
   8:         }
   9:         catch (IOException ioe) {}

View Full Code Here
   1:           FileOutputStream("smallMap.jpeg"));
   2:         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   3:         JPEGEncodeParam param = encoder.
   4:         ...
   5:         encoder.setJPEGEncodeParam(param);
   6:         encoder.encode(thumbImage);
   7:         out.close(); 
   8:         System.out.println("Done.");
   9:         System.exit(0);

View Full Code Here

encode

public void encode(BufferedImage bi,
                   JPEGEncodeParam jep)
            throws IOException,
                   ImageFormatException
Encode a BufferedImage as a JPEG data stream. Note, some color conversions may takes place. The jep's encoded COLOR_ID should match the value returned by getDefaultColorID when given the BufferedImage's ColorModel.

This call also sets the current JPEGEncodeParam object. The given JPEGEncodeParam object will be used for this and future encodings. If jep is null then a new JPEGEncodeParam object will be created by calling getDefaultJPEGEncodeParam with bi.

Parameters:
bi - The BufferedImage to encode.
jep - The JPEGEncodeParam object used to control the encoding.
Usages and Demos :

View More Examples of encode(BufferedImage bi,JPEGEncodeParam jep)
   1: import com.sun.image.codec.jpeg.JPEGEncodeParam;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: 
   4:         ...
   5:     try{
   6:         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   7:         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(compImage);
   8:         ...
   9:         param.setQuality(0.9f,true);
  10:         encoder.encode(compImage,param);
  11:     }

View Full Code Here
   1:         
   2:       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out );
   3:   
   4:         ...
   5:       param.setQuality( quality, true );
   6:       encoder.encode( img, param );
   7:       
   8:       FileOutputStream fos = new FileOutputStream("JPG"+quality+".jpg");
   9:       fos.write( out.toByteArray() );

View Full Code Here
   1: import com.sun.image.codec.jpeg.JPEGEncodeParam;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: 
   4:         ...
   5:         throws IOException {
   6:         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
   7:         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buf);
   8:         ...
   9:         param.setQuality(1, false);
  10:         encoder.encode(buf, param);
  11:     }

View Full Code Here
   1:       FileOutputStream out = new FileOutputStream(file);
   2:       JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(out);
   3:       JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(image);
   4:         ...
   5:       encodeParam.setQuality((float)0.9, true);
   6:       jpegEncoder.encode(image, encodeParam);
   7:       out.close();
   8: 
   9:     } catch (SVGParseException e) {

View Full Code Here
   1: import com.sun.image.codec.jpeg.JPEGEncodeParam;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: 
   4:         ...
   5:             OutputStream os = new FileOutputStream(imageFile);
   6:             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
   7:             JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buf);
   8:         ...
   9:             param.setQuality(1, false);
  10:             encoder.encode(buf, param);
  11:             os.flush();

View Full Code Here

encode

public void encode(Raster ras)
            throws IOException,
                   ImageFormatException
Encode a Raster as a JPEG data stream. Note that no color conversion takes place. It is required that you match the Raster to the encoded COLOR_ID contained in the current JPEGEncodeParam object.

If no JPEGEncodeParam object has been provided yet a new JPEGEncodeParam object will be created by calling getDefaultJPEGEncodeParam with ras and COLOR_ID_UNKNOWN.

Parameters:
ras - The Raster to encode.
Usages and Demos :

View More Examples of encode(Raster ras)
   1: import com.sun.image.codec.jpeg.JPEGCodec;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: 
   4:         ...
   5:     ByteArrayOutputStream  os = new ByteArrayOutputStream() ;
   6:     JPEGImageEncoder pp = JPEGCodec.createJPEGEncoder(os);
   7:         ...
   8:     pp.encode(bimage_) ;
   9:     ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()) ;

View Full Code Here
   1: import com.sun.image.codec.jpeg.JPEGCodec;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: public class JPEGServlet extends HttpServlet{
   4:         ...
   5:         graphics.fillRect(0,0,750*per/100,30);
   6:         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   7:         ...
   8:         encoder.encode(image);
   9:         out.close();

View Full Code Here
   1:      
   2:       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   3:         ...
   4:       encoder.encode(image);
   5:       out.flush();
   6: 
   7:     } catch (Exception e) {

View Full Code Here
   1:           FileOutputStream os = new FileOutputStream(file);
   2:           JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
   3:           BufferedImage im = gui.finalPost;
   4:         ...
   5:           JPEGEncodeParam par = JPEGCodec.getDefaultJPEGEncodeParam(im);
   6:           encoder.encode(im);
   7:           os.close();
   8:         }
   9:         catch (IOException ioe) {}

View Full Code Here
   1:           FileOutputStream("smallMap.jpeg"));
   2:         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   3:         JPEGEncodeParam param = encoder.
   4:         ...
   5:         encoder.setJPEGEncodeParam(param);
   6:         encoder.encode(thumbImage);
   7:         out.close(); 
   8:         System.out.println("Done.");
   9:         System.exit(0);

View Full Code Here

encode

public void encode(Raster ras,
                   JPEGEncodeParam jep)
            throws IOException,
                   ImageFormatException
Encode a Raster as a JPEG data stream. Note that no color conversion takes place. It is required that you match the Raster to the encoded COLOR_ID contained in the JPEGEncodeParam object. If jep is null a new JPEGEncodeParam object will be created by calling getDefaultJPEGEncodeParam with ras and COLOR_ID_UNKNOWN.
Parameters:
ras - The Raster to encode.
jep - The JPEGEncodeParam object used to control the encoding.
Usages and Demos :

View More Examples of encode(Raster ras,JPEGEncodeParam jep)
   1: import com.sun.image.codec.jpeg.JPEGEncodeParam;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: 
   4:         ...
   5:     try{
   6:         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   7:         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(compImage);
   8:         ...
   9:         param.setQuality(0.9f,true);
  10:         encoder.encode(compImage,param);
  11:     }

View Full Code Here
   1:         
   2:       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out );
   3:   
   4:         ...
   5:       param.setQuality( quality, true );
   6:       encoder.encode( img, param );
   7:       
   8:       FileOutputStream fos = new FileOutputStream("JPG"+quality+".jpg");
   9:       fos.write( out.toByteArray() );

View Full Code Here
   1: import com.sun.image.codec.jpeg.JPEGEncodeParam;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: 
   4:         ...
   5:         throws IOException {
   6:         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
   7:         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buf);
   8:         ...
   9:         param.setQuality(1, false);
  10:         encoder.encode(buf, param);
  11:     }

View Full Code Here
   1:       FileOutputStream out = new FileOutputStream(file);
   2:       JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(out);
   3:       JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(image);
   4:         ...
   5:       encodeParam.setQuality((float)0.9, true);
   6:       jpegEncoder.encode(image, encodeParam);
   7:       out.close();
   8: 
   9:     } catch (SVGParseException e) {

View Full Code Here
   1: import com.sun.image.codec.jpeg.JPEGEncodeParam;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: 
   4:         ...
   5:             OutputStream os = new FileOutputStream(imageFile);
   6:             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
   7:             JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(buf);
   8:         ...
   9:             param.setQuality(1, false);
  10:             encoder.encode(buf, param);
  11:             os.flush();

View Full Code Here

getDefaultColorId

public int getDefaultColorId(ColorModel cm)
Returns the 'default' encoded COLOR_ID for a given ColorModel. This method is not needed in the simple case of encoding Buffered Images (the library will figure things out for you). It can be useful for encoding Rasters. To determine what needs to be done to the image prior to encoding.
Parameters:
cm - The ColorModel to map to an jpeg encoded COLOR_ID.
Returns:
The default mapping of cm to a jpeg Color_ID note that in a few cases color conversion is required.

getDefaultJPEGEncodeParam

public JPEGEncodeParam getDefaultJPEGEncodeParam(JPEGDecodeParam jdp)
            throws ImageFormatException
This is a factory method for creating a JPEGEncodeParam from a JPEGDecodeParam. This will return a new JPEGEncodeParam object that is initialized from the JPEGDecodeParam object. All major pieces of information will be initialized from the DecodeParam (Markers, Tables, mappings).
Parameters:
jdp - The JPEGDecodeParam object to copy.

getDefaultJPEGEncodeParam

public JPEGEncodeParam getDefaultJPEGEncodeParam(int numBands,
                                                 int colorID)
            throws ImageFormatException
This is a factory method for creating JPEGEncodeParam objects. It is the users responsiblity to match the colorID with the given number of bands, which should match the data being encoded. Failure to do so may lead to poor compression and/or poor image quality. If you don't understand much about JPEG it is strongly recommended that you stick to the BufferedImage interface.
Parameters:
numBands - the number of bands that will be encoded (max of four).
colorID - the COLOR_ID for the encoded data. This is used to set reasonable defaults in the parameter object. This must match the number of bands given.

getDefaultJPEGEncodeParam

public JPEGEncodeParam getDefaultJPEGEncodeParam(BufferedImage bi)
            throws ImageFormatException
This is a factory method for creating JPEGEncodeParam objects. The returned object will do a credible job of encoding the given BufferedImage.

getDefaultJPEGEncodeParam

public JPEGEncodeParam getDefaultJPEGEncodeParam(Raster ras,
                                                 int colorID)
            throws ImageFormatException
This is a factory method for creating JPEGEncodeParam objects. It is the users responsiblity to match the colorID with the data contained in the Raster. Failure to do so may lead to either poor compression or poor image quality. If you don't understand much about JPEG it is strongly reccomended that you stick to the BufferedImage interfaces.

getJPEGEncodeParam

public JPEGEncodeParam getJPEGEncodeParam()
This returns a copy of the current JPEGEncodeParam object, if you want changes to affect the encoding process you must 'set' it back into the encoder (either through setJPEGEncodeParam or by providing the modified param object in the call to encode.
Returns:
A copy of the current JPEGEncodeParam object

getOutputStream

public OutputStream getOutputStream()
Return the stream the Encoder is currenlt associated with.
Usages and Demos :

View More Examples of getOutputStream()
   1:         private BufferedReader in; 
   2:         private JPEGImageEncoder encoder; 
   3:         private boolean done; 
   4:         ...
   5:                                    encoder.encode(image); 
   6:                                 encoder.getOutputStream().flush();  
   7:                             }
   8:         ...
   9:                      if(temp.isConnected()&&(!temp.isClosed()))
  10:                          baos.writeTo(temp.getOutputStream());
  11:                      else  clientConnection.remove(temp);

View Full Code Here

setJPEGEncodeParam

public void setJPEGEncodeParam(JPEGEncodeParam jep)
Set the JPEGEncodeParam object that is to be used for future encoding operations. 'jep' is copied so changes will not be tracked, unless you call this method again.
Parameters:
jep - The JPEGEncodeParam object to use for future encodings.
Usages and Demos :

View More Examples of setJPEGEncodeParam(JPEGEncodeParam jep)
   1:           FileOutputStream("smallMap.jpeg"));
   2:         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   3:         JPEGEncodeParam param = encoder.
   4:         ...
   5:         param.setQuality(1, false);
   6:         encoder.setJPEGEncodeParam(param);
   7:         encoder.encode(thumbImage);
   8:         out.close(); 
   9:         System.out.println("Done.");

View Full Code Here
   1:             bimg.createGraphics().drawImage(image, 0, 0, null);
   2:             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
   3:             JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimg);
   4:         ...
   5:             param.setQuality(1.0f, false);
   6:             encoder.setJPEGEncodeParam(param);
   7:             encoder.encode(bimg);
   8:             }
   9:         catch (Exception e)

View Full Code Here
   1: import com.sun.image.codec.jpeg.JPEGEncodeParam;
   2: import com.sun.image.codec.jpeg.JPEGImageEncoder;
   3: 
   4:         ...
   5:         BufferedOutputStream output = new BufferedOutputStream(sout);
   6:         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
   7:         JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
   8:         ...
   9: 
  10:         encoder.setJPEGEncodeParam(param);
  11:         encoder.encode(thumbImage);

View Full Code Here
   1:                 OutputStream os = new FileOutputStream(full_filename);
   2:                 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
   3:                 JPEGEncodeParam encodeParams = encoder.getDefaultJPEGEncodeParam(i);
   4:         ...
   5:                 encodeParams.setQuality(Globals.JPEGQuality, true);
   6:                 encoder.setJPEGEncodeParam(encodeParams);
   7:                 encoder.encode(i);
   8:                 os.close();
   9:                 System.out.println("Encoding successful");

View Full Code Here
   1:             ByteArrayOutputStream out = new ByteArrayOutputStream();
   2:             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   3:             JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
   4:         ...
   5:             param.setQuality(1.0f, false);
   6:             encoder.setJPEGEncodeParam(param);
   7:             encoder.encode(bi);
   8: 
   9:             ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

View Full Code Here