Using Dynamic Web TWAIN integrate with angular 8

1.1k views Asked by At

I am using Dynamsoft Web TWAIN In My Scanner.I am getting error with bellow code,

Html Code -

<button (click)="acquireImage()">Scan Document</button>
<div id="dwtcontrolContainer"></div>

Angular code -

acquireImage(): void {
  const dwObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
  dwObject.IfShowIndicator = false;
  const bSelected = dwObject.SelectSource();
  if (bSelected) {
    const onAcquireImageSuccess = () => { dwObject.CloseSource(); };
    const onAcquireImageFailure = onAcquireImageSuccess;
    dwObject.OpenSource();
    dwObject.AcquireImage({}, onAcquireImageSuccess, onAcquireImageFailure);
  }

}

1

There are 1 answers

0
yushulx On

The namespace of Dynamic Web TWAIN has been changed. You can refer to the latest sample code https://github.com/Dynamsoft/dwt-angular-simple/blob/master/src/app/dwt/dwt.component.ts

Initialize Dynamic Web TWAIN Object:

ngOnInit(): void {
 Dynamsoft.DWT.Containers = [{ WebTwainId: 'dwtObject', ContainerId: this.containerId, Width: '300px', Height: '400px' }];
 Dynamsoft.DWT.RegisterEvent('OnWebTwainReady', () => { this.Dynamsoft_OnReady(); });                                                        
 Dynamsoft.DWT.ResourcesPath = 'assets/dwt-resources';
   Dynamsoft.DWT.ProductKey = 't00891wAAAKBfWo4sRRVNTyLqdC7nKomEJIfBYqfXWg5mblnP0eeJi+LsMIUdQvrBf//ocS3z8MJA47R4VdO4x24uJwlqKgkuZOa7BUQHPkFNA5hFSi6lG2qOK6I=';
 let checkScript = () => {
   if (Dynamsoft.Lib.detect.scriptLoaded) {
     Dynamsoft.DWT.Load();
   } else {
     setTimeout(() => checkScript(), 100);
   }
 };
 checkScript();
}

Dynamsoft_OnReady(): void {
 this.DWObject = Dynamsoft.DWT.GetWebTwain(this.containerId);
 this.bWASM = Dynamsoft.Lib.env.bMobile || !Dynamsoft.DWT.UseLocalService;
 if (this.bWASM) {
   this.DWObject.Viewer.cursor = "pointer";
 } else {
   let sources = this.DWObject.GetSourceNames();
   this.selectSources = <HTMLSelectElement>document.getElementById("sources");
   this.selectSources.options.length = 0;
   for (let i = 0; i < sources.length; i++) {
     this.selectSources.options.add(new Option(<string>sources[i], i.toString()));
   }
 }
}

Acquire images from scanners:

acquireImage(): void {
    if (!this.DWObject)
      this.DWObject = Dynamsoft.DWT.GetWebTwain();
    if (this.bWASM) {
      alert("Scanning is not supported under the WASM mode!");
    }
    else if (this.DWObject.SourceCount > 0 && this.DWObject.SelectSourceByIndex(this.selectSources.selectedIndex)) {
      const onAcquireImageSuccess = () => { this.DWObject.CloseSource(); };
      const onAcquireImageFailure = onAcquireImageSuccess;
      this.DWObject.OpenSource();
      this.DWObject.AcquireImage({}, onAcquireImageSuccess, onAcquireImageFailure);
    } else {
      alert("No Source Available!");
    }
  }