You lose the dpi of the image when you lower the resolution

3

I have the following code. What it does is take an image and lower the resolution because I need it to weigh less. but the problem is that you lose the original ppp or dpi of 200 to 1 and must keep the same when downloading resolution

public void resize(InputStream input, OutputStream output, int width, int height) throws IOException {
    BufferedImage src = ImageIO.read(input);
    BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = dest.createGraphics();

    AffineTransform at = AffineTransform.getScaleInstance((double)width / src.getWidth(), (double)height / src.getHeight());
    g.drawRenderedImage(src, at);
    ImageIO.write(dest, "tif", output);
    output.close();
}

How could I do this?

Compressed image:

Image before downloading dimensions:

    
asked by usernovell 14.08.2016 в 22:11
source

2 answers

1

Resolution is the number of pixels contained in an image. I give you an example, if we have an image of 1080x800 pixels we would have a resolution of 864,000 pixels, if you make a transformation with smaller size you would have a lower resolution.

I recommend using another method to reduce the weight of the image but without losing pixels.

How to resize an image without losing quality.

public static BufferedImage resize(BufferedImage bufferedImage, int newW, int newH) {
    int w = bufferedImage.getWidth();
    int h = bufferedImage.getHeight();
    BufferedImage imagenRedimensionada = new BufferedImage(newW, newH, bufferedImage.getType());
    Graphics2D g = imagenRedimensionada.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.drawImage(bufferedImage, 0, 0, newW, newH, 0, 0, w, h, null);
    g.dispose();
    return imagenRedimensionada;
}
    
answered by 15.08.2016 в 04:52
1

If you mean to maintain the resolution "physical" of the pixels (DPI), that is considered part of the "metadata" of the images, and the support given by the Java ImageIO API for those things is limited. And even more in the case of the TIF format, which is less supported than the most popular ones (JPEG and PNG). I recommend you go to PNG if you can, and I'll give you some links (in English) 1 2 3

    
answered by 15.08.2016 в 05:15