Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**********************************************************************************************
*
* Asprise Scanning and Imaging API
* Copyright (C) 1998-2016. Asprise Inc.
*
* This file is licensed under the GNU Affero General Public License version 3 as published by
* the Free Software Foundation.
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*
* You should have received a copy of the GNU Affero General Public License. If not, please
* visit .
*
**********************************************************************************************/
package com.asprise.imaging.core;
import com.asprise.imaging.core.scan.twain.Source;
import com.asprise.imaging.core.scan.twain.TwainException;
import com.asprise.imaging.core.scan.twain.TwainNative;
import com.asprise.imaging.core.scan.twain.TwainUtil;
import com.asprise.imaging.core.util.JsonUtils;
import com.asprise.imaging.core.util.system.NativeScanLibHelper;
import com.asprise.imaging.core.util.system.Utils;
import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.JSONComposer;
import com.fasterxml.jackson.jr.ob.comp.ArrayComposer;
import com.fasterxml.jackson.jr.ob.comp.ObjectComposer;
import sun.awt.windows.WComponentPeer;
import javax.swing.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.Window;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Imaging API.
*/
public class Imaging {
public static final int LOG_LEVEL_ERROR = 1;
public static final int LOG_LEVEL_WARN = 2;
public static final int LOG_LEVEL_INFO = 3;
public static final int LOG_LEVEL_DEBUG = 4;
public static final String LOG_TO_STDOUT = "stdout";
public static final String LOG_TO_STDERR = "stderr";
public static final String OUTPUT_RETURN_BASE64 = "return-base64";
public static final String OUTPUT_RETURN_BASE64_THUMB = "return-base64-thumbnail";
public static final String OUTPUT_RETURN_HANDLE = "return-handle";
public static final String OUTPUT_RETURN_HANDLE_THUMB = "return-handle-thumbnail";
public static final String OUTPUT_SAVE = "save";
public static final String OUTPUT_SAVE_THUMB = "save-thumbnail";
public static final String OUTPUT_UPLOAD = "upload";
public static final String OUTPUT_UPLOAD_THUMB = "upload-thumbnail";
public static final String FORMAT_JPG = "jpg";
public static final String FORMAT_PNG = "png";
public static final String FORMAT_BMP = "bmp";
public static final String FORMAT_TIF = "tif";
public static final String FORMAT_PDF = "pdf";
public static final String TIFF_COMPRESSION_CCITT_G3 = "G3";
public static final String TIFF_COMPRESSION_CCITT_G4 = "G4";
public static final String TIFF_COMPRESSION_LZW = "LZW";
public static final String TIFF_COMPRESSION_RLE = "RLE";
public static final String TIFF_COMPRESSION_NONE = "NONE";
public static final String TIFF_COMPRESSION_PACKBITS = "PACKBITS";
public static final String TIFF_COMPRESSION_ZIP = "ZIP";
public static final String EXIF_NAME_DocumentName = "DocumentName";
public static final String EXIF_NAME_ImageDescription = "ImageDescription";
public static final String EXIF_NAME_EquipMake = "EquipMake";
public static final String EXIF_NAME_EquipModel = "EquipModel";
// public static final String EXIF_NAME_SoftwareUsed = "SoftwareUsed"
/** Limit length to max 20 */
// public static final String EXIF_NAME_DateTime "DateTime"
public static final String EXIF_NAME_Copyright = "Copyright";
public static final String EXIF_NAME_UserComment = "UserComment";
static {
try {
NativeScanLibHelper.loadScanLib();
} catch (Throwable t) {
System.err.println("Unable to load native library: " + t);
t.printStackTrace();
}
}
String appId;
int windowHandle;
public Imaging(String appId, int windowHandle) {
this.appId = appId;
this.windowHandle = windowHandle;
}
public Imaging(Component owningUI) {
this.appId = "Java";
this.windowHandle = (int)getOwningWindowHandle(owningUI);
}
private static ExecutorService executorServiceForScanning;
/** Use this executor service to make sure that all scanning related code is executed from the same thread. */
public static ExecutorService getDefaultExecutorServiceForScanning() {
if(executorServiceForScanning == null) {
synchronized (Imaging.class) {
if(executorServiceForScanning == null) {
executorServiceForScanning = Executors.newSingleThreadExecutor(new ThreadFactory() { // custom factory for user-friendly thread name
final AtomicInteger threadNumber = new AtomicInteger(1);
ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
@Override
public Thread newThread(Runnable r) {
Thread thread = defaultThreadFactory.newThread(r);
thread.setName("scan" + (threadNumber.get() == 1 ? "" : "-" + threadNumber));
return thread;
}
});
}
}
}
return executorServiceForScanning;
}
/**
* Executes and wait indefinitely until the result is returned or exception occurs
* @param callable
* @param
* @return
* @throws Throwable in case of exeception occurred during execution
*/
public static R executeInDefaultExecutorServiceAndWaitTillReturn(Callable callable) throws Throwable {
List> list = new ArrayList>();
list.add(callable);
try {
List> futures = getDefaultExecutorServiceForScanning().invokeAll(list);
Future returned = futures.get(0);
return returned.get();
} catch (Throwable e) {
if(e instanceof ExecutionException) {
throw ((ExecutionException)e).getCause();
} else {
throw e;
}
}
}
/**
* Performs scanning from a device and output (return, save, and/or upload).
* @param request scan request object.
* @param sourceName the exact source name or "select" to prompt dialog selection; "default" to use default source; "current" refers to current opened source if any.
* @param showUI set to true to use scanner UI or false to hide the UI. Set to true for maximum compatibility.
* @param modalUI whether the scanner UI should be modal. Set to to true if you are not sure.
* @return Scan result or null if user cancels.
* @throws TwainException if failed.
*/
public Result scan(Request request, String sourceName, boolean showUI, boolean modalUI) {
return scan(JsonUtils.jsonSerialize(request.toJsonObject(), true), sourceName, showUI, modalUI);
}
/**
* Performs scanning from a device and output (return, save, and/or upload).
* @param scanRequestInJson scan request in JSON format.
* @param sourceName the exact source name or "select" to prompt dialog selection; "default" to use default source; "current" refers to current opened source if any.
* @param showUI set to true to use scanner UI or false to hide the UI. Set to true for maximum compatibility.
* @param modalUI whether the scanner UI should be modal. Set to to true if you are not sure.
* @return Scan result or null if user cancels.
* @throws TwainException if failed.
*/
public Result scan(String scanRequestInJson, String sourceName, boolean showUI, boolean modalUI) {
ensureScanFuncsCallInTheSameThread();
String rawResult = scanAndReturnRaw(scanRequestInJson, sourceName, showUI, modalUI);
if(rawResult != null && rawResult.startsWith(" root = JSON.std.mapFrom(rawResult);
Result r = Result.createScanResult(root);
return r;
} catch (Throwable t) {
throw new TwainException(rawResult, t);
}
}
/**
* Performs scanning from a device and output result in JSON.
* @param scanRequestInJson scan request in JSON format.
* @param sourceName the exact source name or "select" to prompt dialog selection; "default" to use default source; "current" refers to current opened source if any.
* @param showUI set to true to use scanner UI or false to hide the UI. Set to true for maximum compatibility.
* @param modalUI whether the scanner UI should be modal. Set to to true if you are not sure.
* @return Scan result in JSON or null if user cancels.
* @throws TwainException if failed.
*/
public String scanAndReturnRaw(String scanRequestInJson, String sourceName, boolean showUI, boolean modalUI) {
ensureScanFuncsCallInTheSameThread();
String rawResult = callNativeFunc(TwainNative.FUNC_twain_scan, appId,
// FUJITSU fi-5120Cdj waits forever if we use windowHandle (from JFrame) here
0, // windowHandle,
scanRequestInJson, sourceName, showUI, modalUI);
if(rawResult != null && rawResult.contains("failed to open data source: TWRC_CANCEL")) {
return null;
}
return rawResult;
}
/**
* Performs image conversion and output (return, save, and/or upload).
* @param request scan request object.
* @return Scan result or null if user cancels.
* @throws TwainException if failed.
*/
public Result convert(Request request) {
String requestInJson = JsonUtils.jsonSerialize(request.toJsonObject(), true);
String result = callNativeFunc(TwainNative.FUNC_image_output, requestInJson);
if(result != null && result.startsWith(" root = JSON.std.mapFrom(result);
return Result.createScanResult(root);
} catch (Throwable t) {
throw new TwainException(result, t);
}
}
/**
* Get information about the image, e.g. bytes, width, height, etc.
* @param imageFile Path to the image file.
* @return Information as map
* @throws TwainException if failed.
*/
public Map getImageInfo(String imageFile) {
String result = callNativeFunc(TwainNative.FUNC_image_info, imageFile);
try {
Map root = JSON.std.mapFrom(result);
return root;
} catch (Throwable t) {
throw new TwainException(result, t);
}
}
/**
* Performs operations on image, e.g., rotate, crop, scale, gray, etc.
* @param inputImageFile Path to the input file.
* @param commands Processing commands
* @param outputImageFile Path to the output file.
* @return Information as map
* @throws TwainException if failed.
*/
public Map processImage(String inputImageFile, String commands, String outputImageFile) {
String result = callNativeFunc(TwainNative.FUNC_image_process, inputImageFile, commands, outputImageFile);
try {
Map root = JSON.std.mapFrom(result);
return root;
} catch (Throwable t) {
throw new TwainException(result, t);
}
}
/**
* Retrieve list of sources (i.e., devices) optionally with caps; the default source has "default": true in JSON format.
* @param nameOnly if true, return list of device names separated by ',' otherwise return device info in JSON format.
* @param capsToRetrieve only effective if nameOnly is false - If set, return JSON string; can be cap name or code separated by comma or 'all' to list all caps supported.
* @param detectDeviceType detect whether the device has ADF and/or flatbed.
* @param excludeTwainDsOnWia exclude WIA synthesized sources
* @return JSON or comma separated string depending on nameOnly.
*/
public List