@@ -2,12 +2,12 @@ import { Injectable } from '@angular/core';
22import { Subject } from 'rxjs' ;
33import { environment } from './../environments/environment' ;
44import Dynamsoft from 'dwt' ;
5- import { DWTInitialConfig } from 'dwt/Dynamsoft' ;
65import { DynamsoftEnums } from 'dwt/Dynamsoft.Enum' ;
76import { WebTwain } from 'dwt/WebTwain' ;
87import { DeviceConfiguration , ScanSetup } from 'dwt/WebTwain.Acquire' ;
98import { RuntimeSettings , TextResults , TextResult } from 'dwt/Addon.BarcodeReader' ;
109import { OCRPro , Rect } from 'dwt/Addon.OCRPro' ;
10+ import { DWTInitialConfig } from 'dwt/Dynamsoft' ;
1111
1212@Injectable ( {
1313 providedIn : 'root'
@@ -45,7 +45,11 @@ export class DwtService {
4545 * Camera
4646 */
4747 protected _useCamera : boolean ;
48- public bCameraAddonUsable : boolean = false ;
48+ /**
49+ * The Webcam Addon vai DirectShow only works for Service mode on Desktop (dwt@16.1.1)
50+ * Otherwise, Camera Addon is used.
51+ */
52+ public bUseCameraViaDirectShow : boolean = false ;
4953 public cameraOptions = [ ] ;
5054 /**
5155 * Barcode
@@ -163,29 +167,6 @@ export class DwtService {
163167 this . mountDWT ( ) ;
164168 } ;
165169 }
166- /**
167- * To make dynamsoft.webtwain.install.js compatible with Angular
168- */
169- modulizeInstallJS ( ) {
170- let _DWT_Reconnect = ( < any > window ) . DWT_Reconnect ;
171- ( < any > window ) . DWT_Reconnect = ( ...args ) => _DWT_Reconnect . call ( { Dynamsoft : Dynamsoft } , ...args ) ;
172- let __show_install_dialog = ( < any > window ) . _show_install_dialog ;
173- ( < any > window ) . _show_install_dialog = ( ...args ) => __show_install_dialog . call ( { Dynamsoft : Dynamsoft } , ...args ) ;
174- let _OnWebTwainOldPluginNotAllowedCallback = ( < any > window ) . OnWebTwainOldPluginNotAllowedCallback ;
175- ( < any > window ) . OnWebTwainOldPluginNotAllowedCallback = ( ...args ) => _OnWebTwainOldPluginNotAllowedCallback . call ( { Dynamsoft : Dynamsoft } , ...args ) ;
176- let _OnWebTwainNeedUpgradeCallback = ( < any > window ) . OnWebTwainNeedUpgradeCallback ;
177- ( < any > window ) . OnWebTwainNeedUpgradeCallback = ( ...args ) => _OnWebTwainNeedUpgradeCallback . call ( { Dynamsoft : Dynamsoft } , ...args ) ;
178- let _OnWebTwainPreExecuteCallback = ( < any > window ) . OnWebTwainPreExecuteCallback ;
179- ( < any > window ) . OnWebTwainPreExecuteCallback = ( ...args ) => _OnWebTwainPreExecuteCallback . call ( { Dynamsoft : Dynamsoft } , ...args ) ;
180- let _OnWebTwainPostExecuteCallback = ( < any > window ) . OnWebTwainPostExecuteCallback ;
181- ( < any > window ) . OnWebTwainPostExecuteCallback = ( ...args ) => _OnWebTwainPostExecuteCallback . call ( { Dynamsoft : Dynamsoft } , ...args ) ;
182- let _OnRemoteWebTwainNotFoundCallback = ( < any > window ) . OnRemoteWebTwainNotFoundCallback ;
183- ( < any > window ) . OnRemoteWebTwainNotFoundCallback = ( ...args ) => _OnRemoteWebTwainNotFoundCallback . call ( { Dynamsoft : Dynamsoft } , ...args ) ;
184- let _OnRemoteWebTwainNeedUpgradeCallback = ( < any > window ) . OnRemoteWebTwainNeedUpgradeCallback ;
185- ( < any > window ) . OnRemoteWebTwainNeedUpgradeCallback = ( ...args ) => _OnRemoteWebTwainNeedUpgradeCallback . call ( { Dynamsoft : Dynamsoft } , ...args ) ;
186- let _OnWebTWAINDllDownloadFailure = ( < any > window ) . OnWebTWAINDllDownloadFailure ;
187- ( < any > window ) . OnWebTWAINDllDownloadFailure = ( ...args ) => _OnWebTWAINDllDownloadFailure . call ( { Dynamsoft : Dynamsoft } , ...args ) ;
188- }
189170 mountDWT ( UseService ?: boolean ) : Promise < any > {
190171 this . _DWObject = null ;
191172 return new Promise ( ( res , rej ) => {
@@ -199,17 +180,16 @@ export class DwtService {
199180 */
200181 let checkScript = ( ) => {
201182 if ( Dynamsoft . Lib . detect . scriptLoaded ) {
202- this . modulizeInstallJS ( ) ;
203- if ( UseService !== undefined )
204- Dynamsoft . WebTwainEnv . UseLocalService = UseService ;
205- else {
206- Dynamsoft . WebTwainEnv . UseLocalService = this . bUseService ;
183+ if ( this . runningEnvironment . bMobile ) {
184+ Dynamsoft . WebTwainEnv . UseLocalService = false ;
185+ } else {
186+ if ( UseService !== undefined )
187+ Dynamsoft . WebTwainEnv . UseLocalService = UseService ;
188+ else {
189+ Dynamsoft . WebTwainEnv . UseLocalService = this . bUseService ;
190+ }
207191 }
208192 this . bWASM = this . runningEnvironment . bMobile || ! Dynamsoft . WebTwainEnv . UseLocalService ;
209- /**
210- * The Camera Addon only works for Service mode on Desktop at present (dwt@16.0.0)
211- */
212- this . bCameraAddonUsable = ! this . bWASM && this . runningEnvironment . bWin ;
213193 Dynamsoft . WebTwainEnv . CreateDWTObjectEx (
214194 dwtInitialConfig ,
215195 ( _DWObject ) => {
@@ -296,26 +276,41 @@ export class DwtService {
296276 /**
297277 * Retrieve all devices (scanners + cameras).
298278 */
299- getDevices ( ) {
300- let _dwt = this . _DWObject ;
301- if ( this . _DWObjectEx )
302- _dwt = this . _DWObjectEx ;
303- this . devices = [ ] ;
304- let count = this . _DWObject . SourceCount ;
305- let _scanners = < string [ ] > ( this . _DWObject . GetSourceNames ( ) ) ;
306- if ( count !== _scanners . length ) {
307- console . log ( 'Possible wrong source count!' ) ; //not likely to happen
308- }
309- for ( let i = 0 ; i < _scanners . length ; i ++ ) {
310- this . devices . push ( { name : ( i + 1 ) . toString ( ) + "." + _scanners [ i ] , realName : _scanners [ i ] , type : "scanner" } ) ;
311- }
312- this . _scannersCount = this . devices . length ;
313- let _cameras = _dwt . Addon . Webcam . GetSourceList ( ) ;
314- for ( let i = 0 ; i < _cameras . length ; i ++ ) {
315- this . devices . push ( { name : ( i + 1 ) . toString ( ) + "." + _cameras [ i ] , realName : _cameras [ i ] , type : "camera" } ) ;
316- }
317- console . log ( this . devices ) ;
318- return this . devices ;
279+ getDevices ( ) : Promise < Device [ ] > {
280+ return new Promise ( ( res , rej ) => {
281+ let _dwt = this . _DWObject ;
282+ if ( this . _DWObjectEx )
283+ _dwt = this . _DWObjectEx ;
284+ this . devices = [ ] ;
285+ let count = this . _DWObject . SourceCount ;
286+ let _scanners = < string [ ] > ( this . _DWObject . GetSourceNames ( ) ) ;
287+ if ( count !== _scanners . length ) {
288+ rej ( 'Possible wrong source count!' ) ; //not likely to happen
289+ }
290+ for ( let i = 0 ; i < _scanners . length ; i ++ ) {
291+ this . devices . push ( { deviceId : Math . floor ( Math . random ( ) * 100000 ) . toString ( ) , name : ( i + 1 ) . toString ( ) + "." + _scanners [ i ] , label : _scanners [ i ] , type : "scanner" } ) ;
292+ }
293+ this . _scannersCount = this . devices . length ;
294+ if ( this . bUseCameraViaDirectShow ) {
295+ try {
296+ let _cameras = _dwt . Addon . Webcam . GetSourceList ( ) ;
297+ for ( let i = 0 ; i < _cameras . length ; i ++ ) {
298+ this . devices . push ( { deviceId : Math . floor ( Math . random ( ) * 100000 ) . toString ( ) , name : ( i + 1 ) . toString ( ) + "." + _cameras [ i ] , label : _cameras [ i ] , type : "camera" } ) ;
299+ }
300+ res ( this . devices ) ;
301+ } catch ( e ) {
302+ rej ( e ) ;
303+ }
304+ } else {
305+ _dwt . Addon . Camera . getSourceList ( )
306+ . then ( _cameras => {
307+ for ( let i = 0 ; i < _cameras . length ; i ++ ) {
308+ this . devices . push ( { deviceId : _cameras [ i ] . deviceId , name : ( i + 1 ) . toString ( ) + "." + _cameras [ i ] . label , label : _cameras [ i ] . label , type : "camera" } ) ;
309+ }
310+ res ( this . devices ) ;
311+ } , err => rej ( err ) ) ;
312+ }
313+ } ) ;
319314 }
320315 /**
321316 * Retrieve detailed information of the devices.
@@ -329,6 +324,7 @@ export class DwtService {
329324 */
330325 selectADevice ( name : string ) : Promise < boolean > {
331326 return new Promise ( ( res , rej ) => {
327+ let waitForAnotherPromise = false ;
332328 this . _selectedDevice = "" ;
333329 this . _useCamera = false ;
334330 if ( this . devices . length === 0 )
@@ -339,14 +335,34 @@ export class DwtService {
339335 let _dwt = this . _DWObject ;
340336 if ( this . _DWObjectEx )
341337 _dwt = this . _DWObjectEx ;
342- _dwt . Addon . Webcam . StopVideo ( ) ;
343- if ( _dwt . Addon . Webcam . SelectSource ( value . realName ) ) {
344- this . _selectedDevice = name ;
345- this . _useCamera = true ;
346- this . updateCameraValues ( _dwt ) ;
338+ if ( this . bUseCameraViaDirectShow ) {
339+ _dwt . Addon . Webcam . StopVideo ( ) ;
340+ if ( _dwt . Addon . Webcam . SelectSource ( value . label ) ) {
341+ this . _selectedDevice = name ;
342+ this . _useCamera = true ;
343+ this . updateCameraValues ( _dwt ) ;
344+ }
345+ else {
346+ rej ( "Can't use the Webcam " + name + ", please make sure it's not in use!" )
347+ }
347348 }
348349 else {
349- rej ( "Can't use the Webcam " + name + ", please make sure it's not in use!" )
350+ waitForAnotherPromise = true ;
351+ _dwt . Addon . Camera . selectSource ( value . deviceId )
352+ . then ( deviceInfo => {
353+ this . _selectedDevice = name ;
354+ this . _useCamera = true ;
355+ if ( this . _selectedDevice !== "" ) {
356+ this . generalSubject . next ( { type : "deviceName" , deviceName : this . _selectedDevice } ) ;
357+ res ( true ) ;
358+ }
359+ else
360+ res ( false ) ;
361+ } ,
362+ ( ) => {
363+ rej ( "Can't use the Webcam " + name + ", please make sure it's not in use!" )
364+ }
365+ )
350366 }
351367 }
352368 else {
@@ -356,12 +372,14 @@ export class DwtService {
356372 }
357373 }
358374 } ) ;
359- if ( this . _selectedDevice !== "" ) {
360- this . generalSubject . next ( { type : "deviceName" , deviceName : this . _selectedDevice } ) ;
361- res ( true ) ;
375+ if ( ! waitForAnotherPromise ) {
376+ if ( this . _selectedDevice !== "" ) {
377+ this . generalSubject . next ( { type : "deviceName" , deviceName : this . _selectedDevice } ) ;
378+ res ( true ) ;
379+ }
380+ else
381+ res ( false ) ;
362382 }
363- else
364- res ( false ) ;
365383 } ) ;
366384 }
367385 /**
@@ -437,20 +455,25 @@ export class DwtService {
437455 return new Promise ( ( res , rej ) => {
438456 if ( this . _selectedDevice !== "" ) {
439457 if ( this . _useCamera ) {
440- if ( config === undefined ) {
441- if ( this . _DWObjectEx ) {
458+ if ( this . _DWObjectEx ) {
459+ if ( this . bUseCameraViaDirectShow ) {
442460 this . _DWObjectEx . Addon . Webcam . CaptureImage ( ( ) => {
443461 this . getBlob ( [ 0 ] , Dynamsoft . EnumDWT_ImageType . IT_PNG , this . _DWObjectEx )
444462 . then ( blob => this . _DWObject . LoadImageFromBinary ( blob , ( ) => {
445463 this . _DWObjectEx . RemoveImage ( 0 ) ;
446464 res ( true ) ;
447465 } , ( errCode , errString ) => rej ( errString ) ) ) ;
448466 } , ( errCode , errStr ) => rej ( errStr ) ) ;
449- } else {
450- rej ( "No WebTwain instanance for camera capture!" ) ;
467+ }
468+ else {
469+ this . _DWObjectEx . Addon . Camera . capture ( )
470+ . then ( blob => this . _DWObject . LoadImageFromBinary ( blob , ( ) => {
471+ this . _DWObjectEx . RemoveImage ( 0 ) ;
472+ res ( true ) ;
473+ } , ( errCode , errString ) => rej ( errString ) ) ) ;
451474 }
452475 } else {
453- rej ( "Please select a scanner first !" ) ;
476+ rej ( "No WebTwain instanance for camera capture !" ) ;
454477 }
455478 } else {
456479 this . _DWObject . SetOpenSourceTimeout ( 3000 ) ;
@@ -1073,8 +1096,9 @@ export class DwtService {
10731096 }
10741097}
10751098export interface Device {
1099+ deviceId : string ,
10761100 name : string ,
1077- realName : string ,
1101+ label : string ,
10781102 type : string
10791103}
10801104
0 commit comments