hai guys... can someone solve my problems?
i'm doing a thumbnail program. But, i have some problems.
1. The thumbnail image become black (cannot see anything)
2. Can anyone know how to get the size (height and width) of the original image?
this is my code:
Image image = Toolkit.getDefa ultToolkit().ge tImage(namePath );
MediaTracker mediaTracker = new MediaTracker(ne w Container());
mediaTracker.ad dImage(image, 0);
mediaTracker.wa itForID(0);
int thumbWidth = 600; // how to get the original size???
int thumbHeight = 800;// how to get the original size???
// determine thumbnail size from WIDTH and HEIGHT
double thumbRatio = (double)thumbWi dth / (double)thumbHe ight;
int imageWidth = image.getWidth( null);
int imageHeight = image.getHeight (null);
double imageRatio = (double)imageWi dth / (double)imageHe ight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidt h / imageRatio);
} else {
thumbWidth = (int)(thumbHeig ht * imageRatio);
}
BufferedImage thumbImage = new BufferedImage(t humbWidth, thumbHeight, BufferedImage.T YPE_INT_RGB);
Graphics2D graphics2D = thumbImage.crea teGraphics();
graphics2D.setR enderingHint(Re nderingHints.KE Y_INTERPOLATION , RenderingHints. VALUE_INTERPOLA TION_BILINEAR);
graphics2D.draw Image(image, 0, 0, thumbWidth, thumbHeight, null);
BufferedOutputS tream out = new BufferedOutputS tream(new FileOutputStrea m(file1));
JPEGImageEncode r encoder = JPEGCodec.creat eJPEGEncoder(ou t);
JPEGEncodeParam param = encoder.getDefa ultJPEGEncodePa ram(thumbImage) ;
int quality = 100;
quality = Math.max(0, Math.min(qualit y, 100));
param.setQualit y((float)qualit y / 100.0f, false);
encoder.setJPEG EncodeParam(par am);
encoder.encode( thumbImage);
out.close();
thank you
i'm doing a thumbnail program. But, i have some problems.
1. The thumbnail image become black (cannot see anything)
2. Can anyone know how to get the size (height and width) of the original image?
this is my code:
Image image = Toolkit.getDefa ultToolkit().ge tImage(namePath );
MediaTracker mediaTracker = new MediaTracker(ne w Container());
mediaTracker.ad dImage(image, 0);
mediaTracker.wa itForID(0);
int thumbWidth = 600; // how to get the original size???
int thumbHeight = 800;// how to get the original size???
// determine thumbnail size from WIDTH and HEIGHT
double thumbRatio = (double)thumbWi dth / (double)thumbHe ight;
int imageWidth = image.getWidth( null);
int imageHeight = image.getHeight (null);
double imageRatio = (double)imageWi dth / (double)imageHe ight;
if (thumbRatio < imageRatio) {
thumbHeight = (int)(thumbWidt h / imageRatio);
} else {
thumbWidth = (int)(thumbHeig ht * imageRatio);
}
BufferedImage thumbImage = new BufferedImage(t humbWidth, thumbHeight, BufferedImage.T YPE_INT_RGB);
Graphics2D graphics2D = thumbImage.crea teGraphics();
graphics2D.setR enderingHint(Re nderingHints.KE Y_INTERPOLATION , RenderingHints. VALUE_INTERPOLA TION_BILINEAR);
graphics2D.draw Image(image, 0, 0, thumbWidth, thumbHeight, null);
BufferedOutputS tream out = new BufferedOutputS tream(new FileOutputStrea m(file1));
JPEGImageEncode r encoder = JPEGCodec.creat eJPEGEncoder(ou t);
JPEGEncodeParam param = encoder.getDefa ultJPEGEncodePa ram(thumbImage) ;
int quality = 100;
quality = Math.max(0, Math.min(qualit y, 100));
param.setQualit y((float)qualit y / 100.0f, false);
encoder.setJPEG EncodeParam(par am);
encoder.encode( thumbImage);
out.close();
thank you