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

resizing an image

13 views
Skip to first unread message

Pfanzelter Thomas

unread,
Mar 20, 2002, 7:38:38 PM3/20/02
to
hello ppl!

i want to resize an image (available as a BufferedImage) to a given size.

i tried the following code, which worked fine on my windows system, but is
not workin on linux cause of the "known"
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:126)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:130)

ERROR.

so my question:
is there another way to resize a BufferedImage and get a byteArray of the
resized image (to upload it to a webserver via ftp) than using a Graphics2D
(see following code i tried)?

Graphics2D g2d = outImage.createGraphics();
g2d.drawImage(inImage, tx, null);
g2d.dispose();

i tried to use
Image test =
inImage.getScaledInstance(scaledW,scaledH,Image.SCALE_DEFAULT);
but the problem is that as result i get a java.awt.Image and i don't know
how to get a BuferedImage from the Image to use the following code

byteOS = new ByteArrayOutputStream();
encoder = JPEGCodec.createJPEGEncoder(byteOS);
encoder.encode(largeImg);
b = byteOS.toByteArray();
server.upload(img_path, (i+1)+".jpg", b);

to code the image as a jpg and get a byteArray to upload it to the server

thanks for help!
Thomas P.


Marshall Spight

unread,
Mar 21, 2002, 2:19:48 AM3/21/02
to
"Pfanzelter Thomas" <i...@efftrade.at> wrote in message news:3c99...@news.uni-linz.ac.at...

>
> i want to resize an image (available as a BufferedImage) to a given size.

Perhaps you will find this code useful:


public static void scaleStream(File in, File out, double scale)
throws IOException
{
// can't use standard cache dir in servlet context.
ImageIO.setUseCache(false);
BufferedImage image = ImageIO.read(in);
AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImage scaledImage = op.filter(image, null);
ImageIO.write(scaledImage, "jpg", out);
}

Marshall

Pfanzelter Thomas

unread,
Mar 20, 2002, 8:20:03 PM3/20/02
to
hi there
for those having the same problem as me, i now got a solution looking like
the following:

AffineTransformOp op = new
AffineTransformOp(AffineTransform.getScaleInstance(scale, scale), null);
outImage = op.filter(inImage, null);

where outImage and inImage are BufferImage.


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

0 new messages