Git Product home page Git Product logo

node-twain's Introduction

node-twain

A nodejs library for TWAIN using node-addon-api.

此项目处在开发阶段。目前开发者手头已经没有扫描仪了,已经搁置了开发进程。

如果有人可以捐助一台扫描仪,开发者会考虑继续维护。

目前TWAIN官方没有提供Mac版本的server端。所以没办法进一步支持Mac

NOTE: Project is under develop. PR is welcomed, any issue is welcomed.

Installation

$ npm install node-twain

Twain flow

img.png

Usage

import {twain} from "node-twain"

const app = new twain.TwainSDK( {
    productName: "productName!",
    productFamily: "productFamily!",
    manufacturer: "manufacturer!",
    version: {
        country: twain.TWCY_CHINA,
        language: twain.TWLG_CHINESE,
        majorNum: 1,
        minorNum: 1,
        info: "v0.0.1",
    }
})
const sources = app.getDataSources()            // ["PaperStream IP SP-1120N #2"]
const defaultSource = app.getDefaultSource()    // "PaperStream IP SP-1120N #2"
app.setDefaultSource(sources[0])    //set which scanner to work

await app.openDataSource(defaultSource) //args 

const enumTest = app.getCapability(twain.ICAP_XFERMECH)         // Enum
const oneValueTest = app.getCapability(twain.CAP_AUTHOR)        // one value
const rangeTest = app.getCapability(twain.ICAP_JPEGQUALITY)     // range
const arrayTest = app.getCapability(twain.CAP_SUPPORTEDCAPS)    // array

session.setCallback()
app.scan(twain.TWSX_FILE, fileName);  //fileName:Full path

Data Source Support

According to twain‘s github, Data Source Manager looks for data sources in

OperaSystem directory
windows C:\Windows\twain_32 C:\Windows\twain_32
Linux /usr/local/lib/twain
MacOS /Library/Image Capture/TWAIN Data Sources

Install your scanner driver and confirm whether your scanner support TWAIN or not.

License

MIT License. See the LICENSE file.

Setup Develop Environment

Windows

  • Python is required for node-gyp is used to build package.

  • Vitual Studio is required.

Steps

  1. Confirm Twain Datasource Manage is supported. Checkout this directory:
C:\Windows\System32

If there is no file "TWAINDSM.dll", copy the "TWAINDSM.dll" file into the System32 directory.

  1. Confirm the scanner driver is installed. For example, my scanner is SP-1120N.

PS: The scanner MUST support TWAIN protocal.

scanner_driver.jpg

  1. Run the test script
npm run test

This command will search *.test.js, then run the twain.test.js script.

node-twain's People

Contributors

luomusha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

node-twain's Issues

Installation failes

npm install node-twain
npm ERR! code 1
npm ERR! path /Users/username/Documents/projects/node-scanner/node_modules/node-twain
npm ERR! command failed
npm ERR! command sh -c -- node-gyp rebuild
npm ERR! CXX(target) Release/obj.target/twain/src/main.o
npm ERR!   CXX(target) Release/obj.target/twain/src/TwainSession.o
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | darwin | arm64
npm ERR! gyp info find Python using Python version 3.10.9 found at "/opt/homebrew/opt/[email protected]/bin/python3.10"
npm ERR! gyp info spawn /opt/homebrew/opt/[email protected]/bin/python3.10
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args   '/Users/username/.nvm/versions/node/v18.12.0/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args   'binding.gyp',
npm ERR! gyp info spawn args   '-f',
npm ERR! gyp info spawn args   'make',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/Users/username/Documents/projects/node-scanner/node_modules/node-twain/build/config.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/Users/username/.nvm/versions/node/v18.12.0/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/Users/username/Library/Caches/node-gyp/18.12.0/include/node/common.gypi',
npm ERR! gyp info spawn args   '-Dlibrary=shared_library',
npm ERR! gyp info spawn args   '-Dvisibility=default',
npm ERR! gyp info spawn args   '-Dnode_root_dir=/Users/username/Library/Caches/node-gyp/18.12.0',
npm ERR! gyp info spawn args   '-Dnode_gyp_dir=/Users/username/.nvm/versions/node/v18.12.0/lib/node_modules/npm/node_modules/node-gyp',
npm ERR! gyp info spawn args   '-Dnode_lib_file=/Users/username/Library/Caches/node-gyp/18.12.0/<(target_arch)/node.lib',
npm ERR! gyp info spawn args   '-Dmodule_root_dir=/Users/username/Documents/projects/node-scanner/node_modules/node-twain',
npm ERR! gyp info spawn args   '-Dnode_engine=v8',
npm ERR! gyp info spawn args   '--depth=.',
npm ERR! gyp info spawn args   '--no-parallel',
npm ERR! gyp info spawn args   '--generator-output',
npm ERR! gyp info spawn args   'build',
npm ERR! gyp info spawn args   '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! gyp info spawn make
npm ERR! gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
npm ERR! In file included from ../src/main.cpp:2:
npm ERR! In file included from ../src/TwainSDK.h:11:
npm ERR! In file included from ../src/TwainSession.h:12:
npm ERR! ../src/twain/Common.h:201:11: warning: 'MAX' macro redefined [-Wmacro-redefined]
npm ERR!   #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
npm ERR!           ^
npm ERR! /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/param.h:218:9: note: previous definition is here
npm ERR! #define MAX(a, b) (((a)>(b))?(a):(b))
npm ERR!         ^
npm ERR! In file included from ../src/main.cpp:2:
npm ERR! In file included from ../src/TwainSDK.h:11:
npm ERR! In file included from ../src/TwainSession.h:12:
npm ERR! ../src/twain/Common.h:202:11: warning: 'MIN' macro redefined [-Wmacro-redefined]
npm ERR!   #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
npm ERR!           ^
npm ERR! /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/param.h:215:9: note: previous definition is here
npm ERR! #define MIN(a, b) (((a)<(b))?(a):(b))
npm ERR!         ^
npm ERR! In file included from ../src/main.cpp:2:
npm ERR! In file included from ../src/TwainSDK.h:11:
npm ERR! In file included from ../src/TwainSession.h:12:
npm ERR! ../src/twain/Common.h:207:13: warning: 'kTWAIN_DSM_DIR' macro redefined [-Wmacro-redefined]
npm ERR!     #define kTWAIN_DSM_DIR       "/Library/Frameworks/TWAINDSM.framework/Versions/Current/"
npm ERR!             ^
npm ERR! ../src/twain/Common.h:108:13: note: previous definition is here
npm ERR!     #define kTWAIN_DSM_DIR         "/usr/local/lib/"
npm ERR!             ^
npm ERR! ../src/twain/Common.h:208:13: warning: 'kTWAIN_DSM_DLL_NAME' macro redefined [-Wmacro-redefined]
npm ERR!     #define kTWAIN_DSM_DLL_NAME  "TWAINDSM"
npm ERR!             ^
npm ERR! ../src/twain/Common.h:194:11: note: previous definition is here
npm ERR!   #define kTWAIN_DSM_DLL_NAME "libtwaindsm.so"
npm ERR!           ^
npm ERR! In file included from ../src/main.cpp:2:
npm ERR! In file included from ../src/TwainSDK.h:11:
npm ERR! ../src/TwainSession.h:104:33: warning: missing field 'DSM_Entry' initializer [-Wmissing-field-initializers]
npm ERR!     TW_ENTRYPOINT gDSMEntry = {0};
npm ERR!                                 ^
npm ERR! 5 warnings generated.
npm ERR! In file included from ../src/TwainSession.cpp:5:
npm ERR! In file included from ../src/TwainSession.h:12:
npm ERR! ../src/twain/Common.h:201:11: warning: 'MAX' macro redefined [-Wmacro-redefined]
npm ERR!   #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
npm ERR!           ^
npm ERR! /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/param.h:218:9: note: previous definition is here
npm ERR! #define MAX(a, b) (((a)>(b))?(a):(b))
npm ERR!         ^
npm ERR! In file included from ../src/TwainSession.cpp:5:
npm ERR! In file included from ../src/TwainSession.h:12:
npm ERR! ../src/twain/Common.h:202:11: warning: 'MIN' macro redefined [-Wmacro-redefined]
npm ERR!   #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
npm ERR!           ^
npm ERR! /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/param.h:215:9: note: previous definition is here
npm ERR! #define MIN(a, b) (((a)<(b))?(a):(b))
npm ERR!         ^
npm ERR! In file included from ../src/TwainSession.cpp:5:
npm ERR! In file included from ../src/TwainSession.h:12:
npm ERR! ../src/twain/Common.h:207:13: warning: 'kTWAIN_DSM_DIR' macro redefined [-Wmacro-redefined]
npm ERR!     #define kTWAIN_DSM_DIR       "/Library/Frameworks/TWAINDSM.framework/Versions/Current/"
npm ERR!             ^
npm ERR! ../src/twain/Common.h:108:13: note: previous definition is here
npm ERR!     #define kTWAIN_DSM_DIR         "/usr/local/lib/"
npm ERR!             ^
npm ERR! ../src/twain/Common.h:208:13: warning: 'kTWAIN_DSM_DLL_NAME' macro redefined [-Wmacro-redefined]
npm ERR!     #define kTWAIN_DSM_DLL_NAME  "TWAINDSM"
npm ERR!             ^
npm ERR! ../src/twain/Common.h:194:11: note: previous definition is here
npm ERR!   #define kTWAIN_DSM_DLL_NAME "libtwaindsm.so"
npm ERR!           ^
npm ERR! In file included from ../src/TwainSession.cpp:5:
npm ERR! ../src/TwainSession.h:104:33: warning: missing field 'DSM_Entry' initializer [-Wmissing-field-initializers]
npm ERR!     TW_ENTRYPOINT gDSMEntry = {0};
npm ERR!                                 ^
npm ERR! ../src/TwainSession.cpp:61:14: warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
npm ERR!     if(error = dlerror() != NULL) {
npm ERR!        ~~~~~~^~~~~~~~~~~~~~~~~~~
npm ERR! ../src/TwainSession.cpp:61:14: note: place parentheses around the assignment to silence this warning
npm ERR!     if(error = dlerror() != NULL) {
npm ERR!              ^
npm ERR!        (                        )
npm ERR! ../src/TwainSession.cpp:61:14: note: use '==' to turn this assignment into an equality comparison
npm ERR!     if(error = dlerror() != NULL) {
npm ERR!              ^
npm ERR!              ==
npm ERR! ../src/TwainSession.cpp:360:30: warning: missing field 'RefCon' initializer [-Wmissing-field-initializers]
npm ERR!     TW_CALLBACK callback = {0};
npm ERR!                              ^
npm ERR! ../src/TwainSession.cpp:524:5: error: no matching function for call to 'strcpy'
npm ERR!     strcpy(fileXfer.FileName, (fileName + ext).c_str());
npm ERR!     ^~~~~~
npm ERR! /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/string.h:79:7: note: candidate function not viable: no known conversion from 'TW_STR255' (aka 'unsigned char[256]') to 'char *' for 1st argument
npm ERR! char    *strcpy(char *__dst, const char *__src);
npm ERR!          ^
npm ERR! ../src/TwainSession.cpp:602:14: warning: unused variable 'bScanStarted' [-Wunused-variable]
npm ERR!         bool bScanStarted = false;
npm ERR!              ^
npm ERR! ../src/TwainSession.cpp:603:13: warning: unused variable 'nBytePerRow' [-Wunused-variable]
npm ERR!         int nBytePerRow = (((imageInfo.ImageWidth * imageInfo.BitsPerPixel) + 7) / 8);
npm ERR!             ^
npm ERR! 9 warnings and 1 error generated.
npm ERR! make: *** [Release/obj.target/twain/src/TwainSession.o] Error 1
npm ERR! gyp ERR! build error 
npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2
npm ERR! gyp ERR! stack     at ChildProcess.onExit (/Users/username/.nvm/versions/node/v18.12.0/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:201:23)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
npm ERR! gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:291:12)
npm ERR! gyp ERR! System Darwin 21.6.0
npm ERR! gyp ERR! command "/Users/username/.nvm/versions/node/v18.12.0/bin/node" "/Users/username/.nvm/versions/node/v18.12.0/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd /Users/username/Documents/projects/node-scanner/node_modules/node-twain
npm ERR! gyp ERR! node -v v18.12.0
npm ERR! gyp ERR! node-gyp -v v9.1.0
npm ERR! gyp ERR! not ok

Failed to get the data source info!

PS D:\twain-test> node test.js
Load Library:TWAINDSM.dll
Triplet:DG_CONTROL / DAT_PARENT / MSG_OPENDSM
RC:TWRC_SUCCESS
Triplet:DG_CONTROL / DAT_IDENTITY / MSG_GETFIRST
RC:TWRC_ENDOFLIST
TWRC_ENDOFLIST7
Triplet:DG_CONTROL / DAT_IDENTITY / MSG_GETDEFAULT
RC:TWRC_FAILURE
---------- ERROR -----------
RC:TWRC_SUCCESS
CC:TWCC_NODS
Failed to get the data source info!
Triplet:DG_CONTROL / DAT_IDENTITY / MSG_GETFIRST
RC:TWRC_ENDOFLIST
TWRC_ENDOFLIST7
sources ---> []
Triplet:DG_CONTROL / DAT_IDENTITY / MSG_GETDEFAULT
RC:TWRC_FAILURE
---------- ERROR -----------
RC:TWRC_SUCCESS
CC:TWCC_NODS
Failed to get the data source info!

What does this error mean?

在我的electron-vite项目中安装报错

我本地还有sqlite和serialport两个库,都可以正常安装编译
node环境:18.19.0 32和64都尝试过
报错信息:

if not defined npm_config_node_gyp (node "C:\Users\MrChildren\AppData\Roaming\nvm\v18.19.0\node_
│ gyp info it worked if it ends with ok
│ gyp info using [email protected]
│ gyp info using [email protected] | win32 | x64
│ gyp info find Python using Python version 3.10.4 found at "C:\Users\MrChildren\AppData\Local\Programs\Python\Python310\python.exe"
│ gyp info find VS using VS2022 (17.6.33723.286) found at:
│ gyp info find VS "C:\Program Files\Microsoft Visual Studio\2022\Enterprise"
│ gyp info find VS run with --verbose for detailed information
│ gyp info spawn C:\Users\MrChildren\AppData\Local\Programs\Python\Python310\python.exe
│ gyp info spawn args [
│ gyp info spawn args 'C:\Users\MrChildren\AppData\Roaming\nvm\v18.19.0\node_modules\pnpm\dist\node_modules\node-gyp\gyp\gyp_main.py',
│ gyp info spawn args 'binding.gyp',
│ gyp info spawn args '-f',
│ gyp info spawn args 'msvs',
│ gyp info spawn args '-I',
│ gyp info spawn args 'D:\opulenDev\windows\flashtest-client\node_modules\node-twain\build\config.gypi',
│ gyp info spawn args '-I',
│ gyp info spawn args 'C:\Users\MrChildren\AppData\Roaming\nvm\v18.19.0\node_modules\pnpm\dist\node_modules\node-gyp\addon.gypi',
│ gyp info spawn args '-I',
│ gyp info spawn args 'C:\Users\MrChildren\AppData\Local\node-gyp\Cache\18.19.0\include\node\common.gypi',
│ gyp info spawn args '-Dlibrary=shared_library',
│ gyp info spawn args '-Dvisibility=default',
│ gyp info spawn args '-Dnode_root_dir=C:\Users\MrChildren\AppData\Local\node-gyp\Cache\18.19.0',
│ gyp info spawn args '-Dnode_gyp_dir=C:\Users\MrChildren\AppData\Roaming\nvm\v18.19.0\node_modules\pnpm\dist\node_modules\node-gyp',
│ gyp info spawn args '-Dnode_lib_file=C:\\Users\\MrChildren\\AppData\\Local\\node-gyp\\Cache\\18.19.0\\<(target_arch)\\node.lib',
│ gyp info spawn args '-Dmodule_root_dir=D:\opulenDev\windows\flashtest-client\node_modules\node-twain',
│ gyp info spawn args '-Dnode_engine=v8',
│ gyp info spawn args '--depth=.',
│ gyp info spawn args '--no-parallel',
│ gyp info spawn args '--generator-output',
│ gyp info spawn args 'D:\opulenDev\windows\flashtest-client\node_modules\node-twain\build',
│ gyp info spawn args '-Goutput_dir=.'
│ gyp info spawn args ]
│ gyp info spawn C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe
│ gyp info spawn args [
│ gyp info spawn args 'build/binding.sln',
│ gyp info spawn args '/clp:Verbosity=minimal',
│ gyp info spawn args '/nologo',
│ gyp info spawn args '/p:Configuration=Release;Platform=x64'
│ gyp info spawn args ]
│ �ڴ˽��������һ������һ����Ŀ����Ҫ���ò������ɣ������ӡ�-m�����ء�
│ main.cpp
│ TwainSession.cpp
│ TwainSDK.cpp
│ win_delay_load_hook.cc
│ LINK : fatal error LNK1181: �޷��������ļ���C:\Users\MrChildren\AppData\Local\node-gyp\Cache\18.19.0\x64\node.lib�� [D:\opulenDev\windows\flashtest-cli
│ gyp ERR! build error
│ gyp ERR! stack Error: C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe failed with exit code: 1
│ gyp ERR! stack at ChildProcess.onExit (C:\Users\MrChildren\AppData\Roaming\nvm\v18.19.0\node_modules\pnpm\dist\node_modules\node-gyp\lib\build.js:203:23)
│ gyp ERR! stack at ChildProcess.emit (node:events:517:28)
│ gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:292:12)
│ gyp ERR! System Windows_NT 10.0.22631
│ gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Users\MrChildren\AppData\Roaming\nvm\v18.19.0\node_modules\pnpm\dist\node_modules\node-g
│ gyp ERR! cwd D:\opulenDev\windows\flashtest-client\node_modules\node-twain
│ gyp ERR! node -v v18.19.0
│ gyp ERR! node-gyp -v v9.3.1
│ gyp ERR! not ok
└─ Failed in 2.9s at D:\opulenDev\windows\flashtest-client\node_modules\node-twain
Progress: resolved 1090, reused 1043, downloaded 1, added 5, done
node_modules/vue-demi: Running postinstall script, done in 143ms
node_modules/vue-echarts/node_modules/vue-demi: Running postinstall script, done in 117ms
 ELIFECYCLE  Command failed with exit code 1.

请问是否可以预先在Win环境中编译好,然后吧node文件拷贝到项目中使用。

因为目前开发环境是Mac,没法装vs,基本编译了几次都是报错。但是在win上面完成了安装,就是能否在win环境下随便创建个文件夹执行npm install node-twain,安装完成后在node_modules里面找到相应的node文件,然后拷贝到项目中使用,项目本身不添加这个依赖。如果可以的话 具体应该拷贝哪些文件,如何引用。

How get 2 side image?

Since I can get the image from both sides, it usually only lets me have one side of the image, but I don't know how to tell it to output both. @Luomusha ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.