Hi,

I just wrote this email to somebody else, but it should solve your 
problem too.

Hello,

here is a simple jsp-page, that takes a BufferedImage (provided as 
sessionAttribute) converts it and shows it as jpeg.

<%@ page contentType="image/jpeg"
   import="java.awt.image.*, com.sun.image.codec.jpeg.*" %>

<%
   BufferedImage InputImage = (BufferedImage)session.getAttribute("Image");
   ServletOutputStream sos = response.getOutputStream();
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
   encoder.encode(InputImage);
%>
              You can also take a class that reads a file, converts it 
to a jpeg-file and writes this file to disk. Same problem here, working 
commandline it's fine, running in a servlet I am getting 
OutOfMemory-Error. You have to install JavaAdvancedImaging for this.


-------------------------------------------------------------------------------------------------------------------------------------------------------------------
 

// Klasse zum Einlesen von JAI-bekannten Grafikformaten und Abspeichern 
in JPEG
//
// A.Rosenheinrich
// last changed: 10.08.01

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.image.renderable.*;
import java.io.*;
import javax.media.jai.*;
import javax.media.jai.widget.*;
import com.sun.media.jai.codec.*;

public class ToJPEG
{

 private ImageEncoder encoder = null;
 private JPEGEncodeParam encodeParam = null;


 public static void main(String args[])
 {
    ToJPEG jtest = new ToJPEG(args);
 }

  // Load the source image
 private PlanarImage loadImage(String imageName)
 {
    ParameterBlock pb = (new ParameterBlock()).add(imageName);
    PlanarImage src = JAI.create("fileload", pb);
    if (src == null)
    {
       System.out.println("Error in loading image " + imageName);
       System.exit(1);
    }
    return src;
 }

 // Create the image encoder
 private void encodeImage(PlanarImage img, FileOutputStream out)
 {
    encoder = ImageCodec.createImageEncoder("JPEG", out, encodeParam);
    try
    {
       encoder.encode(img);
       out.close();
    }
    catch (IOException e)
    {
       System.out.println("IOException at encoding..");
       System.exit(1);
    }
 
}                                                                                      
                                 


 private FileOutputStream createOutputStream(String outFile)
 {
    FileOutputStream out = null;
    try
    {
       out = new FileOutputStream(outFile);
    }
    catch(IOException e)
    {
       System.out.println("IOException.");
       System.exit(1);
    }

    return out;
 }

 public ToJPEG(String args[])
 {
    // check parameters from command line arguments.
    // first parameter: inputfile
    // second paramter: output jpg-file

    if(args.length != 2)
    {
       System.out.println("Wrong number of input parameters !!!");
       System.exit(1);
     }

    FileOutputStream out1 = createOutputStream(args[1]);


    // Create the source op image
    PlanarImage src = loadImage(args[0]);

    double[] constants = new double[3];
    constants[0] = 0.0;
    constants[1] = 0.0;
    constants[2] = 0.0;
    ParameterBlock pb = new ParameterBlock();
    pb.addSource(src);
    pb.add(constants);

    // Create a new src image with weird tile sizes
    ImageLayout layout = new ImageLayout();
    layout.setTileWidth(57);
    layout.setTileHeight(57);
    RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, 
layout);
    PlanarImage src1 = JAI.create("addconst", pb, hints);

    // ----- End src loading ------


    // Set the encoding parameters if necessary
    encodeParam = new JPEGEncodeParam();

    encodeParam.setQuality(0.2F);

    //encodeParam.setHorizontalSubsampling(0, 1);
    //encodeParam.setHorizontalSubsampling(1, 2);
    //encodeParam.setHorizontalSubsampling(2, 2);

    //encodeParam.setVerticalSubsampling(0, 1);
    //encodeParam.setVerticalSubsampling(1, 1);
    //encodeParam.setVerticalSubsampling(2, 1);

    encodeParam.setRestartInterval(64);
    //encodeParam.setWriteImageOnly(false);
    //encodeParam.setWriteTablesOnly(true);
    //encodeParam.setWriteJFIFHeader(true);

    // Create the encoder
    encodeImage(src, out1);
    //PlanarImage dst1 = loadImage(args[1]);
 }
}

--------------------------------------------------------------------------------------------------------------------------------
 

Hope it helps

Greetings
Andrej

Reply via email to