I am trying to save the current frame to a file, but I cannot use "saveFrame" because I want to be able to control the JPG "Quality". In the code below it is 50%.
I also want to have access to the JPG-encoded bytes, since I plan to send them over the net to a server. For now, I am saving to a file to check the JPG encoding worked.
With the code below, an image file gets created, but the colours are all wrong.
The problem seems to be the pixel format parameter in:
Code:new BufferedImage(width, height, 1);
Does anyone know the proper parameter value for the "ARGB" format used by Processing?
I looked in the Processing source code for the "save" function (saveImageIO, actually), and I can't quite follow it. Also, ... I don't know how to modify it to encode the image into a byte array, instead of writing it directly to a file.
http://dev.processing.org/source/index.cgi/trunk/processing/core/src/processing/core/PImage.java?view=markup
If there are any Java / JPG gurus out there, I'd appreciate any help you can offer.
Code:
void SaveJpg(String fname) {
loadPixels();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
BufferedImage img = new BufferedImage(width, height, 1);
img = (BufferedImage) createImage(width, height);
/* for (j = 0; j < height; ++j) {
k = j*width;
for (i = 0; i < width; ++i) {
img.setRGB(i,j, pixels[k+i]);
}
}
*/
img.setRGB(0,0,width,height,pixels,0,width);
encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam p = encoder.getDefaultJPEGEncodeParam(img);
// set JPEG quality to 50% with baseline optimization
p.setQuality(0.50,true);
encoder.setJPEGEncodeParam(p);
encoder.encode(img);
byte [] a = out.toByteArray();
saveBytes(fname,a);
}
catch(FileNotFoundException e){
System.out.println(e);
}
catch(IOException ioe){
System.out.println(ioe);
}
}
thanks,
djones