Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1580 > unrolled thread

using JAI to create a thu

Started by"mlevin" <mlevin@THRWHITE.remove-dii-this>
First post2011-04-27 15:34 +0000
Last post2011-04-27 15:34 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.gui


Contents

  using JAI to create a thu "mlevin" <mlevin@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000
    Re: using JAI to create a "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:34 +0000

#1580 — using JAI to create a thu

From"mlevin" <mlevin@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
Subjectusing JAI to create a thu
Message-ID<1178377882.053099.72580@p77g2000hsh.googlegroups.com>
  To: comp.lang.java.programmer
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

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1581 — Re: using JAI to create a

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:34 +0000
SubjectRe: using JAI to create a
Message-ID<71bae4561395c@uwe>
In reply to#1580
  To: comp.lang.java.gui
mlevin wrote:
>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:
..
> java.io.IOException: reading encoded JPEG Stream
..
>Any suggestions? I'm baffled.

Whenever a "why cannot 'a' API open 'b' media and do..?"
type question arises, I wonder, did the OP check that the 
media in question (or its codec or internal format) is 
compatible with the API?

So, can you do operations on this image from the command 
line using JAI?  Can you do the operation using the example 
media supplied* with JAI?  

* Is there any example media with JAI?

Note also that servlets and serverside code is harder 
to develop and debug than command line classes, so 
I advise getting an example running, that works from 
the command line - before wonderring why the servlet 
breaks.

-- 
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200705/1

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web