Java Scanning and Image Capture

High performance Java scanner access and imaging SDK for Windows 32bit/64bit and Mac OS X

Resources and FAQ's for Java Scanning SDK

    What about JTwain and JSane?

    We'll continue to offer JTwain and JSane. However, there are some limitations with both of them. JTwain can only runs on 32bit JVM. The main reason is most of vendors do not offer any 64bit TWAIN driver. This means even if JTwain is compiled into a 64bit library, no device will ever show up. Currently, you need JSane to access scanners on Linux and Unix operating systems.

    Java versions supported

    Java 1.6 is required. The SDK may work with Java version 1.4 and 1.5, however, these are not officially supported.

    Scan Programmatically

    The typical code flow: 1. Get the device and the functional unit; 2. Set the capabilities – color, bitdepth, paper size, etc; 3. Scan.

    List<Device> devices = ScanManager.getDefaultManager().getDevices(); // list all devices
    String deviceId = ScanManager.getDefaultManager().selectDeviceUsingUI(); // UI select
    Device device = Utils.searchDevice(devices, deviceId); // device object obtained
    
    FunctionalUnit flatbed = device.getFlatbed(); // each device may have multiple functional units
    FunctionalUnit feeder = device.getDocumentFeeder(); // could be null
    
    flatbed.setPixelDataType((FunctionalUnit.PixelDataType.COLOR); // set capabilities
    flatbed.setBitDepth(FunctionalUnit.BitDepth.BIT_DEPTH_24_BITS);
    flatbed.setResolution(300);
    
    functionalUnit.scanOnePage(new File("test.jpg"), new ScanProgressListener() {
      @Override
      public void started() {
        // progressBarScanPureJava.setValue(0);
      }
    
      @Override
      public void progress(final int percent) {
        // progressBarScanPureJava.setValue(percent);
      }
    
      @Override
      public void finished(String error) {
        // progressBarScanPureJava.setValue(100);
      }
    }, null);

    Scan into memory

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    functionalUnit.scanOnePage(outputStream, null, null);
    System.out.println("Size of image scanned is: " + outputStream.size());
    
    byte[] bytes = outputStream.toByteArray();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
    Image image = ImageIO.read(inputStream); // then you can display to UI

    Specify scan output format: TIFF, PDF, JPEG, PNG, etc

    output to PNG:

    functionalUnit.scanOnePage(outputStream, null, new FunctionalUnit.PropertyBuilder()
      .setOutputFormat(FunctionalUnit.OutputFormat.PNG).toString());

    Output to TIFF:

    functionalUnit.scanOnePage(outputStream, null, new FunctionalUnit.PropertyBuilder()
      .setOutputFormat(FunctionalUnit.OutputFormat.TIFF).setTiffCompressionScheme(FunctionalUnit.TiffCompression.CCITT4).toString());

    Output to PDF:

    functionalUnit.scanOnePage(outputStream, null, new FunctionalUnit.PropertyBuilder()
      .setOutputFormat(FunctionalUnit.OutputFormat.PDF).setTiffCompressionScheme(FunctionalUnit.TiffCompression.CCITT4).toString());

    When file size is a concern, do use TIFF/PDF with CCITT Group 4 (T.6) to get ultra small size image files.

    Automatic Document Feeder (ADF) Scan

    Asprise Scanning and Image Capture SDK offers a clean and powerful API for ADF scan. Sample code:

    ScanManager manager = ScanManager.getDefaultManager();
    Device device = manager.getDevices().get(0);
    FunctionalUnit feeder = device.getDocumentFeeder();
    
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    fu.scan(new AbstractScanListener() {
      @Override
      public Object getPageOutputTarget(int pageIndex, long pageId) {
        if(pageIndex <= 2) {
          return outputStream;
        } else { // accept max 3 pages (0, 1, 2).
          return 0;
        }
      }
    },
     new FunctionalUnit.PropertyBuilder()
      .setAdf(true)
      .setOutputFormat(FunctionalUnit.OutputFormat.TIFF)
      .setTiffCompressionScheme(FunctionalUnit.TiffCompression.CCITT4)
      .setMode(FunctionalUnit.Mode.HIGH_SPEED)
      .toString()
    );

    The above code will scan max 3 pages and save the content in TIFF format into the memory. Note you use the getPageOutputTarget method to control output destination or to stop the scan by returning null. Besides streams, you can also return File.

    Back to top