Table of Contents

saveasjpg

versions0095+
contributorsseltar
started on2006-03-02 06:58

Saving a PImage as jpg is easier than it seems..
usage: saveBytes(“filename.jpg”,bufferImage(get(0,0,width,height))); // gets the entire applet screen and saves it to disk

Source code

/**
saveasjpg taken from http://processinghacks.com/hacks:saveasjpg
@author Yonas Sandbæk
*/
 
import com.sun.image.codec.jpeg.*;
 
byte[] bufferImage(PImage srcimg){
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  BufferedImage img = new BufferedImage(srcimg.width, srcimg.height, 2);
  img = (BufferedImage)createImage(srcimg.width,srcimg.height);
  for(int i = 0; i < srcimg.width; i++)
  	for(int j = 0; j < srcimg.height; j++)
  		img.setRGB(i,j,srcimg.pixels[j*srcimg.width+i]);
  try{
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(img);
    encpar.setQuality(1,false);
    encoder.setJPEGEncodeParam(encpar);
    encoder.encode(img);
  }
  catch(FileNotFoundException e){
    System.out.println(e);
  }
  catch(IOException ioe){
    System.out.println(ioe);
  }
  return out.toByteArray();
}

Related Links

hacks/saveasjpg.txt · Last modified: 2007-07-13 02:12 (external edit)