using JAI to create a thumbnail as a JPEG

Discussion in 'Java' started by mlevin, May 5, 2007.

  1. mlevin

    mlevin Guest

    I'm trying to use JAI to create a thumbnail of a JPEG and no matter
    what I do, I keep getting the following stack trace:

    exception

    javax.servlet.ServletException: All factories fail for the operation
    "encode"

    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:
    523)

    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:
    421)

    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:
    224)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:
    1194)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    root cause

    javax.media.jai.util.ImagingException: All factories fail for the
    operation "encode"

    javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:
    1687)

    javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:
    473)
    javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
    javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)
    javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
    javax.media.jai.RenderedOp.getRendering(RenderedOp.java:888)
    javax.media.jai.JAI.createNS(JAI.java:1099)
    javax.media.jai.JAI.create(JAI.java:973)
    javax.media.jai.JAI.create(JAI.java:1395)
    UploadAction.resizeImage(UploadAction.java:111)
    UploadAction.execute(UploadAction.java:71)

    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:
    419)

    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:
    224)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:
    1194)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    root cause

    java.io.IOException: reading encoded JPEG Stream
    sun.awt.image.codec.JPEGImageEncoderImpl.writeJPEGStream(Native
    Method)

    sun.awt.image.codec.JPEGImageEncoderImpl.encode(JPEGImageEncoderImpl.java:
    472)

    sun.awt.image.codec.JPEGImageEncoderImpl.encode(JPEGImageEncoderImpl.java:
    228)

    com.sun.media.jai.codecimpl.JPEGImageEncoder.encode(JPEGImageEncoder.java:
    277)
    com.sun.media.jai.opimage.EncodeRIF.create(EncodeRIF.java:70)
    sun.reflect.GeneratedMethodAccessor51.invoke(Unknown Source)

    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
    25)
    java.lang.reflect.Method.invoke(Method.java:585)
    javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)

    javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:
    1674)

    javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:
    473)
    javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
    javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)
    javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
    javax.media.jai.RenderedOp.getRendering(RenderedOp.java:888)
    javax.media.jai.JAI.createNS(JAI.java:1099)
    javax.media.jai.JAI.create(JAI.java:973)
    javax.media.jai.JAI.create(JAI.java:1395)
    UploadAction.resizeImage(UploadAction.java:111)
    UploadAction.execute(UploadAction.java:71)

    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:
    419)

    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:
    224)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:
    1194)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

    The code I'm using is cobbled together from several examples and
    clearly I'm not doing something right. Here's what I've got so far:

    import java.awt.RenderingHints;
    import java.awt.image.BufferedImage;
    import java.awt.image.renderable.ParameterBlock;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io_OutputStream;

    import javax.media.jai.Interpolation;
    import javax.media.jai.JAI;
    import javax.media.jai.OpImage;
    import javax.media.jai.RenderedOp;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.apache.struts.action.Action;
    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;

    import com.sun.media.jai.codec.JPEGEncodeParam;
    import com.sun.media.jai.codec.PNGEncodeParam;
    import com.sun.media.jai.codec.SeekableStream;

    public class UploadAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) throws
    IOException {

    String basePath = "C:\\Program Files\\Apache Software Foundation\
    \Tomcat 5.5\\webapps\\strutsTest\\";

    UploadForm upload = (UploadForm) form;

    if (upload.getFile() != null) {
    /*
    *
    *
    *
    *
    *
    */

    // open input stream from uploaded file and copy into byte array
    InputStream is = new
    BufferedInputStream(upload.getFile().getInputStream());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int bytesRead = 0;
    byte[] buffer = new byte[8192];
    while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
    bos.write(buffer, 0, bytesRead);
    }
    bos.close();
    is.close();
    byte[] image = bos.toByteArray();

    // write original (unresized) file
    OutputStream os = new BufferedOutputStream(new
    FileOutputStream(basePath + upload.getFile().getFileName()));
    ByteArrayInputStream bis = new ByteArrayInputStream(image);
    bytesRead = 0;
    buffer = new byte[8192];
    while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
    os.write(buffer, 0, bytesRead);
    }
    os.close();

    // resize image
    bis.reset();
    OutputStream osThumb = new BufferedOutputStream(new
    FileOutputStream(basePath + "THUMB_" +
    upload.getFile().getFileName()));
    resizeImage(bis, os, 150, 150);
    osThumb.close();

    // close input stream
    bis.close();

    }

    return mapping.findForward("success");

    }

    // based on code examples from http://www.i-proving.ca/space/Technologies/Java Advanced Imaging,
    http://forums.java.net/jive/thread.jspa?messageID=64672 and
    http://archives.java.sun.com/cgi-bin/wa?A2=ind0208&L=java-imageio-interest&P=242
    private void resizeImage(InputStream is, OutputStream os, int width,
    int height) {

    // read in the original image from an input stream
    SeekableStream s = SeekableStream.wrapInputStream(is, true);
    RenderedOp image = JAI.create("stream", s);
    ((OpImage) image.getRendering()).setTileCache(null);

    // now resize the image
    double hRatio = ((double) height) / ((double) image.getHeight());
    double wRatio = ((double) width) / ((double) image.getWidth());
    double scale = Math.min(hRatio, wRatio);
    ParameterBlock pb = new ParameterBlock();
    pb.addSource(image).add(scale).add(scale);
    pb.add(Interpolation.getInstance(Interpolation.INTERP_BICUBIC));
    RenderingHints qualityHints = new
    RenderingHints(RenderingHints.KEY_RENDERING,
    RenderingHints.VALUE_RENDER_QUALITY);
    RenderedOp resizedImage = JAI.create("SubsampleAverage", pb,
    qualityHints);

    // lastly, write the newly-resized image to an
    // output stream, in a specific encoding
    pb = new ParameterBlock();
    BufferedImage bi = resizedImage.getAsBufferedImage();
    pb.addSource(bi);
    pb.add(os);
    pb.add("jpeg");
    JPEGEncodeParam ep = new JPEGEncodeParam();
    ep.setQuality(0.75f);
    pb.add(ep);
    JAI.create("encode", pb);

    }

    }

    Any suggestions? I'm baffled.

    Thanks
     
    mlevin, May 5, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi
    Replies:
    1
    Views:
    518
    john smith
    Feb 1, 2006
  2. surendra
    Replies:
    1
    Views:
    8,523
    dipakPardesi
    Jun 21, 2008
  3. Replies:
    17
    Views:
    3,589
    Andrey Kuznetsov
    Oct 25, 2005
  4. lovaspillando
    Replies:
    0
    Views:
    1,065
    lovaspillando
    Aug 26, 2007
  5. Ivan Alameda Carballo
    Replies:
    0
    Views:
    510
    Ivan Alameda Carballo
    Aug 26, 2007
Loading...

Share This Page