Image size reduction
I'm storing the image in SQL database field as Image. From this image, I'm reducing its size to fit into visitors pass. But the clarity is lost. Here is the code that I' using
public static BufferedImage resizeAWT(BufferedImage src, double factorX, double factorY) {
int w = src.getWidth();
int h = src.getHeight();
int newW = (int)(Math.floor(factorX * w));
int newH = (int)(Math.floor(factorY * h));
Image temp = src.getScaledInstance(newW, newH, Image.SCALE_AREA_AVERAGING);
BufferedImage tgt = createBlankImage(src, newW, newH);
Graphics2D g = tgt.createGraphics();
g.drawImage(temp, 0, 0, null);
g.dispose();
return tgt;
}
//Uses image to create another image of the same "type", but of a factored size
public static BufferedImage createBlankImage(BufferedImage src, int w, int h) {
int type = src.getType();
if (type != BufferedImage.TYPE_CUSTOM)
return new BufferedImage(w, h, type);
else {
ColorModel cm = src.getColorModel();
WritableRaster raster = src.getRaster().createCompatibleWritableRaster(w, h);
boolean isRasterPremultiplied = src.isAlphaPremultiplied();
return new BufferedImage(cm, raster, isRasterPremultiplied, null);
}
}
.....
BufferedImage image3 = resizeAWT(bi1, 0.25, 0.25);
Is there any better method of reducing the size so that clarity is not lost?
|