Thumbnail creation problem. really weird.

H

hilz

Hi all,

After doing a search on how to create thumbnails from jpg's, i found the
code below.
If i run this code from a standalone java program, it works fine and it
creates the thumbnail,
but if i run it from inside a jsp page, it creates a black thumbnail with
nothing in it!!!!
does anyone know what could be the problem? it is driving me crazy.

thanks
hilz




//usage: java Thumbnail <file.jpg> <newfile.jpg> <MAXWIDTH>
import java.awt.Image;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io_OutputStream;
import java.io.FileOutputStream;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
class ThumbnailCreator {
public static void main(String[] args) {
createThumbnail(args[0], args[1], Integer.parseInt(args[2]));
}
/**
* Reads an image in a file and creates
* a thumbnail in another file.
* @param orig The name of image file.
* @param thumb The name of thumbnail file.
* Will be created if necessary.
* @param maxDim The width and height of
* the thumbnail must
* be maxDim pixels or less.
*/
public static void createThumbnail( String orig, String thumb, int
maxDim) {
try {
// Get the image from a file.
java.awt.Image inImage = new
javax.swing.ImageIcon(orig).getImage();
// 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.
java.awt.image.BufferedImage outImage = new
java.awt.image.BufferedImage(scaledW,
scaledH,java.awt.image.BufferedImage.TYPE_INT_RGB);
// Set the scale.
java.awt.geom.AffineTransform tx = new
java.awt.geom.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.
java.awt.Graphics2D g2d = outImage.createGraphics();
g2d.drawImage(inImage, tx, null);
g2d.dispose();
// JPEG-encode the image
//and write to file.
java.io_OutputStream os = new java.io.FileOutputStream(thumb);
com.sun.image.codec.jpeg.JPEGImageEncoder encoder =
com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(os);
encoder.encode(outImage);
os.close();
}
catch (java.io.IOException e) {
e.printStackTrace();
}
System.exit(0);
}
}
 
A

ak

After doing a search on how to create thumbnails from jpg's, i found the
code below.
If i run this code from a standalone java program, it works fine and it
creates the thumbnail,
but if i run it from inside a jsp page, it creates a black thumbnail with
nothing in it!!!!
does anyone know what could be the problem? it is driving me crazy.
try image.getScaledInstance()
 
H

hilz

try image.getScaledInstance()

this gives me a java.awt.Image object
then how do i write it to a JPG file?

thanks
hilz
 
A

ak

try image.getScaledInstance()
this gives me a java.awt.Image object
then how do i write it to a JPG file?
use PixelGrabber to get pixel array
create appropriate DataBuffer and SampleModel,
than create Raster which you can pass to PEGImageEncoder.
 
H

hilz

Andrei:
Than you for your help.
this is too complicated for me since i have not used this part of java
before.
i've been trying to do what you suggested, but i don't seem to succeed.
would you be willing to share some code that shows how to do this?

thanks
hilz
 
A

ak

Than you for your help.
this is too complicated for me since i have not used this part of java
before.
i've been trying to do what you suggested, but i don't seem to succeed.
would you be willing to share some code that shows how to do this?

may be it is better to create empty BufferedImage, get the pixels from Image
using PixelGrabber
and then set pixels on BufferedImage using setRGB();

//alredy scaled image
Image image;

//create BufferedImage without alpha
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);

int[] pixels = new int[width * height];
PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixels, 0,
width);
try {
pg.grabPixels();
} catch (InterruptedException e) {
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
throw new RuntimeException("can't fetch pixels");
}

bi.setRGB(0, 0, width, height, pixels, 0, width);
 
C

Cid

Andrei:
Than you for your help.
this is too complicated for me since i have not used this part of java
before.
i've been trying to do what you suggested, but i don't seem to succeed.
would you be willing to share some code that shows how to do this?

thanks
hilz

The javax.imageio.ImageIO class might help you out. It allows you to
get image readers/writers by suffix (ie, "jpg").
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top