1

I have a JPEG image, after resizing it, I want to change the DPI from 96 to 300 of same image in Java. My code:

public BufferedImage resizeImageWithHint(BufferedImage originalImage, int type) {
    BufferedImage resizedImage = new BufferedImage(177, 177, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, 177, 177, null);
    g.dispose();
    g.setComposite(AlphaComposite.Src);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);    //g.translate(4, 4);     return resizedImage;         }
Arman H
  • 5,488
  • 10
  • 51
  • 76
user3603284
  • 43
  • 1
  • 13
  • 2
    Try and understand the the size of the image and the DPI are linked, as you change one, so the other is effected. – MadProgrammer May 05 '14 at 07:04
  • You can't change the DPI of an image. You'll have to enlarge the image to 300 / 96 pixels. – Gilbert Le Blanc May 05 '14 at 07:06
  • @Gilbert Le Blanc Thanks for your comment, but i want to convert 200*200 px image into 177*177 px with 300 dpi. how it is possible in java? – user3603284 May 05 '14 at 07:09
  • this might help https://code.google.com/p/jj2000/ or http://rsbweb.nih.gov/ij/ if this(http://stackoverflow.com/questions/9417356/bufferedimage-resize) does not work or sufficient – bhathiya-perera May 05 '14 at 07:14
  • 3
    so your 200x200 image hast 96 dpi and your 177x177 image has 300 dpi? where do you want to take the information from for the additional pixels that you want to generate? interpolation? – PKlumpp May 05 '14 at 07:15
  • according to this (https://community.oracle.com/thread/1265028?tstart=0) java cannot use built in APIs to change the DPI so I suggest using the library I've linked in my previous comment – bhathiya-perera May 05 '14 at 07:17
  • 2
    @BhathiyaPerera No library or API can magically add resolution that isn't there in the original image. You've already thrown away some resolution by resizing it. It's not going to come back. – user207421 May 05 '14 at 08:02
  • A 200 x 200 pixel image at 96 pixels per inch is 2.08 inches by 2.08 inches. A 177 x 177 pixel image at 300 pixels per inch is 0.59 inches by 0.59 inches. Done. – Gilbert Le Blanc May 05 '14 at 08:23
  • i did it finally with the use of this code :- BufferedImage image = ImageIO.read(new File(path)); JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(new File(path))); JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image); jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH); jpegEncoder.setJPEGEncodeParam(jpegEncodeParam); jpegEncodeParam.setQuality(0.75f, false); jpegEncodeParam.setXDensity(300); jpegEncodeParam.setYDensity(300); jpegEncoder.encode(image, jpegEncodeParam); image.flush(); – user3603284 May 05 '14 at 08:52
  • @EJB thank you for the clarification, and user* post that as an answer and accept it – bhathiya-perera May 05 '14 at 14:30

1 Answers1

2
BufferedImage image = ImageIO.read(new File(path));
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(new File(path)));
JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image);
jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
jpegEncoder.setJPEGEncodeParam(jpegEncodeParam); jpegEncodeParam.setQuality(0.75f, false);
jpegEncodeParam.setXDensity(300); jpegEncodeParam.setYDensity(300);
jpegEncoder.encode(image, jpegEncodeParam);
image.flush();
Pang
  • 9,564
  • 146
  • 81
  • 122
user3603284
  • 43
  • 1
  • 13
  • It looks like this is from the `com.sun.image.codec.jpeg` package - are these public API? – Dan Gravell Jan 08 '18 at 07:35
  • Yes but this is an old API of SUN JDK but most of JVM's are not supported this because of Oracle. so please try to remove it from your project. – user3603284 Mar 21 '18 at 09:25