Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

2 major java image convertion problems

0 views
Skip to first unread message

Pfanzelter Thomas

unread,
Mar 18, 2002, 12:07:13 PM3/18/02
to
hi!

i have 2 major problems both concerning image (.jpg) conversion and handling
in java.

i have a web application (servlets) getting a image file from a html form.
the image data comes in as a array of byte[].

now i have to create a java.awt.image.Image to make my image scaling and so
on. this i do with the following code:

Image image = Toolkit.getDefaultToolkit().createImage(buffer);

using a Toolkit. There i get the first problem when running on linux (tomcat
4.0) which says:

java.lang.InternalError: Can't connect to X11 window server using ':0.0' as
the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at
sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:59)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:120)
at
java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment
.java:58)
at sun.awt.motif.MToolkit.<clinit>(MToolkit.java:57)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:120)
at java.awt.Toolkit$2.run(Toolkit.java:512)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:503)
at gui.entry.field.Image.setImageData(Image.java:72)

is there another way to create a java.awt.image.Image from a byte[] without
using the Toolkit which seems to need a X11 Server running?

the second question is a bit more difficult.

i have the following peace of code to create a BufferedImage from the Image
and to scale it down to the desired size and write it as
ByteArrayOutputStream directly to a webserver via ftp.


private BufferedImage createSizedImage(Image inImage, int maxDim) throws
Exception
{
System.out.println("width: "+(double)inImage.getWidth(null)+", height:
"+(double)inImage.getHeight(null));

// Determine the scale.
double scale = (double)maxDim/(double)inImage.getHeight(null);
if (inImage.getWidth(null) > inImage.getHeight(null)) {
scale = (double)maxDim/(double)inImage.getWidth(null);
}
// Determine size of new image.
//One of them
// should equal maxDim.
int scaledW = (int)(scale*inImage.getWidth(null));
int scaledH = (int)(scale*inImage.getHeight(null));

// Create an image buffer in
//which to paint on.
BufferedImage outImage = new BufferedImage(scaledW,
scaledH,BufferedImage.TYPE_INT_RGB);
// Set the scale.
AffineTransform tx = new AffineTransform();

// If the image is smaller than
//the desired image size,
// don't bother scaling.
if (scale < 1.0d) {
tx.scale(scale, scale);
}

// Paint image.
Graphics2D g2d = outImage.createGraphics();
g2d.drawImage(inImage, tx, null);
g2d.dispose();
return outImage;
}

if i use this methode in my debugger, all works fine. but when not debugging
sometimes the resulting image (after writing to disc or to the webserver) is
just black with de maxDim given as parameter.
after hours of trying i come to the conclusion, that somehow the "time" is
relevant.

the parameter inImage for the above methode is the element of a Vector of
the type Image. and somehow sometimes not all the image-data from the vector
is available to the methode displayed abouve leading that there is no data
to convert, no width or height of the image and so on.
what could this be? i really have no idea.

i now made a way round this problem by just calling the above methode
repeated until i get a image from it. but this could not be the solution

anybody else had such a problem? maybe the problem is the amount of data (an
image has bout 600k) in the vector's element?

please help
regards
Pfanzelter Thomas.


Martin Kersten

unread,
Mar 18, 2002, 2:22:07 PM3/18/02
to
Hi Thomas,

just go to java.sun.com and look for the Image API (slitly diffrent
name). They introduced jpg support and all that other things you will
need too.


Bye

Martin

"Pfanzelter Thomas" <i...@efftrade.at> schrieb im Newsbeitrag
news:3c96...@news.uni-linz.ac.at...

Benisis

unread,
Mar 18, 2002, 3:33:48 PM3/18/02
to
Sujestion for the second problem:
try using a MediaTracker.
code should look like this:

MediaTracker tracker = new MediaTracker(component);
Image img = getImage("img.gif");
tracker.addImage(img, 1);
tracker.waitForAll();
ImageIcon imgicon = new ImageIcon(img); //<---- at this point the imgicon
will definetly be loaded
//----- sample code for getImage()
public Image getImage(URL url)
{
//--- if the file is not an image then this method hangs!
String file = url.getFile();
if(file.endsWith(".gif") || file.endsWith(".jpg") ||
file.endsWith(".jpeg") || file.endsWith(".png"))
{
return new ImageIcon(url).getImage();
}
else
return null;
}

"Pfanzelter Thomas" <i...@efftrade.at> wrote in message
news:3c96...@news.uni-linz.ac.at...

Marshall Spight

unread,
Mar 18, 2002, 11:18:49 PM3/18/02
to
"Martin Kersten" <martin....@student.uni-magdeburg.de> wrote in message news:a75ep8$lnd$1...@graf.cs.uni-magdeburg.de...

> Hi Thomas,
>
> just go to java.sun.com and look for the Image API (slitly diffrent
> name). They introduced jpg support and all that other things you will
> need too.

javax.imageio

Look particularly at the method javax.imageio.ImageIO.read() - one stop shopping.

Available in finer v 1.4 jdks everywhere.


Marshall

Georg Rehfeld

unread,
Mar 18, 2002, 11:27:30 PM3/18/02
to
Hi group,

"Pfanzelter Thomas" <i...@efftrade.at> wrote in message news:3c96...@news.uni-linz.ac.at...

> java.lang.InternalError: Can't connect to X11 window server using ':0.0' as


> the value of the DISPLAY variable.

This is a known bug in Java. See SUN's 'bug parade' somewhere
(too lazy to look it up just now). Essence is: currently you
need an X server running on your system, even when you only
plan to do all in memory and never attempt to render on the server
side. There is even (at least one) 'dummy' X-server available
(from third party, but free) to work around this problem.

regards
Georg
___ ___
| + | |__ Georg Rehfeld Woltmanstr. 12 20097 Hamburg
|_|_\ |___ georg....@gmx.de +49 (40) 23 53 27 10

Thomas Weidenfeller

unread,
Mar 19, 2002, 3:24:50 AM3/19/02
to
"Pfanzelter Thomas" <i...@efftrade.at> writes:
> java.lang.InternalError: Can't connect to X11 window server using ':0.0' as
> the value of the DISPLAY variable.
> at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
> at
> sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:59)
> at java.lang.Class.forName0(Native Method)
> at java.lang.Class.forName(Class.java:120)
> at
> java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment
> .java:58)
> at sun.awt.motif.MToolkit.<clinit>(MToolkit.java:57)
> at java.lang.Class.forName0(Native Method)
> at java.lang.Class.forName(Class.java:120)
> at java.awt.Toolkit$2.run(Toolkit.java:512)
> at java.security.AccessController.doPrivileged(Native Method)
> at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:503)
> at gui.entry.field.Image.setImageData(Image.java:72)

Known "feature". Fixed in Java 1.4. For workarounds for earlier versions
search the Sun web site and bug parade for the phrase "headless java".

/Thomas

Dipl. Ing. Paul Szawlowski

unread,
Mar 20, 2002, 2:50:24 AM3/20/02
to
Hi from .at !


> i have a web application (servlets) getting a image file from a html form.
> the image data comes in as a array of byte[].
>
> now i have to create a java.awt.image.Image to make my image scaling and so
> on. this i do with the following code:
>
> Image image = Toolkit.getDefaultToolkit().createImage(buffer);
>

I suppose you know the layout of your image data. Then you could build a
BufferedImage from your data using ColorModel, SampleModel, DataBuffer,
WritableRaster and inherited classes. The ColorModel specifies how many bands
are used, etc ...; the SampleModel defines the layout of the data (SampleModel
and ColorModel must be compatible ! - number of bands, etc...), DataBuffer holds
the data and WritableRaster wraps SampleModel and DataBuffer.

regards Paul

0 new messages