PDA

View Full Version : Qt4 and TWAIN scanning



^NyAw^
6th August 2008, 13:05
Hi,
I'm trying to use Qt4 to scan images using TWAIN on Windows.
I have downloaded Qt TWAIN Package from http://www.cppfrance.com/codes/PACKAGE-QT-TWAIN_46374.aspx and I'm modifing it to use Qt 4. The code was created by "Stephan Stapel" using Qt 3.

I've attached the Visual Studio Solution into the zip file to get any help. I've deleted "qtwaininterface" and "qtwainsubstitute".

The problem is that on "BOOL CTwain::ProcessMessage(MSG msg)" method, the line "if(GetRC() != TWRC_NOTDSEVENT)" always returns "TWRC_NOTDSEVENT" and so, the application never recives TWAIN messages.

I would like to port this to Qt 4 for everyone to want to use it and I will send the final version to the author "Stephan Stapel".

Thanks,

^NyAw^
7th August 2008, 00:54
Hi,

Any Idea why it don't recive TWAIN messages?
I have scanned only one image by calling "TransferImage()" on "CTwain::Acquire(int numImages)" method:



BOOL CTwain::Acquire(int numImages)
{
if(DSOpen() || OpenSource())
{
SetImageCount(numImages);
EnableSource(false);
TransferImage();
return(true);
/*
if(SetImageCount(numImages))
{
if(EnableSource(false))
{
return TRUE;
}
}
*/
}
return FALSE;
}


If you call "EnableSource(true)", the UI of scanner will appear but don't response to the scan button, but response to preview button.

Thanks,

jeffpogo
18th May 2009, 18:06
Did you ever get the scanning to work? I am interested in it too.

Thank you.

ajsri77
2nd February 2010, 13:08
I'm also looking for the same solution. if you have already find out the way to write your own application using TWAIN, please post it here, so that we can also use it.

^NyAw^
2nd February 2010, 13:52
Hi,

I have no much time to explain how I make it works. Cut here, paste there, ... I need it for a commercial application that only scans gray images.
The attached files have been modified.
Here is some explanation how to use it:

1) Declare a "QTwain" class pointer in your application and create a new QTwain instance in the FIRST code line of your MainWindow class using "this" as the parent.


QTwain* m_pqTwain = new QTwain(this);


2)Redefine the method "showEvent(QShowEvent* event)" on your MainWindow class and there call "m_pqTwain->setParent(this)".

3)Redefine the method "winEvent(MSG* pMsg,long* result)" with this call "return(m_pqTwain->processMessage(*pMsg) == TRUE);

4)Now, here is the hack:
To enumerate the sources(Scanner devices) I use "m_pTwain->initTwain()" and "m_pTwain->enumerateSources()"
To scan an image I use "m_pTwain->ReleaseTwain()", and "m_pTwain->initTwain()" again. Select the selected source using "m_pTwain->selectSource(m_iScannerDeviceIndex)". Configure the Scan device using "m_pTwain->selectDepth(8)", "m_pTwain->selectResolutionX(200)", "m_pTwain->selectResolutionY(200)". Finally, start the capture using "m_pTwain->acquire()". Note that if I don't release the Twain dll every time I get a scan finished, the messages are lost(don't know where, maybe in the space, .. :-P)

5) Use a Timer to let the main application check every X seconds if the scanner has finished. You have to check the "m_pTwain->m_bFiEscaneig" that will be true if it have finished. Oops, before starting the capture you have to set it to false by hand.

6) connect the SIGNAL "emetreImatge(QImage)" to your SLOT to get the image.

I know that is not a good solution to use but it works for my purpose. I hope anyone can hack it a little bit and let other people use it. It's not well documented and also, the comments and my defined variables will be in Catalan language(a language used into a region of Spain that includes Barcelona).

jokinb
4th October 2010, 12:04
Perdona Nyaw, pero al bajarme los dos ficheros qtwain.zip y QtTwainScan.zip e intentarlos abrir se produce un error: "Final inesperado del archivo".
Como estoy interesado en poder añadir capacidades para escanear a una aplicación que estoy haciendo, podrÃ*as facilitarnos esos archivos correctamente.

¿Has hecho algún progreso al respecto?

Muchas gracias.

^NyAw^
4th October 2010, 12:09
Hi,

Dear jokinb,
Don't post spanish posts on this forum as people can't read it.
I just downloaded the file and uncopressed using WinRAR and it had no error.
Respect to the progress on this, I said that it works not very well but it is perfect fot my application.
Maybe the newest 2 version of TWAIN will be better.
Of course you can send me private messages written in spanish.

jokinb
4th October 2010, 22:57
I will write in English.
I have domnloaded the qtwain.zip file and I have got the same error. "Unexpected end of archive".

It is posible to use the WIA tecnology in Qt?
Thanks
Jokinb

^NyAw^
5th October 2010, 09:25
Hi,

Try this, I only donwloaded the file from this forum uncompressed and compressed again.

jokinb
5th October 2010, 12:26
Finally , with a lot of tricks I have got the qtwain file downloaded.
How many files are in. I get six. It is ok.?

Thanks
Jokinb

^NyAw^
5th October 2010, 12:57
Hi,

Yes, there are 6 files.

I don't understand why you have to use a lot of tricks. Is there any other person that can try downloading the file and decompress it to check if there is any error on uncompress it?

nish
5th October 2010, 13:06
the notice on the forum says that they have some problems into attachments before 1st sep 2010. may be that is the issue. the new file is good.

jokinb
5th October 2010, 18:53
I don´t Know but the newest file had the same problem that the first one to open. I don´t Know why but... I tried with diferents programs to deskompress the file . The same message..."end of file corrupt"... "the file can´t bee opened"....

Finnally i got the file opened in an unusual way. I have the six files and must find time to study them.

Thanks

Jokinb

Kata
29th December 2010, 10:51
Hello all,

Have you got any simple example to use this class ?
I have try to acquire and save 1 A4 image :

#include <iostream>
#include "qtwain.h"

using namespace std;

int main(int agc, char **argv)
{
QTwain t;
t.initTwain();
if(t.isValidDriver() && t.selectSource())
{
t.setResolutionY(1654);
t.setResolutionX(2339);
t.setDepth(1);
t.acquire(1);
}
return 0;
}


But it doesn't work; why ?

For your help, by advance, thanks.

high_flyer
29th December 2010, 10:58
The 'example' in the code!
Just look in there.


void QtTwainScan::scan()
{
if (!m_pTwain->initTwain())
qWarning("initTwain() call not successful!");
else
if (!m_pTwain->selectSource())
qWarning("selectSource() call not successful!");
else
if (!m_pTwain->acquire())
qWarning("acquire() call not successful!");
}

Kata
3rd January 2011, 16:56
The 'example' in the code!
Just look in there.


void QtTwainScan::scan()
{
if (!m_pTwain->initTwain())
qWarning("initTwain() call not successful!");
else
if (!m_pTwain->selectSource())
qWarning("selectSource() call not successful!");
else
if (!m_pTwain->acquire())
qWarning("acquire() call not successful!");
}


Happy new year.
I have tried this (with QtCreator or the CLI) :

#include <QApplication>
#include "qtwain.h"

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTwain m_pTwain;
if (!m_pTwain.initTwain())
qWarning("initTwain() call not successful!");
else
if (!m_pTwain.selectSource())
qWarning("selectSource() call not successful!");
else
if (!m_pTwain.acquire())
qWarning("acquire() call not successful!");
return app.exec();
}


But I still have a segmentation error,
Have you ever have this bug ?

By advance,
Thanks

high_flyer
3rd January 2011, 17:20
Happy new year to you too! :)

I just had a 2 seconds look - to see if I could help you getting started.
I didn't look further.
But you should.
Maybe there are some initializations needed to be done, I don't know, stuff like that.
Follow the application in the debugger, and see exactly where it crashes, it will tell you a lot about what the problem is.

d_stranz
3rd January 2011, 17:20
It is probably because the QTwain class requires an event loop to be running, and until you call app.exec(), there is no event loop.

Your code is fundamentally different from the example in the previous post in the thread. You need to move your twain code into a method that gets called after the event loop has started.

high_flyer
3rd January 2011, 17:23
Good point.

Kata
10th January 2011, 15:37
It is probably because the QTwain class requires an event loop to be running, and until you call app.exec(), there is no event loop.

Your code is fundamentally different from the example in the previous post in the thread. You need to move your twain code into a method that gets called after the event loop has started.

Thank you very much,
I create a Widget QTwain and used according to your examples, but I have not found a way to save the image acquired only by the interface.
The only way, according to my investigations, would be to uncomment the lines 92 to 95 of qtwain.cpp, and yet I know not how to get the values to pass as an argument to CopyImage.

For your help,
By advance,
Thanks

^NyAw^
11th January 2011, 10:12
Hi,

As I said, I only modify code snippets to get to do what I needed. Improvements that you can get will be welcome.
I only tested on a Brother multifunction printer, so If anyone can test on othre devices and check if it works it will be great.

Kata
17th January 2011, 16:30
Hello,
thank you for your answers
I have cleaned the code and set more generical QtTwain and I have added a methode to save the scanned image.

Enjoy

elegido03
25th April 2011, 13:23
Hi

First thanks to kata for you help. Now the problem is that I can not see the image out.jpg. debido a esto:

QPixmap::fromWinHBITMAP(), failed to get bitmap info ()

I have windows 7, qt creator 2.01 based qt 4.7.0.

this is what I do

1)instal qt creator sdk
2) open qt creator, open project, I look for. pro and open it.
3) running

What am I doing wrong? or I'm missing.
-----------------------------------------------------
In the .pro :
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
LIBS += -lgdi32

# Input
HEADERS += qtwain.h stdafx.h twain.h twaincpp.h
SOURCES += main.cpp qtwain.cpp twaincpp.cpp
-------------------------------------------------------
Qt displays the following messages:

Application Output:
Starting C:\Users\Elegido\Desktop\QtScanner5-build-desktop\debug\QtScanner5.exe...
QPixmap::fromWinHBITMAP(), failed to get bitmap info ()

Compile output:
Running build steps for project QtScanner5...
Configuration unchanged, skipping qmake step.
Starting: "C:/Qt/2010.05/mingw/bin/mingw32-make.exe" -w
mingw32-make: Entering directory `C:/Users/Elegido/Desktop/QtScanner5-build-desktop'

C:/Qt/2010.05/mingw/bin/mingw32-make -f Makefile.Debug

mingw32-make[1]: Entering directory `C:/Users/Elegido/Desktop/QtScanner5-build-desktop'

mingw32-make[1]: Nothing to be done for `first'.

mingw32-make[1]: Leaving directory `C:/Users/Elegido/Desktop/QtScanner5-build-desktop'

mingw32-make: Leaving directory `C:/Users/Elegido/Desktop/QtScanner5-build-desktop'

The process "C:/Qt/2010.05/mingw/bin/mingw32-make.exe" exited normally.

PLease Help!! tThanks

^NyAw^
26th April 2011, 14:00
Hi,

Someone is telling me that the file is not uncompressing right. Try this other one

schweini
24th May 2011, 16:32
Hi!
Can u help me to save an image from scanner and display it in a dialog
thanks and best regard ^^

hassanbuzer
8th February 2017, 11:39
Hello everyone I've tryed twain but all I can get is To select the scanner an nothing more.. NO SCANNING ACTION. Any one to tell how can use it in my application . thank u.

srsr
8th February 2017, 16:46
I will write in English.
I have domnloaded the qtwain.zip file and I have got the same error. "Unexpected end of archive".

It is posible to use the WIA tecnology in Qt?
Thanks
Jokinb
and

YES it is possible to use WIA technology with Qt. It's even necessary with Windows10.
Just start with Windows SDK sample code :for example fiScanner

SRSR

armad
21st February 2019, 13:13
Hi, I would like to know you solved this problem? if yes please share with me, I'm just starting to learn Qt and this has become a problem for me. thank you in advance

Added after 39 minutes:

Hi, I would like to know you solved this problem? if yes please share with me, I'm just starting to learn Qt and this has become a problem for me. thank you in advance