B4J Question Twain from java

vpires

Member
Licensed User
Longtime User
Hello,
i need to add scanning to my app, so i was looking into some java libs to do it, but i don't have a scanner to try it....
can someone with one test this code, to see if it's worth the time to wrap it into a b4j lib

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
    #AdditionalJar:uk.co.mmscomputing.device.twain.jar
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
 End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    
    Private NativeMe As JavaObject
    NativeMe.InitializeNewInstance("b4j.example.main.TwainExample",Null)
    
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

#If Java
    import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

import uk.co.mmscomputing.device.scanner.Scanner;
import uk.co.mmscomputing.device.scanner.ScannerDevice;
import uk.co.mmscomputing.device.scanner.ScannerListener;
import uk.co.mmscomputing.device.scanner.ScannerIOException;
import uk.co.mmscomputing.device.scanner.ScannerIOMetadata;

public static class TwainExample implements ScannerListener{

  //static TwainExample app; 

  Scanner scanner;

  public TwainExample() throws ScannerIOException{
    scanner=Scanner.getDevice();
    scanner.addListener(this);   
    scanner.acquire();
  }

  public void update(ScannerIOMetadata.Type type, ScannerIOMetadata metadata){
    if(type.equals(ScannerIOMetadata.ACQUIRED)){
      BufferedImage image=metadata.getImage();
      System.out.println("Have an image now!");
      try{
        ImageIO.write(image, "png", new File("mmsc_image.png"));
      }catch(Exception e){
        e.printStackTrace();
      }
    }else if(type.equals(ScannerIOMetadata.NEGOTIATE)){
      ScannerDevice device=metadata.getDevice();
      try{
//        device.setShowUserInterface(true);
//        device.setShowProgressBar(true);
//        device.setResolution(100);
      }catch(Exception e){
        e.printStackTrace();
      }
    }else if(type.equals(ScannerIOMetadata.STATECHANGE)){
      System.err.println(metadata.getStateStr());
      if(metadata.isFinished()){
        System.exit(0);
      }
    }else if(type.equals(ScannerIOMetadata.EXCEPTION)){
      metadata.getException().printStackTrace();
    }
  }
}
#End If

Also this jar in required in b4j additional libs folder



TIA

Nelson
 

Attachments

  • uk.co.mmscomputing.device.twain.jar
    196.7 KB · Views: 326

amykonio

Active Member
Licensed User
Longtime User
Hi.

Here it doesn't work.

From the Log:
Waiting for debugger to connect...
Program started.
JarLib.load: Successfully loaded library [jar:file:/G:/Develop/B4J/AdditionalLibs/uk.co.mmscomputing.device.twain.jar!/uk/co/mmscomputing/device/twain/win64/jtwain.dll] from jar file location
(TwainIOException) uk.co.mmscomputing.device.twain.TwainIOException: Cannot load Twain Source Manager.


Using B4J v.7.00, jdk1.8.0_121...
Andreas.
 
Upvote 0

rboeck

Well-Known Member
Licensed User
Longtime User
Hi, i got another result:
Program started.
JarLib.load: Successfully loaded library [jar:file:/C:/B4J/B4J%20Libraries/uk.co.mmscomputing.device.twain.jar!/uk/co/mmscomputing/device/twain/win64/jtwain.dll] from jar file location
Source Manager Open
Source Manager Open
uk.co.mmscomputing.device.twain.TwainIOException: uk.co.mmscomputing.device.twain.TwainScanner.setException:
Failed during call to twain source.
cc=No Data Source
rc=1
at uk.co.mmscomputing.device.twain.TwainScanner.signalException(TwainScanner.java:131)
at uk.co.mmscomputing.device.twain.jtwain.signalException(jtwain.java:287)
at uk.co.mmscomputing.device.twain.jtwain.cbexecute(jtwain.java:243)
at uk.co.mmscomputing.device.twain.jtwain.nstart(Native Method)
at uk.co.mmscomputing.device.twain.jtwain.access$200(jtwain.java:12)
at uk.co.mmscomputing.device.twain.jtwain$1.run(jtwain.java:338)
uk.co.mmscomputing.device.twain.TwainFailureException: Failed during call to twain source.

B4J v.7.00, jdk 1.8.0_181

Reinhard
 
Upvote 0

amykonio

Active Member
Licensed User
Longtime User

Hi. Actually this should be the same problem that shows to me. Simply I put the following in a try catch:
B4X:
Private NativeMe As JavaObject
NativeMe.InitializeNewInstance("b4j.example.main.TwainExample",Null)

Andreas.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…