Доброго дня!Стала задача сделать ресайзер.
Требования: картинка по соотношению вес/качество должна примерно соответствовать результатам оптимизации графических редакторов.
Тестировал JAI и ImageIO+com.sun.media.encoder.jpeg.JPEGEncoder.
Делал ресайз с ограничением по размерам: 100х100.
В результате:
небольшие картинки обрабатываются с небольшой потерей качества,
большие вообще никак не смотрятся по сравнению с обработанными в редакторах
Есть соображения, как реализовать поставленную задачу в режиме хостинга? (т.е. не использовать сторонних native библиотек типа JAI, ImageMagick).
Код:
[SRC java]public class JAIUtils {
public static void resize(InputStream is, String savePath, int maxSize)
throws IOException {
RenderedOp image = JAI.create("stream", SeekableStream.wrapInputStream(
is, true));
if (image == null) {
return;
}
float wScale = (float) maxSize / (float) image.getWidth();
float hScale = (float) maxSize / (float) image.getHeight();
// If we want to preserve the aspect ratio, pick the smaller
// scale.
float scale = Math.min(wScale, hScale);
// Don't make the image larger than it already is.
if (scale > 1.0) {
scale = 1.0F;
}
wScale = scale;
hScale = scale;
ParameterBlock pb = new ParameterBlock();
pb.addSource(image); // The source image
pb.add(wScale); // The xScale
pb.add(hScale); // The yScale
pb.add(0.0F); // The x translation
pb.add(0.0F); // The y translation
pb.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR)); // The interpolation
// Create the scaled image.
PlanarImage resultImage = JAI.create("scale", pb, null);
// Write out the image as a PNG.
ParameterBlock pb1 = new ParameterBlock();
pb1.addSource(resultImage);
pb1.add(savePath);
pb1.add("JPEG");
pb1.add(null);
pb1.add(new Boolean(false));
JAI.create("filestore", pb1);
}[/SRC]
[SRC java]
public class ImageIOUtil
{
public static InputStream scaleImage(InputStream p_image, int p_width, int p_height) throws Exception
{
InputStream imageStream = new BufferedInputStream(p_image);
Image image = (Image) ImageIO.read(imageStream);
int thumbWidth = p_width;
int thumbHeight = p_height;
// Make sure the aspect ratio is maintained, so the image is not
// skewed
double thumbRatio = (double) thumbWidth / (double) thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double) imageWidth / (double) imageHeight;
if (thumbRatio < imageRatio)
{
thumbHeight = (int) (thumbWidth / imageRatio);
}
else
{
thumbWidth = (int) (thumbHeight * imageRatio);
}
// Draw the scaled image
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbImage);
param.setQuality(JPEG_QUALITY_DEFAULT, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(thumbImage);
ImageIO.write(thumbImage, "JPEG", out);
ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray());
return bis;
}
}
[/SRC]