1
for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        int intPixel = image.getRGB(i, j);            

        image.setRGB(i, j, intPixel);
      }      
    }
ImageIO.write(image, "JPG", new File("img/newfile01.jpg"));

This code make a 96dpi image but the source image was 72 dpi! why

Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75
ehsun7b
  • 4,796
  • 14
  • 59
  • 98

3 Answers3

1
BufferedImage image = ImageIO.read(inputFile);

      if (image.getColorModel().getColorSpace().getType() == ColorSpace.TYPE_GRAY) {
        System.out.println("is grayscale");
      }

      // create jpegEncode for output image
      JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(outputFile));




      // create jpeg encoder from getting defaul value from input buffered
      // image
      JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image);
      // setting up density unit paramter
      jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
      // setting up jpeg encode parameter
      jpegEncoder.setJPEGEncodeParam(jpegEncodeParam);
      // set quality parameter
      jpegEncodeParam.setQuality(0.75f, false);
      // set X-resolution
      jpegEncodeParam.setXDensity(X_DPI);
      // set Y-resolution
      jpegEncodeParam.setYDensity(Y_DPI);
      // encode output image
      jpegEncoder.encode(image, jpegEncodeParam);
      // flush the buffer image
      image.flush();
ehsun7b
  • 4,796
  • 14
  • 59
  • 98
0

it seems there is a default value. Does image.setDpi(72) work?

Reporter
  • 3,897
  • 5
  • 33
  • 47
  • There is no setDpi method in BufferedImage. What class has it? – ehsun7b Apr 20 '11 at 09:24
  • I have to apologize. I posted a customized java class. But I found a n article about your topic. http://www.coderanch.com/t/380658/java/java/Image-Resolution. And as far as I can see you use also the ImageIO class – Reporter Apr 20 '11 at 09:38
  • # 2: Based on the article above, the class javax.imageio.metadata.IIOMetadata contains the methods 'getAsTree()' and 'setFromTree()' – Reporter Apr 20 '11 at 09:48
0

Take a look at the answer here previously asked for PNG images

ImageIO allows you to set image metadata for some output formats, but it is not a trivial task, and may not be supported for JPEG images.

Community
  • 1
  • 1
AndyT
  • 1,413
  • 9
  • 11