• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with JAI

 
Diego Garcia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an image in a directory in the web server, now i need to resize that image. I am trying to use JAI, but i keep getting an exception....hope someone can help me:

import java.io.*;
import java.util.*;
import java.awt.image.renderable.*;
import javax.media.jai.*;
import com.sun.media.jai.codec.*;
import javax.media.jai.JAI;

public class Resize
{
private RenderedOp image = null;
private RenderedOp result = null;
private int height = 0;
private int width = 0;


public void load(String file) throws IOException
{
FileSeekableStream fss = new FileSeekableStream(file);
image = JAI.create("stream", fss);
height = image.getHeight();
width = image.getWidth();
}

public void writeResult(String file, String type) throws IOException
{
FileOutputStream os = new FileOutputStream(file);
JAI.create("encode", result, os, type);
}

public void thumbnail(float edgeLength)
{
boolean tall = (height > width);
float modifier = edgeLength / (float) (tall ? height : width);

ParameterBlock params = new ParameterBlock();
params.addSource(image);

params.add(modifier);//x scale factor
params.add(modifier);//y scale factor
params.add(0.0F);//x translate
params.add(0.0F);//y translate
params.add(new InterpolationNearest());//interpolation method

result = JAI.create("scale", params);
}
}

i call the methods, this way:
Resize min = new Resize ();
String destinoFile = "C:/bea/user_projects/mydomain/applications/..../"+file;//file is the name of the image
float edgeLength = 10;
min.load(fileName);
min.thumbnail(edgeLength);
min.writeResult(destinoFile, "jpg");


and this is the exception:

java.lang.IllegalArgumentException: Encode The specified format has no associated registered ImageCodec.
at javax.media.jai.JAI.createNS(JAI.java:1091)
at javax.media.jai.JAI.create(JAI.java:977)
at javax.media.jai.JAI.create(JAI.java:1625)
at factoriaweb.brandspain.miniatura.Resize.writeResult(Miniatura.java:33)
at factoriaweb.brandspain.upload.UploadServlet.doPost(UploadServlet.java:81)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:1058)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:401)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:306)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:5445)
at weblogic.security.service.SecurityServiceManager.runAs(SecurityServiceManager.java:780)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3105)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2588)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:213)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:189)

Any idea? i am totally lost....thx in advance
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi [unpronounceable],

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It reqiures that you use a full, real (sounding) first and last name for your display name. Initials aren't enough. You can change your display name here.

Secondly, we've actually got a specialized into which this question will fit better: our "Other Java APIs" forum. I'm going to move this question there where it will stand a better chance of being answered.
 
Diego Garcia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, name changed...sorry about that
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you -- but...

Originally posted by Ernest Friedman-Hill:
Initials aren't enough.


Don't let the moose fool you -- we try to run a real professional outfit around here.

Anyways, a a quick Google of the error message pointed me to this, which suggests the problem is that the encoder type has to be written as "JPEG" whereas you've used "jpg". Might give that a try.
 
Diego Garcia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello again, i tried what you told me and now it works, but something weird is happening.
Its suppossed that if i pass the type as JPEG, the new image should have that format, right? well, i have a gif image and the new generated image still has gif format...

Also its suppossed that JAI doesnt support GIF to rewrite images.

Any idea?

Thanks for the help
 
Diego Garcia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved, i did something wrong.
Thanks for all the help.
 
Diego Garcia
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,

there is something wrong with that code... when i resize the image and place it in a directory in the web server, i cant open it with a photo editor until i shut down the webserver.

Anyone knows why?

Thx
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!