How To Resize Uploaded Images Using Java – Better Way

Based on helpful comments from Matt on this previous post:

How To Resize Uploaded Images Using Java

I have upgraded the image resizing code. The results are noticeably better in quality, even at thumbnail sizes. I wanted to share the completed new code, in case anyone needs it.

Again, thanks to Matt S. for his pointers.

    /**
     * The JAI.create action name for handling a stream.
     */
    private static final String JAI_STREAM_ACTION = "stream";

    /**
     * The JAI.create action name for handling a resizing using a subsample averaging technique.
     */
    private static final String JAI_SUBSAMPLE_AVERAGE_ACTION = "SubsampleAverage";

    /**
     * The JAI.create encoding format name for JPEG.
     */
    private static final String JAI_ENCODE_FORMAT_JPEG = "JPEG";

    /**
     * The JAI.create action name for encoding image data.
     */
    private static final String JAI_ENCODE_ACTION = "encode";

    /**
     * The http content type/mime-type for JPEG images.
     */
    private static final String JPEG_CONTENT_TYPE = "image/jpeg";


    private int mMaxWidth = 800;

    private int mMaxWidthThumbnail = 150;

.....



    /**
     * This method takes in an image as a byte array (currently supports GIF, JPG, PNG and 
     * possibly other formats) and
     * resizes it to have a width no greater than the pMaxWidth parameter in pixels. 
     * It converts the image to a standard
     * quality JPG and returns the byte array of that JPG image.
     * 
     * @param pImageData
     *                the image data.
     * @param pMaxWidth
     *                the max width in pixels, 0 means do not scale.
     * @return the resized JPG image.
     * @throws IOException
     *                 if the image could not be manipulated correctly.
     */
    public byte[] resizeImageAsJPG(byte[] pImageData, int pMaxWidth) throws IOException {
	InputStream imageInputStream = new ByteArrayInputStream(pImageData);
	// read in the original image from an input stream
	SeekableStream seekableImageStream = SeekableStream.wrapInputStream(imageInputStream, true);
	RenderedOp originalImage = JAI.create(JAI_STREAM_ACTION, seekableImageStream);
	((OpImage) originalImage.getRendering()).setTileCache(null);
	int origImageWidth = originalImage.getWidth();
	// now resize the image
	double scale = 1.0;
	if (pMaxWidth > 0 && origImageWidth > pMaxWidth) {
	    scale = (double) pMaxWidth / originalImage.getWidth();
	}
	ParameterBlock paramBlock = new ParameterBlock();
	paramBlock.addSource(originalImage); // The source image
	paramBlock.add(scale); // The xScale
	paramBlock.add(scale); // The yScale
	paramBlock.add(0.0); // The x translation
	paramBlock.add(0.0); // The y translation

	RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_RENDERING,
		RenderingHints.VALUE_RENDER_QUALITY);

	RenderedOp resizedImage = JAI.create(JAI_SUBSAMPLE_AVERAGE_ACTION, paramBlock, qualityHints);

	// lastly, write the newly-resized image to an output stream, in a specific encoding
	ByteArrayOutputStream encoderOutputStream = new ByteArrayOutputStream();
	JAI.create(JAI_ENCODE_ACTION, resizedImage, encoderOutputStream, JAI_ENCODE_FORMAT_JPEG, null);
	// Export to Byte Array
	byte[] resizedImageByteArray = encoderOutputStream.toByteArray();
	return resizedImageByteArray;
    }

57 thoughts on “How To Resize Uploaded Images Using Java – Better Way

  1. Hi!
    Thanks for this code! I tried many different code-pieces, but yours was the only one which worked.
    The result looks good too. =)
    Greetings, Maeve

  2. I’m trying to test out this improved version in an existing app I have, and I’m getting this error when I run it:

    Exception in thread “AgentThread: JavaAgent” java.lang.NoClassDefFoundError: com.sun.media.jai.codec.SeekableStream
    at com.lekkimworld.lotusphere2007.picture_importer.PictureImporter.resizeImageAsJPG(PictureImporter.java:111)
    at com.lekkimworld.lotusphere2007.picture_importer.PictureImporter.importPicture(PictureImporter.java:326)
    at JavaAgent.NotesMain(JavaAgent.java:19)
    at lotus.domino.AgentBase.runNotes(Unknown Source)
    at lotus.domino.NotesThread.run(Unknown Source)

    In Eclipse, I’ve referenced jai_codec.jar and jai_core.jar, and I verified that they exist where their paths point on my file system. Any ideas what the cause of this could be? I’m anxious to test the quality of this improved version over my homebrew code (very similar to Devon’s first posting on the topic).

    take care, Mike

  3. Mike:

    it compiles in Eclipse I assume, and just throws this error during runtime? You should check the classpath for the running app and ensure the jai_codec.jar is in the classpath of the app (outside of eclipse).

    Just my initial thoughts…

  4. Ahh, you’re completely correct…this was my dumb mistake! I’m running this app in Lotus Notes, and I forgot to move the jai JAR files over to my running app! Thanks Devon…works like a charm, and much better quality than my earlier code!

    take care,
    MIke

  5. @Matthias: The encoder call is actually using the JAI_ENCODE_FORMAT_JPEG constant, which has a value of “JPEG”. The JPEG_CONTENT_TYPE constant isn’t used in that code snippet, but it used elsewhere in the class I copied the code from to set mime type headers into the database record for the file metadata.

  6. Iam new to JAI and i tried using the code snipped provided here to resize an image.I get the following exception:
    java.lang.RuntimeException: – Unable to render RenderedOp for this operation.
    Pls let me know wht could be the reason for the same

  7. @Newbie: perhaps the image format isn’t supported by the codecs in JAI? What is the image format you are working with? Can you share the full stack trace of the error?

    Thanks!

  8. I just wanted to say that having tried some really horrible messy code to integrate apache commons fileupload and some awt-based image resizing code, I stumbled across your artice, and the code dropped into my project seamlessly. Brilliant.

  9. I’ve found that when I need to scale a large image down to less than 50% of the original size, this method works GREAT! However, when (0.5d <= scale < 1.0d) this really didn’t produce very good images for me. I had to modify the code to resize using up to three different methods depending on the value of scale. See the code I’ve provided here:
    http://pastebin.com/f13fbbfb3

    I welcome any feedback!

  10. Devon,

    I like your work here. One question (and maybe I just missed reading up on it on your article(s)), any idea on what I can do to deal with images that have transparencies? GIF in particular.

    Thanks in advance,

    Adrian

  11. Hi Devon,

    I am another Newbie to JAI and I too get the same exception as Newbie. My stack trace as below:

    Exception in thread “main” java.lang.RuntimeException: – Unable to render RenderedOp for this operation.
    at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:826)
    at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:866)
    at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:887)
    at testfiles.ResizeImage.resizeImageAsJPG(ResizeImage.java:89)
    at testfiles.ResizeImage.main(ResizeImage.java:30)

    I call your method as below in main method of a standalone java class:

    try{
    File file = new File(“C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Blue hills.jpg”);
    long length = file.length();
    byte[] bytes = new byte[(int)length];
    new ResizeImage().resizeImageAsJPG(bytes, 20);
    }catch (IOException e) {
    e.printStackTrace();
    }

    Please let me know where I am going wrong.

  12. Well on loading data into byte array as in the example link I get the following error:

    Error: Could not find mediaLib accelerator wrapper classes. Continuing in pure Java mode.
    Occurs in: com.sun.media.jai.mlib.MediaLibAccessor
    java.lang.NoClassDefFoundError: com/sun/medialib/mlib/Image
    at com.sun.media.jai.mlib.MediaLibAccessor$1.run(MediaLibAccessor.java:248)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.media.jai.mlib.MediaLibAccessor.setUseMlib(MediaLibAccessor.java:245)
    at com.sun.media.jai.mlib.MediaLibAccessor.useMlib(MediaLibAccessor.java:177)
    at com.sun.media.jai.mlib.MediaLibAccessor.isMediaLibCompatible(MediaLibAccessor.java:357)
    at com.sun.media.jai.mlib.MediaLibAccessor.isMediaLibCompatible(MediaLibAccessor.java:315)
    at com.sun.media.jai.mlib.MlibSubsampleAverageRIF.create(MlibSubsampleAverageRIF.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)
    at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)
    at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)
    at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
    at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:818)
    at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:866)
    at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:887)
    at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:798)
    at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:866)
    at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:887)
    at javax.media.jai.JAI.createNS(JAI.java:1099)
    at javax.media.jai.JAI.create(JAI.java:973)
    at javax.media.jai.JAI.create(JAI.java:1668)
    at testfiles.ResizeImage.resizeImageAsJPG(ResizeImage.java:146)
    at testfiles.ResizeImage.main(ResizeImage.java:38)
    Caused by: java.lang.ClassNotFoundException: com.sun.medialib.mlib.Image
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    … 26 more

    Its breaking at this point in the resizeImageAsJPG which is line 146

    JAI.create(JAI_ENCODE_ACTION, resizedImage, encoderOutputStream, JAI_ENCODE_FORMAT_JPEG, null);

    Anything I missed?

  13. Pingback: How To Resize Uploaded Images Using Java | Devon Hillard Tech Blog

  14. Hi Devon,

    Is there a better way to crop images vertically to a specific height for specific widths if the height is beyond certain limit. For e.g.
    if the resized image’s width is 160 px and its height is beyond 200px than it should be cropped vertically upto 200 px in height. The cropping should be proportionate from top as well as bottom and retain the center portion of the resized image as is. Appreciate your help.

    • NewBie2JAI: a quick googling for “JAI crop image” gave some good results. This code snippet looks promising, but I’d definitely google for it and read the docs on the JAI “crop” action.

      RenderedImage ri = JAI.create(“fileload”,pathandfilename);

      public void crop()
      {
      pb = new ParameterBlock();
      pb.addSource(ri);
      pb.add((float)topLeftmx);
      pb.add((float)topLeftmy);
      pb.add((float)roiWidth);
      pb.add((float)roiHeight);
      ri = JAI.create(“crop”,pb);
      }

  15. Thanks Devon. I too googled it out and impemented it but I am getting this error:”Crop The rectangular crop area must not be outside the image.” Working on it.. Thanks though.

  16. I had very bad quality resized images, and it appeared that the solution was to specify the interpolation.
    The default is the InterpolationNearest, but the InterpolationBilinear or InterpolationBicubic give much better quality.
    But then there was an other problem with those interpolations : black borders sometimes appear after scale operation.
    So I had to scale it with 2 more pixels, and then crop it with x=1 y=1 and the size I want.

  17. This code looks great. But i am facing one small (small??? or big???) issue. I am getting this exception. can you please show me the way on this

    java.lang.IllegalArgumentException: SubsampleAverage: No OperationDescriptor is registered in the current operation registry under this name.
    at javax.media.jai.JAI.createNS(Ljava/lang/String;Ljava/awt/image/renderable/ParameterBlock;Ljava/awt/RenderingHints;)Ljavax/media/jai/RenderedOp;(JAI.java:883)
    at javax.media.jai.JAI.create(Ljava/lang/String;Ljava/awt/image/renderable/ParameterBlock;Ljava/awt/RenderingHints;)Ljavax/media/jai/RenderedOp;(JAI.java:786)

  18. I have this problem, when the code it’s executed in a machine with Linux, and if the file is a GIF

    java.lang.RuntimeException: – Unable to render RenderedOp for this operation.
    at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:827)
    at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
    at javax.media.jai.RenderedOp.getWidth(RenderedOp.java:2179)

    Any ideas?

  19. Guys,

    Let me congratulate everyone on doing an extremely wonderful job here.

    I have been working on a TIFF document viewe/editor (applet+JAI) piece.

    It provides zoom and rotate functionality using the “scale” and “transpose” operators on JAI.create(). Now my applet is signed and I have JAI installed on the client machine (jai_imageio.jar, jai_core.jar, jai_codec.jar) in the JRE/lib/ext folder.

    The applets loads fine, downloading my applet JAR, and the TIFF document is retrieved from the server and displayed in the applet, within the browser. I use javascript buttons to call on the applet functions to zoom and rotate.

    ROTATE CODE
    —————————————
    ParameterBlock pb = new ParameterBlock();
    pb.addSource(image);
    pb.add(TransposeDescriptor.ROTATE_90); //assume I always rotate by 90 degrees
    RenderedOp rotatedImage = JAI.create(“transpose”, pb);

    ZOOM CODE:
    ——————————————
    ParameterBlock pb = new ParameterBlock();
    pb.addSource(image);
    pb.add((float)magFactor);
    pb.add((float)magFactor);
    pb.add(0.0F);
    pb.add(0.0F);
    RenderedImage scaledImage = JAI.create(“scale”, pb, null);

    ——————————————————-

    I am getting the following exceptions for Zoom and Rotate respectively

    ZOOM EXCEPTION
    ——————————————————-
    java.lang.reflect.InvocationTargetException

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at gov.wisconsin.viewer.framework.controller.AbstractControllerApplet.invokeService(AbstractControllerApplet.java:83)

    at gov.wisconsin.viewer.framework.controller.AbstractControllerApplet.perform(AbstractControllerApplet.java:106)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at sun.plugin.javascript.invoke.JSInvoke.invoke(Unknown Source)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)

    at sun.plugin.com.MethodDispatcher.invoke(Unknown Source)

    at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)

    at sun.plugin.com.DispatchImpl$1.run(Unknown Source)

    at java.security.AccessController.doPrivileged(Native Method)

    at sun.plugin.com.DispatchImpl.invoke(Unknown Source)

    Caused by: java.lang.ExceptionInInitializerError

    at gov.wisconsin.viewer.management.TiffManager.magnifyAndShow(TiffManager.java:240)

    at gov.wisconsin.viewer.management.TiffManager.zoom(TiffManager.java:266)

    … 21 more

    Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThreadGroup)

    at java.security.AccessControlContext.checkPermission(Unknown Source)

    at java.security.AccessController.checkPermission(Unknown Source)

    at java.lang.SecurityManager.checkPermission(Unknown Source)

    at sun.applet.AppletSecurity.checkAccess(Unknown Source)

    at java.lang.ThreadGroup.checkAccess(Unknown Source)

    at java.lang.ThreadGroup.(Unknown Source)

    at java.lang.ThreadGroup.(Unknown Source)

    at com.sun.media.jai.util.SunTileScheduler.(SunTileScheduler.java:689)

    at javax.media.jai.JAI.(JAI.java:560)

    … 23 more

    ROTATE EXCEPTION
    ——————————————————-

    java.lang.reflect.InvocationTargetException

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at gov.wisconsin.viewer.framework.controller.AbstractControllerApplet.invokeService(AbstractControllerApplet.java:83)

    at gov.wisconsin.viewer.framework.controller.AbstractControllerApplet.perform(AbstractControllerApplet.java:106)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at sun.plugin.javascript.invoke.JSInvoke.invoke(Unknown Source)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

    at java.lang.reflect.Method.invoke(Unknown Source)

    at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)

    at sun.plugin.com.MethodDispatcher.invoke(Unknown Source)

    at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)

    at sun.plugin.com.DispatchImpl$1.run(Unknown Source)

    at java.security.AccessController.doPrivileged(Native Method)

    at sun.plugin.com.DispatchImpl.invoke(Unknown Source)

    Caused by: java.lang.ExceptionInInitializerError

    at gov.wisconsin.viewer.management.TiffManager.rotate(TiffManager.java:337)

    … 21 more

    Caused by: java.security.AccessControlException: access denied (java.lang.RuntimePermission modifyThreadGroup)

    at java.security.AccessControlContext.checkPermission(Unknown Source)

    at java.security.AccessController.checkPermission(Unknown Source)

    at java.lang.SecurityManager.checkPermission(Unknown Source)

    at sun.applet.AppletSecurity.checkAccess(Unknown Source)

    at java.lang.ThreadGroup.checkAccess(Unknown Source)

    at java.lang.ThreadGroup.(Unknown Source)

    at java.lang.ThreadGroup.(Unknown Source)

    at com.sun.media.jai.util.SunTileScheduler.(SunTileScheduler.java:689)

    at javax.media.jai.JAI.(JAI.java:560)

    … 22 more

    ———————————————————

    Both exceptions trace down to the line where I use JAI.create() method for scale or rotate. I thought by signing the jar all access exceptions would be resolved. But it seems I am still getting “Caused by: java.security.AccessControlException: access denied” exceptions.

    Now I am not sure what am I doing wrong. Is there anything special that I need to do within applet in order to get the JAI.create() method to work?

    Any feedback, pointers, help would be very appreciated !

    Regards,
    Nix4J

  20. Hi, I have followed your method to do my image scaling & resizing. It works well for jpegs and some png files, but I am having a problem with transparent gifs and some png files. They come out pink in color. What could be the reason? (I have used your code as given above).

  21. hi i have a question..
    How do i use this code if i have a 100×100 jpg and i want to transform it in a 400×400 jpg?Please help me

    • Dugout,

      you’ll need to change the logic in lines 55-58 to allow for upscaling. You may also want to change the sub sample average action to provide better quality when uprezing the images.

      Otherwise the code should work for what you want.

      • exception

        java.lang.RuntimeException: – Unable to render RenderedOp for this operation.
        at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:827)
        at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
        at javax.media.jai.RenderedOp.getRendering(RenderedOp.java:888)
        at com.styleretail.servlets.ImageDisplay.doGet(ImageDisplay.java:83)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:189)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:91)
        at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:92)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:325)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Thread.java:595)

  22. I got this error, can someone help me to prevent this error please?
    Error: One factory fails for the operation “encode”
    Occurs in: javax.media.jai.ThreadSafeOperationRegistry
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.media.jai.FactoryCache.invoke(Unknown Source)
    at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
    at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
    at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
    at javax.media.jai.RenderedOp.createInstance(Unknown Source)
    at javax.media.jai.RenderedOp.createRendering(Unknown Source)
    at javax.media.jai.RenderedOp.getRendering(Unknown Source)
    at javax.media.jai.JAI.createNS(Unknown Source)
    at javax.media.jai.JAI.create(Unknown Source)
    at javax.media.jai.JAI.create(Unknown Source)
    at images.ImageForm.resizeImageAsJPG(ImageForm.java:103)
    at images.ImageForm.customInit(ImageForm.java:68)
    at images.ImageForm.(ImageForm.java:61)
    at images.Main.main(Main.java:17)
    Caused by: java.lang.IllegalArgumentException: Requested rectangle bounds is not contained in the input raster`s bounds.
    at javax.media.jai.RasterAccessor.(Unknown Source)
    at com.sun.media.jai.opimage.SubsampleAverageOpImage.computeRect(Unknown Source)
    at javax.media.jai.GeometricOpImage.computeTile(Unknown Source)
    at com.sun.media.jai.util.SunTileScheduler.scheduleTile(Unknown Source)
    at javax.media.jai.OpImage.getTile(Unknown Source)
    at javax.media.jai.PlanarImage.getData(Unknown Source)
    at javax.media.jai.PlanarImage.getData(Unknown Source)
    at com.sun.media.jai.codecimpl.JPEGImageEncoder.encode(JPEGImageEncoder.java:174)
    at com.sun.media.jai.opimage.EncodeRIF.create(Unknown Source)
    … 18 more

    • Glen,

      looks like you’re trying to do something using dimensions that are bigger than the image without upscaling?

      “Caused by: java.lang.IllegalArgumentException: Requested rectangle bounds is not contained in the input raster`s bounds.”

  23. Pingback: A journey with resizing and cropping images in Java | Future Medium

  24. I have trouble . Please help me :(
    Error: One factory fails for the operation “encode”
    Occurs in: javax.media.jai.ThreadSafeOperationRegistry
    java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at javax.media.jai.FactoryCache.invoke(Unknown Source)
    at javax.media.jai.OperationRegistry.invokeFactory(Unknown Source)
    at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(Unknown Source)
    at javax.media.jai.registry.RIFRegistry.create(Unknown Source)
    at javax.media.jai.RenderedOp.createInstance(Unknown Source)
    at javax.media.jai.RenderedOp.createRendering(Unknown Source)
    at javax.media.jai.RenderedOp.getRendering(Unknown Source)
    at javax.media.jai.JAI.createNS(Unknown Source)
    at javax.media.jai.JAI.create(Unknown Source)
    at javax.media.jai.JAI.create(Unknown Source)
    at hqbc.util.ImageResize.resizeImageAsJPG(ImageResize.java:61)
    at org.apache.jsp.admin.photo.add_005fresp_jsp._jspService(add_005fresp_jsp.java:141)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    at java.lang.Thread.run(Thread.java:619)
    Caused by: java.lang.IllegalArgumentException: Requested rectangle bounds is not contained in the input raster`s bounds.
    at javax.media.jai.RasterAccessor.(Unknown Source)
    at com.sun.media.jai.opimage.SubsampleAverageOpImage.computeRect(Unknown Source)
    at javax.media.jai.GeometricOpImage.computeTile(Unknown Source)
    at com.sun.media.jai.util.SunTileScheduler.scheduleTile(Unknown Source)
    at javax.media.jai.OpImage.getTile(Unknown Source)
    at javax.media.jai.PlanarImage.getData(Unknown Source)
    at javax.media.jai.PlanarImage.getData(Unknown Source)
    at com.sun.media.jai.codecimpl.PNGImageEncoder.writeIDAT(PNGImageEncoder.java:446)
    at com.sun.media.jai.codecimpl.PNGImageEncoder.encode(PNGImageEncoder.java:1026)
    at com.sun.media.jai.opimage.EncodeRIF.create(Unknown Source)
    … 34 more

    • Are you trying to encode a TIFF? Buggy TIFF files will cause that. Please Google the error message: “Error: One factory fails for the operation “encode” Occurs in: javax.media.jai.ThreadSafeOperationRegistry”

  25. Please help me..

    Get an exception when trying to upload an image:

    Error: One factory fails for the operation “encode”
    Occurs in: javax.media.jai.ThreadSafeOperationRegistry

    Caused by: java.lang.IncompatibleClassChangeError: Found class
    com.sun.image.codec.jpeg.JPEGImageEncoder, but interface was expected
    at
    com.sun.media.jai.codecimpl.JPEGImageEncoder.encode(JPEGImageEncoder.java:215)
    at com.sun.media.jai.opimage.EncodeRIF.create(EncodeRIF.java:79)
    … 47 more

  26. Thanks you devon…

    people who got trouble or having unsuccessful program should pay attention whether you got “JAI” or not ..
    if you have not installed it yet.. you can get it from the following..
    http://download.java.net/media/jai/builds/release/1_1_3/

    and the following is the installation instruction
    http://download.java.net/media/jai/builds/release/1_1_3/INSTALL.html#Windows

    and finally do not forget to import the following as well.

    import com.sun.media.jai.codec.*;
    import javax.media.jai.JAI;
    import javax.media.jai.RenderedOp;
    import javax.media.jai.OpImage;
    import java.awt.image.renderable.ParameterBlock;
    import java.awt.RenderingHints;

    Thanks for everyone again.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>