简体   繁体   中英

How to set DPI information in a grayscale image?

First of all, hello and thanks for the opportunity.

I have a solution that scans (via native twain source) an image (or many images) and saves them into a folder in the file system.

My question is: I made some tests, and I always got an 96 image DPI (I was using ImageIO.write to save the images that came from the twain API in a BufferedImage object). Than, I saw the answer of Peter Kofler in stackoverflow at this link ( How to set DPI information in an image? ) and it works in Colored and Black & White situations (set up from TwainCapability object).

But, for my scanned grayscale images, the DPI doesn't change anyway !!!

I'm verifying it using MS Paint -> Properties. It's always 96 DPI when I scan a grayscale image.

Any idea how I can set the DPI in this case??

I'm setting the DPI, like the Peter Kofler example as this:

resolutionState = 100;
//or resolutionState = 200;
//or resolutionState = 300;
double dotsPerMilli = resolutionState / 10 / 2.54;

thanks one more time in advance.

I found an answer and one way to do this !!!

In the following link: How to change the DPI from 96 to 300 of an image in java after resizing? the "user3603284"posted an solution that helped me doing this.

I changed from png to jpeg (it does not matter for the project specifications) and then, worked like a charm !!!

The code:

File imageFile = new File("C:/ScannerOutput/scannerImage" + System.currentTimeMillis() +".jpeg");
FileOutputStream fos = new FileOutputStream(imageFile);
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image);
jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
jpegEncoder.setJPEGEncodeParam(jpegEncodeParam);
jpegEncodeParam.setQuality(0.75f, false);
jpegEncodeParam.setXDensity(resolutionState); //DPI rate 100, 200 or 300
jpegEncodeParam.setYDensity(resolutionState); //DPI rate 100, 200 or 300
jpegEncoder.encode(image, jpegEncodeParam);
image.flush();
fos.close();

Thanks very much SO, always helping me =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM