Home › Forums › TWAIN Classic › From Java with user interface Twain
- This topic has 27 replies, 6 voices, and was last updated 8 years, 4 months ago by mikelilin.
- AuthorPosts
I’ve been working on this an hour, all I get for g4 is mac … some tiff and jpeg stuff but this is gonna be slogging unless I can get some trick pointers. Okay,
An image consists of ( ? ) a bitstream which I may consider for the moment to be alpha rgb packed according to some spec. Would rather user cmyk but that will come soon enough.
Graphics will have headers that tell one such things as 640×480 and 1028×764 such as how the bitops packed the pixels. jpeg has advanced compression algora that are beyond me. I want { x , y } ordered pair has enough information that I can decide if that pixel is recordable as set, vis-a-vis “blank”
A great deal will be simplified if I can get past the information that assumes one does not wish to study bits, bytes, and Alpha + Color channel…gonna be needless waste otherwise.
found an open project at mms computing, a great deal of the work is already done – what we will have to contend with is gui – those things should be called “notorious interface” – has isues like: “Due to the fact that twain is signalling events through the OS-dependent event queues, we need to set up an application window including it’s wndProc callback function.” …. I am put in a pitiful position of not being able to tell you the place where this will run will use that to make life difficult for me + and do it for no reason.
The place where I am going to setup is an isolated area protected by SSL and so on, we drive some things remotely as it is proven ……
never mind.
So anyway, there are going to be some compiler warnings and errors that are going to be uh, challenging 🙂 to get rid of, e.g. packing ints into a byte array:
// copy container pointed to by 'handle' into byte buffer
That sits aside trying to implement CannyEdgeDetector + OCR …. having never done so. Gosh, the possiblities, Road Dog doing forefront cs! Hope I don’t get any re-entrant callbacks stuck in a loop……
In EZTwain classic the only transfer mode (1 out of 3) implemented is the native transfer. Which means it will save the data to a DIB in memory. No compression.
By “g4” I don’t mean the CPU previously used in macs, but the CCITT Group 4 compression for black&white images used in fax machines, which is the best compression around for b&w images and is supported in the TWAIN driver of some scanners. You can usually setup that for TIFF files, if your model does support it.
The window for TWAIN messages is solved by creating a proxy window (not visible) to receive them. If I remember correctly EZTwain already does this. The C code for Visual Studio 6 in the “Vc” directory should have the implementation.
I spent something around three days digging into the internals, ended up with part done sorting things to where they should be and stuff like:
javascannerukcommscomputingdeviceukcommscomputingnewsindex.html
due to the usual. There’s a stubb for the idea on google, someone has taken the position of Team Lead and has a discussion avenue stubbed with some code to work with, I sorta need to set aside the compression work as likely to drill-in to historical matters and get nothing done. ( me – maybe them too )
….javaEZTWAIN1VCEZTWAIN.C has a message loop already coded at line 198 so I can get in the message loop, thus achieving a way to retire the allocated resources on long loads when a shutdown hook in code might be the way someone else would do it.
At 958 in EZTWAIN.C the code
hDSMLib = LoadLibrary(szSMDir);
…. I guess what we do here is load the twain dll and call into that, but I do not see the point of the copy operation in there, ( that was some C/CPP code deeper in the exploration ) efforts to just get the datastream drilled in on what we know and loath:
“… ( some commercial entity ) …. is no longer in business, TIFF Class F remains in use and is considered by some to be an excellent storage format for facsimile data.”
( not the tiff stuff, the 🙄 no-longer-in-business-because-tried-to-do-something: USEFUL )
… is there some call that will get me TWAIN_RGB ? I do not see what the pallette is for, ( if I can get ) a simple translation that will get the datastream extracted by going through the pallette operation or whatever it wants to do arriving at what amouts to a pointer into the data, with a height x width value and a way to do grayscale[][] from the DIB would really help matters for me right this moment.
I spent last night stubbing a dll to receive the call from Java – there are issues there as Java code practice is that numbers are signed, always, nothing else is signed ….. getting direct bitops and byte ops on image data directly in Java is asking for it.
I did manage to run javah on my Java sources, contorted from the preliminary work of bahri.gencsoy
here’s what I have stubbed in the dll to recieve the call into native code:
/* Gabriel DeClieu (also referenced as Mathieu Gabriel De
* Clieu and Chevalier Gabriel Mathiew de Clieu) was a French
* naval officer whom is attributed to bringing coffee to the
* Americas in 1723. It is said that the Dutch unwittingly
* provided Louis XIV of France with a coffee bush that
* Gabriel DeClieu in turn made due with a seedling which he
* transported to Martinique whilst supposedly sacrificing his
* water ration to the seedling after a storm ravaged his
* vessel. Within 50 years of an official survey a recorded 19
* million coffee trees are said to have sprouted within that
* time. It is said that 90% of the world's coffee spread from
* this single plant.[source: wiki]
*/
__declspec(dllexport) long _callUncompressedCMYKload(void* data_pointer, long data_size)
{
;//int fntessjnidll(void)
}
//
__declspec(dllexport) long _callUncompressedCMYKstore(void* data_pointer,long data_size)
{
;//int fntessjnidll(void)
}note placing active code behind comment marker for now
and there hung on the java does not do signed character stuff and how to call in,….. i was running low on brain-calories at that point, and had been for several hours.
I tried to write a C++ class for this, don’t think I really need that, probably Java will load a dll just as well …. still sorting things where they need to be – Issue 93: TessDLL wraper for java at google goes into several issues, I just wrote my own Java code and dug in for the effort:
// remove this line to get the file to compile.
private Khazad khazad = new Khazad();
// load dll I am prototyping:
static{Runtime.getRuntime().loadLibrary("****DLL");}
// the designer of testJeractApproach affirms under the
// reasonable person test that there are no intentions of basing
// the work on practices of the 16-bit bus.
public testJeractApproach(File fileName)
{
//
super();
//
dataBufferByte = new DataBufferByte(DataBufferByte.TYPE_INT,0x40000);
//
edgeDetector = new CannyEdgeDetector();
}
//skipping the issue of L-2 writebacks and so on ( for now ) that the use of java.io.File raises, going for some sort of “read the scanner” as a delcared buffer and begin some sort of edge detection or other work, first just getting the thing to run so I could use it for my own work in my daily business, from Java, to do intake on warehousing reports.
The whole thing stalled on getting grayscale[][] as image_height / image_width – I do not have the skills to do imaging on a flat dataspace ( [] rather than [][] ) and at that point I had about five thought trains running in parallel, looking at function calls buried so deep in thousands of lines of code that I felt my grasp of where I was at evaporating like Coffee Aroma being breezed out an Open Window…..
gonna have to set down the work for a few days and take care of business.
You can set the scanner to scan in RGB colorspace in TWAIN, then you can convert that data to grayscale as you wish. Look for the pixeltype declarations in TWAIN.H and the specs. You might be able to scan directly in grayscale if you only need that. The value for RGB is TWPT_RGB and TWPT_GRAY for grayscale if I remember correctly.
/* ICAP_PIXELTYPE values (PT_ means Pixel Type) */
9 indice, including:
#define TWPT_GRAY 1
#define TWPT_RGB 2where’s ICAP? I know about google and so on, + wkik has profound consequences in information availability and will go to both of those places right now … but I was trying to do that last time I posted and had 50,000 lines of code available – did not know where I was at. What I am asking for from you is a one-point place to start such that I do not drill in on consumer grade resources: The place I am writing this to run has to run as commercial grade equipment, do so in an arena of the linguists that are being shipped by todays “computer science” graduate level work, having trace controls that can be linked to SHA or MD-5 in the background ( no ui ) ….
Just where to start, that’s all.
“Use of operation triplets” in the TWAIN 1.9 spec. It’s necessary to know about how triplets work to setup the scanner. The spec also has a list of icaps with explanations about their functionality and parameters. With that basis and the functions in EZTwain it’s easy to change parameters. It’s important to spend some time going over the TWAIN spec (at least the sections relevant to application developers).
Also download and use the TWAIN sample application from the twain.org website, it’s useful to list and test the capabilities supported in the scanner.
The ICAP you’re asking about is in the TWAIN.H file. Once you incorporate that header file to your project, if you’re using Visual Studio with autocomplete/intellisense enabled you can just type “tw” and control+space to get a list of TWAIN-related declarations.
To use the library with no UI displaying, check the EZTwain functions TWAIN_SetHideUI and TWAIN_SetIndicators.
The functions declared in EZTWAIN.H are also a good way to learn how to program TWAIN.
Before I run some code and tear up my only machine that I can use for a development platform:
int EZTAPI TWAIN_IsAvailable(void);
goes to ( … terminology… ) the operating system, sort of a shell that calls into kernel mode, returning true ( non-zero ) if a device of some kind has been installed on the system that supports twain – correct?
@fct wrote:
“Use of operation triplets” in the TWAIN 1.9 spec. It’s necessary to know about how triplets work to setup the scanner. The spec also has a list of icaps with explanations about their functionality and parameters. With that basis and the functions in EZTwain it’s easy to change parameters. It’s important to spend some time going over the TWAIN spec (at least the sections relevant to application developers).
I’m gonna be awhile on this, will have to check back ( re-post ) if addional research leads are needed.
@fct wrote:
Also download and use the TWAIN sample application from the twain.org website, it’s useful to list and test the capabilities supported in the scanner.
I have two directories:
Twain_DS_sample01
which has
TWAIN Data Source [DS]
which is a ram resident virtual scanner, and
TWAIN Application
which is a TWAIN Application and gets into vc and so on, I threw away vc a long time ago and use a C/C++ compiler that expects one to dig through the headers yourself, and in doing so I am seeing a great deal of very professonal work.
@fct wrote:
The ICAP you’re asking about is in the TWAIN.H file. Once you incorporate that header file to your project, if you’re using Visual Studio with autocomplete/intellisense enabled you can just type “tw” and control+space to get a list of TWAIN-related declarations.
I had pretty much found that already or knew I would be finding it momentarily as soon as I dug in, intellisense drives me nuts. What I was asking is a “where to start” when I start looking at
TW_UINT8 Index; /* Value used to index into the color table. */
TW_UINT8 Channel1; /* First tri-stimulus value (e.g Red) */
TW_UINT8 Channel2; /* Second tri-stimulus value (e.g Green) */
TW_UINT8 Channel3; /* Third tri-stimulus value (e.g Blue) */and do not have sufficiently informed technical information to dig into. I have tried the magic-button approach and that just will not cut it for where I want this to run. On the opposition postion, I will be in over my head so it is a hell of a job trying to find ground zero in ten acres of code with no cs degree.
@fct wrote:
To use the library with no UI displaying, check the EZTwain functions TWAIN_SetHideUI and TWAIN_SetIndicators.
My ui will be driven by Java, this is native code which may pop-up a dialog if it just has to but pretty sure I can do reading on SetHideUI && SetIndicators and figure it out. I have a lot of reading to do, it’s gonna be where to find the values that are passed into these calls and what the rerturn value signifies that is of consequence in my questions. Documentaton, so called becaue it is, does not do me any good when it insists on calling throgh the shell to give me the documentation, I run in raw text mode with editors that do not have to do a context switch on every character and consider it a litmus test of the beginner to be able to dig throgh 10k headers and find in things almost as fast as the intellisense but without the intrusion from the sales department that brings.
It doesn’t do any good to use that stuff if one has to kowtow to help department from hell and breaks the budget.
@fct wrote:
The functions declared in EZTWAIN.H are also a good way to learn how to program TWAIN.
So I could just start with
HANDLE EZTAPI TWAIN_AcquireNative(HWND hwndApp, unsigned wPixTypes);
and call GlobalFree after the call, wouldn’t that release the handle to the data I just got? I mean I was looking a little deeper and for some reason someone had done the load with copy semantics. I understand how that stuff gets started, STL has a lot of it and we have some very good reasons why that happens but if I call something and it just has to do copy-semantics then it can write the copy to a pointer I alloc and it needs to free the pointer it alloc’d after passsing the buffer.
Bloating ram is exactly one of the attack vectors I see too much of, I’m a near-nutcase about it.
@Nicholas Jordan wrote:
@fct wrote:
Also download and use the TWAIN sample application from the twain.org website, it’s useful to list and test the capabilities supported in the scanner.
I have two directories:
Twain_DS_sample01
which has
TWAIN Data Source [DS]
which is a ram resident virtual scanner, and
TWAIN Application
which is a TWAIN Application and gets into vc and so on, I threw away vc a long time ago and use a C/C++ compiler that expects one to dig through the headers yourself, and in doing so I am seeing a great deal of very professonal work.
I’m talking about the already-compiled TWAIN data source application linked from the front page in twain.org.
@Nicholas Jordan wrote:
I had pretty much found that already or knew I would be finding it momentarily as soon as I dug in, intellisense drives me nuts. What I was asking is a “where to start” when I start looking at
TW_UINT8 Index; /* Value used to index into the color table. */
TW_UINT8 Channel1; /* First tri-stimulus value (e.g Red) */
TW_UINT8 Channel2; /* Second tri-stimulus value (e.g Green) */
TW_UINT8 Channel3; /* Third tri-stimulus value (e.g Blue) */and do not have sufficiently informed technical information to dig into. I have tried the magic-button approach and that just will not cut it for where I want this to run. On the opposition postion, I will be in over my head so it is a hell of a job trying to find ground zero in ten acres of code with no cs degree.
You’re looking at data for an 8bpp palette color mode.
That’s neither 8bpp grayscale mode nor 24bpp RGB. It’s almost sure you won’t need to use that.
All the technical information you need to understand how TWAIN works is in the specification. EZTwain classic implements parts of it, but just reading the source (or the TWAIN.H header) won’t cut it if you need to extend EZTwain.
@Nicholas Jordan wrote:
My ui will be driven by Java, this is native code which may pop-up a dialog if it just has to but pretty sure I can do reading on SetHideUI && SetIndicators and figure it out. I have a lot of reading to do, it’s gonna be where to find the values that are passed into these calls and what the rerturn value signifies that is of consequence in my questions.
From the EZTWAIN.H header:
void EZTAPI TWAIN_SetHideUI(int fHide);
int EZTAPI TWAIN_GetHideUI(void);
// These functions control the 'hide source user interface' flag.
// This flag is cleared initially, but if you set it non-zero, then when
// a source is enabled it will be asked to hide its user interface.
// Note that this is only a request - some sources will ignore it!
// This affects AcquireNative, AcquireToClipboard, and EnableSource.
// If the user interface is hidden, you will probably want to set at least
// some of the basic acquisition parameters yourself - see
// SetCurrentUnits, SetBitDepth, SetCurrentPixelType and
// SetCurrentResolution below.Also, sorry. SetIndicators is a function I implemented myself, it’s not in the EZTwain code you downloaded. But anyway it’s mostly one line using the CAP_INDICATORS capability and the TWAIN_SetCapOneValue function.
@Nicholas Jordan wrote:
@fct wrote:
The functions declared in EZTWAIN.H are also a good way to learn how to program TWAIN.
So I could just start with
HANDLE EZTAPI TWAIN_AcquireNative(HWND hwndApp, unsigned wPixTypes);
and call GlobalFree after the call, wouldn’t that release the handle to the data I just got?
Yes.
@Nicholas Jordan wrote:
Bloating ram is exactly one of the attack vectors I see too much of, I’m a near-nutcase about it.
Then consider implementing either buffered or direct to disk transfers.
@fct wrote:
I’m talking about the already-compiled TWAIN data source application linked from the front page in twain.org.
Okay, it will probably do a lot to orient me in the twain spec to run the testbed first,…I have to keep some idea of where I am at.
@fct wrote:You’re looking at data for an 8bpp palette color mode.
That’s neither 8bpp grayscale mode nor 24bpp RGB. It’s almost sure you won’t need to use that.
Just grabbed something to elicit your next paragraph
@fct wrote:
All the technical information you need to understand how TWAIN works is in the specification. EZTwain classic implements parts of it, but just reading the source (or the TWAIN.H header) won’t cut it if you need to extend EZTwain.
So it gives all ( *ALL* ) [in][out]? ( don’t use that syntax, here for clarity ) I use as in the code you posted somewhat down the page. What is at issue here ( I did extract the text of the spec – and have it where I can find it ) is reading those specs too often do not have enough of the information that is in the headers ( #defines as paramater values ), thus results do not always have enough information to disentangle things like the poster I was just reading on The Code Project who had struggles trying do get the vals passed into the message loop and what they convey.
I guess I am in a professional grade of work that I am not normally encountering.
Your work is remarkable, I do not know if I will need to extend EZTwain as I have not made enough progress to report. I knew the code snip I pulled probably had the wrong bpp and so on, sorta what we get to here to be effective is a 0-1-2-3-4-5- do this do that to get a basic grayscale from what amounts to beginner grade work, doing that without knowing what ( coder ) doing. Writing code is vastly more powerful than pushing a few reset buttons on the front panel…..
@fct wrote:
From the EZTWAIN.H header:
.....
// This affects AcquireNative, AcquireToClipboard, and EnableSource.
...Okay, that’s where I’m gonna start.
@fct wrote:Also, sorry. SetIndicators is a function I implemented myself, it’s not in the EZTwain code you downloaded. But anyway it’s mostly one line using the CAP_INDICATORS capability and the TWAIN_SetCapOneValue function.
?
@fct wrote:
Then consider implementing either buffered or direct to disk transfers.
Can that be done with a call you have already implemented in EZTwain?
/*
* Class: ocr_zarf
* Method: zarf_AcquireNative
* Signature: ()[B
*/
__declspec(dllexport) jbyteArray __stdcall Java_ocr_zarf_zarf_1AcquireNative (JNIEnv * JEnvironmentPointer, jobject jayObject)
{ // 1
static jfieldID fid_s = NULL; /* cached field ID for s */
jclass cls = (*JEnvironmentPointer)->GetObjectClass(JEnvironmentPointer, jayObject);
jclass localRefBufferClass = (*JEnvironmentPointer)->FindClass(JEnvironmentPointer, "java/awt/image/DataBufferByte");
if(availability > 0x00000000)
{ // 2
void * ImageHandle_hnative=(void *)TWAIN_AcquireNative(0x00000000,TWAIN_GRAY);
if(ImageHandle_hnative) // 0 == NULL == no entry
{ // 3
//
void * bitMapHandle=(void *)GlobalLock(ImageHandle_hnative);
if(bitMapHandle)
{
LPSTR pBitmapInfo;
PBITMAPINFOHEADER pBitmapInfoPointer = (PBITMAPINFOHEADER)bitMapHandle;
KalyttaPointer = ( KalyttaPointer ) pBitmapInfoPointer;
LONG const widthOfImage=(LONG)pBitmapInfoPointer->biWidth;
LONG const heightOfImage=(LONG)pBitmapInfoPointer->biHeight;
int bitsPerPixel=(int)pBitmapInfoPointer->biBitCount;
if((widthOfImage > 0x00000000)&&(heightOfImage > 0x00000000))
{
int RowScanIndex=heightOfImage;
unsigned char * imageData= ( unsigned char * ) malloc( heightOfImage * widthOfImage );
do
{
DIB_ReadRow((void *)pBitmapInfoPointer,--RowScanIndex, ( unsigned char * ) imageData[RowScanIndex]);
}
while(RowScanIndex > 0x00000000);
// begin image ops
// end image ops
free(imageData);
GlobalFree(bitMapHandle);
}
}
}
}
}
this is going pretty slow, any suggestions on how to go about this?
🙂Hi!, I’m actually working with Java and I want to use Twain but I only want to scan from a scanner with a sets of parameters (Color, 300dpi). How can I do this without the user interfaze of Twain, only into a call to any function???
If you decide to use Morena Image Acquisition Framework for Java (http://www.gnome.sk), you can hide Twain User Interface window by calling setVisible(false) method of the TwainSource class. And then set all capabiities directly from your application. (It could happend, that setVisible method will be not implemented by the Twain driver.)
Here is a snapshot demonstrating how to do it:
TwainSource source = TwainManager.selectSource(null);
source.setVisible(false);
source.setColorMode();
source.setResolution(300);
MorenaImage morenaImage = new MorenaImage(source);
Image image=Toolkit.getDefaultToolkit().createImage(morenaImage);
TwainManager.close();Now I have another problem. I want to scan in duplex mode and I don’t know how to capture the two images. Can you help me???.
Using Morena you can use the following code design:
TwainSource source=TwainManager.selectSource(null);
if (source!=null)
{
source.setDuplexEnabled(true);
source.setFeederEnabled(true);
source.setAutoFeed(true);
source.setTransferCount(-1);
int count=1;
do
{ MorenaImage image=new MorenaImage(source);
System.err.println("Size of acquired image "+(count++)+" is "
+image.getWidth()+" x "
+image.getHeight()+" x "
+image.getPixelSize());
}
while (source.hasMoreImages());
}
TwainManager.close();Martin Motovsky
Member of Gnome’s technical supportI know that it’s quite a long time that those posts are here but I found them while looking for a free online ocr solution in Java, and I would like to share the FREE answer I have created.
I browsed lots of posts while searching for OCR in Java, and all was linking to Asprise / javaocr, but those are unaffordable for non-commercial project.
quite agree with you, I prefer free ocr tools, and there are many in google search, and tesseract ocr is also a good one.
- AuthorPosts