Need to convert WImage to byte array
I am pulling an image out of the database and streaming it into a WImage ...then changing it's size and then streaming it out into a web ap. However, I am unable to find a way to throw the WImage into a byte array... What I have below was my (failed) attempt. Due to the fact that when I try to call .getBufferedImage() it returns null to my variable bi. So then I get a NullPointerException error...Any ideas? Alternative Solutions?
WImage wi = (WImage)Toolkit.getDefaultToolkit().createImage(image.getImage());
BufferedImage bi = wi.getBufferedImage();
bi.getScaledInstance(50 , 20, java.awt.Image.SCALE_SMOOTH);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(bi);
Thanks!
[917 byte] By [
davemka] at [2007-10-2 21:05:38]

Why not use ImageIO libraries so you don't have to muck around with WImage and the other com.sun classes?
BufferedImage bi = ImageIO.read(fileOrUrl);
bi.getScaledInstance(50 , 20, java.awt.Image.SCALE_SMOOTH);
ImageWriter writer = ImageIO.getImageWritersBySuffx("jpg").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(1f);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ImageOutputStream imageOut = new MemoryCacheImageOutputStream(bytes);
imageWriter.setOutput(imageOut);
imageWriter.write(null,new IIOImage(bi,null,null),param);
imageOut.close();
byte[] theBytes = baos.toByteArray()
Warning: rapidly typed, may contain errors.
Great thanks - this was a step in the right direction!
Alright well this did get it populated...but I am still having trouble.
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(image.getImage()));
bi = (BufferedImage) bi.getScaledInstance(10, 10, java.awt.Image.SCALE_SMOOTH);
ImageWriter writer = (ImageWriter)ImageIO.getImageWritersBySuffix("jpg").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(1f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream imageOut = new MemoryCacheImageOutputStream(baos);
writer.setOutput(imageOut);
writer.write(null,new IIOImage((RenderedImage)bi,null,null),param);
imageOut.close();
response.getOutputStream().write(baos.toByteArray());
What is happening now ties back to a problem that got me started on this long painful route in the first place.
bi = (BufferedImage) bi.getScaledInstance(10, 10, java.awt.Image.SCALE_SMOOTH);
This line. When I try to cast the Image type into a BufferedImage I get the following error:
Error 500: sun/awt/windows/WImage incompatible with java/awt/image/BufferedImage
I dont understand why it is a "WImage" ...I dont have any calls at all to the WImage in my entire project.
Sorry. should have also mentioned that the getScaledInstance method is kinda hokey since it returns an Image (not guarunteed to be a BufferedImage as you've found out). Use the following:
BufferedImage orig = ...
BufferedImage scaled = new BufferedImage(10,10,orig.getType());
Graphics2D g2 = scaled.createGraphics();
g2.drawRenderedImage(orig,AffineTransform.getScaleInstance(orig.getWidth() / 10., orig.getHeight() / 10.));
And we have lift off! After two hours wasted on somthing that probably should of taken 2 minutes. It is working like a dream!Thanks for the help!