Home › Forums › TWAIN Classic › How to set the resolution (DPI)?
- This topic has 10 replies, 5 voices, and was last updated 14 years, 2 months ago by mmotovsk.
- AuthorPosts
Hi,
I am using the following code with JTwain to try to set the DPI to 499 x 499 but the resulting scanned image has 96 x 96 DPI. How can I set the resolution?
I am using a HP ScanJet G3010.Thanks.
source.open();
if (source.isUIEnabled()) {
source.setUIEnabled(false);
}
source.setIndicators(false);
source.setRegion(0,0,source.getCurrentPhyscialWidth(),source.getPhysicalHeight());
source.setXResolution(499);
source.setYResolution(499);
source.setBitDepth(8);
source.setPixelType(JTwainConstants.TWPT_GRAY);
source.acquireImage();
source.saveLastAcquiredImageIntoFile("c:\temp\scan.JPG");are you sure the resolution is supported by the scanner?
do a dat_capability msg_get for the resolution before you scan and make sure you really set the resolution or at lest check the answer from the source when you set the resolution to make sure your command was correctly executed.
@TheCrow99 wrote:
are you sure the resolution is supported by the scanner?
I am not sure it supports 499 x 499 but I scanned an image with resolution 200 x 200 using the scanner alone and it works. When I try to scan the image using JTwain with 200x 200 dpi settings I get a 96 x 96 dpi image.
@TheCrow99 wrote:
do a dat_capability msg_get for the resolution before you scan and make sure you really set the resolution or at lest check the answer from the source when you set the resolution to make sure your command was correctly executed.
Could you please post an example of code of how to use dat_capability and msg_get from Java?
How do I check the answer from the source?
Thanks.
When you’re setting resolution you should check what the units (ICAP_UNITS) are set to first – 499 x 499 pixels isn’t quite the same as 499×499 cm. See the capability ordering whitepaper for information on the order to set capabilities – http://twain.org/docs/CapOrderForWeb.PDF
You also may want to check the supported resolution(s) with a Get before trying the Set, some devices will round to the nearest supported and others with simply disregard your setting – recheck executeing the Get op for the resolution after you execute the set should assure you that you have successfully set the capability.
When you set the resolution are you getting an rc=success from the triplet?
.
@gabe wrote:
When you’re setting resolution you should check what the units (ICAP_UNITS) are set to first – 499 x 499 pixels isn’t quite the same as 499×499 cm. See the capability ordering whitepaper for information on the order to set capabilities – http://twain.org/docs/CapOrderForWeb.PDF
You also may want to check the supported resolution(s) with a Get before trying the Set, some devices will round to the nearest supported and others with simply disregard your setting – recheck executeing the Get op for the resolution after you execute the set should assure you that you have successfully set the capability.
When you set the resolution are you getting an rc=success from the triplet?
.
I am using JTwain (Java) so I don’t know what “rc” is. Is it from the .NET implementation? Here is the code I am executing and the output. I tried setting the units before the resolution but it still doesn’t work.
Source source = SourceManager.instance().selectSourceByName("HP Scanjet G3010 TWAIN");
source.open();
if (source.isUIEnabled()) {
source.setUIEnabled(false);
}
source.setIndicators(false);
System.out.println("============= BEFORE SETTING =============");
System.out.println("bit depth: " + source.getCurrentBitDepth());
System.out.println("units: " + source.getCurrentUnits()[0]);
System.out.println("pixel type: " + source.getCurrentPixelType());
System.out.println("x res: " + source.getCurrentXResolution());
System.out.println("y res: " + source.getCurrentYResolution());
source.setMinimumExceptionEnabled(false);
source.setRegion(0,0,source.getCurrentPhyscialWidth(),source.getPhysicalHeight());
source.setUnits(JTwainConstants.TWUN_PIXELS);
source.setPixelType(JTwainConstants.TWPT_GRAY);
source.setXResolution(300.0);
source.setYResolution(300.0);
source.setBitDepth(8);
System.out.println("n" + "============= AFTER SETTING =============");
System.out.println("bit depth: " + source.getCurrentBitDepth());
System.out.println("units: " + source.getCurrentUnits()[0]);
System.out.println("pixel type: " + source.getCurrentPixelType());
System.out.println("x res: " + source.getCurrentXResolution());
System.out.println("y res: " + source.getCurrentYResolution());
source.acquireImage();
source.saveLastAcquiredImageIntoFile("c:\temp\scan.JPG");
============= BEFORE SETTING =============
bit depth: 8
units: 0
pixel type: 2
x res: 200.0
y res: 200.0
============= AFTER SETTING =============
bit depth: 8
units: 5
pixel type: 1
x res: 1.0
y res: 1.0
It looks like the scanner ignores the values and sets x res and y res to 1.0??!!
But the scanned image file properties show that it has 96 dpi.rc is return code and it is from the twain spec. If you don’t think that the java wrapper you’re using makes the rc available to you recheck the docs for the library, it could be that the library’s author decieded not to expose it but it seems unlikely to me. While you’re looking for information on an rc look for information on the cc, condition code for the calls to twain as there are situation when dealing with twain that you need to know why a call failed. From the code you submitted I’m most interested in the rc for:
source.setXResolution(300.0);
but it looks like you’re on the right track. You see that before you set the units they were inches (and 300 wouldn’t make much sense in most cases nor be supported in any that I’ve ever heard of) and now you’re getting pixels as the units and your 300 is much more likely. Now add another set and right after you set the units to pixels (and double check that the setting ‘sticks’) recheck for the supported x/y resolution. Just execute the Get for x/y resolution after you set the units. In most cases you should either get an enumeration of supported values or a range of acceptable values with a stepsize between each value in the range.
.
@gabe wrote:
rc is return code and it is from the twain spec. If you don’t think that the java wrapper you’re using makes the rc available to you recheck the docs for the library, it could be that the library’s author decieded not to expose it but it seems unlikely to me. While you’re looking for information on an rc look for information on the cc, condition code for the calls to twain as there are situation when dealing with twain that you need to know why a call failed. From the code you submitted I’m most interested in the rc for:
source.setXResolution(300.0);
but it looks like you’re on the right track. You see that before you set the units they were inches (and 300 wouldn’t make much sense in most cases nor be supported in any that I’ve ever heard of) and now you’re getting pixels as the units and your 300 is much more likely. Now add another set and right after you set the units to pixels (and double check that the setting ‘sticks’) recheck for the supported x/y resolution. Just execute the Get for x/y resolution after you set the units. In most cases you should either get an enumeration of supported values or a range of acceptable values with a stepsize between each value in the range.
.
Right after I set the units the get x resolution shows only one value in the range: 1.0.
One important bit of information is that JTwain is not free and I am using a trial edition which has expired (maybe this is the cause of my problems I’m not sure).Anyway I’d be interested in a free solution with the only requisite that I must be able to use it from a Java application. Can I use OpenTwain from Java?
Thanks.
you can use opentwain from java is you know how to use a .net library from java and I’d bet heavily on this issue having something to do with your library. On the plus side you know somewhat at least whats going on and why you’re getting 96dpi.
I am using JTwain and facing the same problem that dabd is facing. I have verified that the resolution (200 x 200) in which I am trying to scan is supported by the scanner. In fact I also verified that when UI is enabled we can see that the driver (in my case HP Scanner dialogue) has actually received the DPI settings, though getCurrentXResolution and getCurrentYResolution are reporting 1.0, but when the scanned image is verified in MSPaint it reports 96 x 96 DPI and GIMP reports 72 x 72 DPI.
The code snippet used is as below
source.setUnits( JTwainConstants.TWUN_PIXELS );
source.setXResolution( 200.00 );
source.setYResolution( 200.00 );
ImageIO.write( source.acquireImageAsBufferedImage(), "jpeg", bos );
JpegBase64 = Base64.encode( bos.toByteArray() );
source.close();
Any help will be much appreciated.
Don’t trust mspaint or any other application to verify the image properties. The image properties shown by all applications once you have processed the image have very little to do with what you received from the twain driver, often they just reflect how you (or the wrapper you are using) saved the image. To verify the settings that were used to acquire your image use ImageInfo in state 6 or 7.
I have verified that the resolution (200 x 200)….
…XResolution and YResolution are reporting 1.0…Those numbers are not relevant until there is a unit attached. You must incorporate Units into your resolution setting&getting if you want consistent results.
.
I am using the following code with JTwain to try to set the DPI to 499 x 499 but the resulting scanned image has 96 x 96 DPI. How can I set the resolution?
Try the following code from Morena – Image Acquisition Framework for Java (http://www.gnome.sk):
TwainSource source=TwainManager.selectSource(null);
source.setVisible(false);
source.setGrayScaleMode();
source.setResolution(499);
MorenaImage image=new MorenaImage(source);
System.err.println("Size of acquired image is "+
+image.getWidth()+" x "
+image.getHeight()+" x "
+image.getPixelSize());
TwainManager.close();You can also run TwainAutomatedTest.java (test.bat, test.sh located at the examples directory of the distribution package). It will write all supported capabilities of your Twain driver to the twainAutomatedTest.txt file. You can see there for the interval of the supported resolutions.
Martin Motovsky
Member of Gnome’s technical support- AuthorPosts