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 - http://asprise.com/document-scanner-image-pdf/java-scanning-api-overview.html
* Copyright (C) 1998-2018. 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.prefs.Base64;
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.TranslationProps;
import com.asprise.imaging.core.util.system.NativeScanLibHelper;
import com.asprise.imaging.core.util.system.StringUtils;
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 javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.List;
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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Imaging API.
*/
public class Imaging {
/** Turn of logging */
public static final int LOG_LEVEL_OFF = 0;
public static final int LOG_LEVEL_ERROR = 1;
public static final int LOG_LEVEL_WARN = 2;
/** Info: the default. */
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";
/** Operating system name. */
public static final String SYSTEM_INFO_OS = "os";
/** User's preferred locale ID (LCID), e.g, 1033; result of GetUserDefaultLCID() */
public static final String SYSTEM_INFO_USER_DEFAULT_LC_ID = "user_default_lc_id";
/** User's preferred locale code (LCID), e.g, 'en_US'; result of GetUserDefaultLCID() */
public static final String SYSTEM_INFO_USER_DEFAULT_LC_CODE = "user_default_lc_code";
/** User's preferred language ID (LANGID), e.g, 1033; result of GetUserDefaultUILanguage() */
public static final String SYSTEM_INFO_USER_DEFAULT_UI_LANG_ID = "user_default_ui_lang_id";
/** User's preferred language ID (LANGID), e.g, 'en_US'; result of GetUserDefaultUILanguage() */
public static final String SYSTEM_INFO_USER_DEFAULT_UI_LANG_CODE = "user_default_ui_lang_code";
/** the language of the installed OS, e.g, 1033; result of GetUserDefaultLangID() */
public static final String SYSTEM_INFO_OS_LANG_ID = "user_default_lang_id";
/** the language of the installed OS, e.g, 'en_US'; result of GetUserDefaultLangID() */
public static final String SYSTEM_INFO_OS_LANG_CODE = "user_default_lang_code";
/** User geo id of nation, e.g, 244; result of GetUserGeoID(GEOCLASS_NATION) */
public static final String SYSTEM_INFO_GEO_NATION_ID = "user_geo_nation_id";
/** User geo name of nation, e.g, 'US; United States'; result of GetUserGeoID(GEOCLASS_NATION) */
public static final String SYSTEM_INFO_GEO_NATION_NAME = "user_geo_nation_name";
/** User geo id of region; result of GetUserGeoID(GEOCLASS_REGION) */
public static final String SYSTEM_INFO_GEO_REGION_ID = "user_geo_region_id";
/** User geo id of region; result of GetUserGeoID(GEOCLASS_REGION) */
public static final String SYSTEM_INFO_GEO_REGION_NAME = "user_geo_region_name";
/** daylight | standard | <error: ...>; result of GetTimeZoneInformation */
public static final String SYSTEM_INFO_TIMEZONE_TYPE = "timezone_type";
/** TIME_ZONE_INFORMATION.DaylightName or TIME_ZONE_INFORMATION.StandardName or null depending on tz type. */
public static final String SYSTEM_INFO_TIMEZONE_NAME = "timezone_name";
/** TIME_ZONE_INFORMATION.Bias: bias in minutes for local time translation. UTC = local time + bias */
public static final String SYSTEM_INFO_TIMEZONE_BIAS = "timezone_bias";
/** TWAIN data source manager version */
public static final String SYSTEM_INFO_TWAIN_VERSION = "twain_version";
/** Default paper size: letter | legal | a3 | a4 */
public static final String SYSTEM_INFO_DEFAULT_PAPER_SIZE = "default_paper_size";
/** Measurement system: metric | us */
public static final String SYSTEM_INFO_MEASUREMENT_SYSTEM = "measurement_system";
static {
try {
NativeScanLibHelper.loadScanLib();
} catch (Throwable t) {
System.err.println("Unable to load native library: " + t);
t.printStackTrace();
JOptionPane.showMessageDialog(null, "Error: " + t.getMessage(), "Unable to load native library", JOptionPane.ERROR_MESSAGE);
}
}
String appId;
int windowHandle;
/** Invoking thread id. */
private volatile long it;
/** Flex license. */
private String license = "";
private static Object globalClientConnectionObj;
/** Whether to use Asprise source select UI instead of system UI for "select" */
private Boolean useAspriseSourceSelectUI;
public Imaging(String appId, int windowHandle) {
this.appId = appId;
this.windowHandle = windowHandle;
this.it = Thread.currentThread().getId();
license = System.getProperty("GLOBAL_FLEX_LICENSE");
if(globalClientConnectionObj != null) {
TwainNative.onClientConnected(globalClientConnectionObj);
}
}
/** Allows ScanServer to reset properly. */
public void resetIt() {
this.it = Thread.currentThread().getId();
}
private Component owningComponent;
public Imaging(Component owningUI) {
this("Java", (int) Utils.getOwningWindowHandle(owningUI));
this.owningComponent = owningUI;
}
public void setLicense(String license) {
this.license = license;
}
public String getLicense() {
return license;
}
public Imaging setUseAspriseSourceSelectUI(boolean value) {
this.useAspriseSourceSelectUI = value;
return this;
}
/**
* Whether to use Asprise Swing based source select UI; default to true.
* Set system property use_asprise_source_select_ui to false to turn off or explicitly set it.
* @return
*/
public boolean getUseAspriseSourceSelectUI() {
if(!Utils.isEval(this) && Utils.getLicenseType(this) <= Utils.LIC_TYPE_STD) { // Only PRO and ENT are allowed.
return false;
}
if(useAspriseSourceSelectUI != null) { // explicitly specified.
return useAspriseSourceSelectUI;
}
String propKey = "use_asprise_source_select_ui";
String propValue = System.getProperty(propKey);
if(StringUtils.isEmpty(propValue)) {
propValue = System.getenv(propKey);
}
if(StringUtils.isEmpty(propValue)) {
return true;
} else {
return ! (propValue.equalsIgnoreCase("false") || propValue.equalsIgnoreCase("0"));
}
}
/**
* Performs swing based source selection is sourceName is "select" and swing based source select UI is enabled.
* @param sourceName
* @return name of the source selected or "select" if user cancels.
*/
public String doSelectSourceIfUsingAspriseSelectUI(String sourceName) {
if(! ("select".equalsIgnoreCase(sourceName) && getUseAspriseSourceSelectUI())) {
return sourceName;
}
String result = doSelectSourceUsingAspriseSelectUI();
return result == null ? sourceName : result;
}
private String doSelectSourceUsingAspriseSelectUI() {
try {
Class> cls = null;
cls = Class.forName("com.asprise.imaging.scan.ui.workbench.ScannerSelectUI");
Method meth = cls.getMethod("select", Component.class, String.class, Properties.class);
String result = (String) meth.invoke(null, owningComponent, license, i18n); // static method doesn't have an instance
return result;
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}
/** To be used by scanner select UI only */
Properties i18n;
/** Sets to i18n to be used by ScanSelectUI */
public void setI18n(Properties i18n) {
if(Utils.isEnterpriseOrEvaluation(this)) {
this.i18n = i18n;
} else {
if(i18n != null && !(i18n.size() == 1 && "en".equalsIgnoreCase(i18n.getProperty("lang")))) {
System.err.println("i18n is not supported under the current license: " + i18n);
}
}
}
public static void setGlobalClientConnectionObj(Object clientConnectionObj) {
globalClientConnectionObj = clientConnectionObj;
}
public void setOwning(Component owningUI) {
this.windowHandle = (int)Utils.getOwningWindowHandle(owningUI);
}
// /** Sets the invoking thread id. */
// private void setIt(long value) {
// this.it = value;
// }
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();
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) {
return null; // user cancels
}
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();
System.setProperty("it", String.valueOf(this.it));
System.setProperty("ASCAN_FLEX_LICENSE", license == null ? "" : license);
try {
Request request = Request.fromJson(scanRequestInJson);
boolean fallbackToEn = false;
if(request != null) {
Properties i18nToSet = TranslationProps.loadLangIfAvailable(request.getI18n(), Request.I18N_PROPS_PREFIX);
if (Utils.isEnterpriseOrEvaluation(this)) {
if (i18nToSet != null && i18nToSet.size() > 0) {
// carry to native code
String scanMoreTitle = i18nToSet.getProperty(Request.I18N_SCAN_MORE_DIALOG_TITLE);
String scanMoreMesg = i18nToSet.getProperty(Request.I18N_SCAN_MORE_DIALOG_MESSAGE);
if (scanMoreTitle != null && scanMoreTitle.trim().length() > 0) {
request.setI18n(Request.I18N_SCAN_MORE_DIALOG_TITLE,
scanMoreTitle.startsWith("base64,") ? scanMoreTitle : "base64," + Base64.byteArrayToBase64(scanMoreTitle.getBytes("UTF-8")));
}
if (scanMoreMesg != null && scanMoreMesg.trim().length() > 0) {
request.setI18n(Request.I18N_SCAN_MORE_DIALOG_MESSAGE,
scanMoreMesg.startsWith("base64,") ? scanMoreMesg : "base64," + Base64.byteArrayToBase64(scanMoreMesg.getBytes("UTF-8")));
}
setI18n(i18nToSet);
} else {
fallbackToEn = true;
}
} else {
fallbackToEn = true;
if(request.getI18n() != null && request.getI18n().size() > 0) {
if(! (request.getI18n().size() == 1 && "en".equalsIgnoreCase(request.getI18n().getProperty("lang")))) {
JOptionPane.showMessageDialog(null, "i18n is not supported under the current license", "Warn", JOptionPane.OK_OPTION | JOptionPane.WARNING_MESSAGE);
}
request.clearI18n(null);
}
}
}
if(fallbackToEn) { // If the UI was in other languages, we need to fallback to 'en' to display EN.
Properties propTmp = new Properties();
propTmp.setProperty(Request.I18N_LANG, "en");
setI18n(TranslationProps.loadLangIfAvailable(propTmp, Request.I18N_PROPS_PREFIX));
}
scanRequestInJson = request.toJson(true);
} catch (Throwable e) {
e.printStackTrace();
}
sourceName = doSelectSourceIfUsingAspriseSelectUI(sourceName);
if(getUseAspriseSourceSelectUI() && "select".equalsIgnoreCase(sourceName)) { // user cancels
return null;
}
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) {
System.setProperty("it", String.valueOf(this.it));
System.setProperty("ASCAN_FLEX_LICENSE", license == null ? "" : license);
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) {
System.setProperty("it", String.valueOf(this.it));
System.setProperty("ASCAN_FLEX_LICENSE", license == null ? "" : license);
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