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.scan.twain.TwainConstants;
import com.asprise.imaging.core.scan.twain.TwainException;
import com.asprise.imaging.core.scan.twain.TwainUtil;
import com.asprise.imaging.core.util.JsonUtils;
import com.asprise.imaging.core.util.system.StringUtils;
import com.fasterxml.jackson.jr.ob.JSON;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Represents a scan result
*/
public class Result {
/** Source device */
String device;
/** Number of images acquired */
int imageCount = -1;
/** Last transfer code, e.g., {@linkplain TwainConstants#TWRC_SUCCESS}. */
int lastTransferResultCode = -1;
String requestId;
Object requestData;
String responseId;
/** Extra info to be carried */
String info;
// String rawResult; // could boost memory footprint, so we disable it here.
List images = new ArrayList();
List outputItems = new ArrayList();
protected Result(Map jsonScanResult) {
device = TwainUtil.toString(jsonScanResult.get("device"));
imageCount = TwainUtil.toInteger(jsonScanResult.get("image_count"), -1);
lastTransferResultCode = TwainConstants.getTwrcCode(TwainUtil.toString(jsonScanResult.get("last_transfer_rc")));
requestId = TwainUtil.toString(jsonScanResult.get("request_id"));
requestData = jsonScanResult.get("request_data");
responseId = TwainUtil.toString(jsonScanResult.get("response_id"));
info = TwainUtil.toString(jsonScanResult.get("info"));
Object jsonImages = jsonScanResult.get("images");
if(jsonImages instanceof List) {
List jsonList = (List)jsonImages;
for(int i = 0; i < jsonList.size(); i++) {
images.add(ResultImageItem.createScanResultImageItem((Map) jsonList.get(i)));
}
}
Object jsonOutputs = jsonScanResult.get("output");
if(jsonOutputs instanceof List) {
List jsonList = (List)jsonOutputs;
for(int i = 0; i < jsonList.size(); i++) {
outputItems.add(ResultOutputItem.createScanResultOutputItem((Map) jsonList.get(i)));
}
}
}
public Map toJsonObject() {
Map json = new HashMap();
json.put("device", device);
json.put("image_count", imageCount);
json.put("last_transfer_rc", lastTransferResultCode);
json.put("request_id", requestId);
if(requestData != null) {
json.put("request_data", requestData);
}
json.put("response_id", responseId);
List jsonImages = new ArrayList();
for(int i = 0; images != null && i < images.size(); i++) {
jsonImages.add(images.get(i).toJsonObject());
}
json.put("images", jsonImages);
List jsonOutputs = new ArrayList();
for(int i = 0; outputItems != null && i < outputItems.size(); i++) {
jsonOutputs.add(outputItems.get(i).toJsonObject());
}
json.put("output", jsonOutputs);
if(! StringUtils.isEmpty(info)) {
json.put("info", info);
}
return json;
}
public String toJson(boolean pretty) {
return JsonUtils.jsonSerialize(toJsonObject(), pretty);
}
/**
* Creates Result if the resultJson is not null.
* @param resultJson the string
* @return Result created or null if resultJson is null.
*/
public static Result fromJson(String resultJson) throws IOException {
if(resultJson == null) {
return null;
}
Map root = JSON.std.mapFrom(resultJson);
return Result.createScanResult(root);
}
/**
* Creates ScanResultImageItem if the jsonImageObject is not null.
* @param jsonImageObject
* @return ScanResultImageItem created or null if jsonImageObject is null.
*/
public static Result createScanResult(Map jsonImageObject) {
if(jsonImageObject == null) {
return null;
} else {
return new Result(jsonImageObject);
}
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
/**
* The source device from which images are acquired.
* @return name of the source device
*/
public String getDevice() {
return device;
}
public void setDevice(String device) {
this.device = device;
}
/**
* Total number of images acquired.
* @return
*/
public int getImageCount() {
return imageCount;
}
public void setImageCount(int imageCount) {
this.imageCount = imageCount;
}
/**
* Last transfer result code, e.g., {@linkplain TwainConstants#TWRC_SUCCESS}.
* @return
*/
public int getLastTransferResultCode() {
return lastTransferResultCode;
}
public void setLastTransferResultCode(int lastTransferResultCode) {
this.lastTransferResultCode = lastTransferResultCode;
}
/**
* Whether user pressed 'Cancel'.
*/
public boolean isUserCancelled() {
return getLastTransferResultCode() == TwainConstants.TWRC_CANCEL;
}
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public Object getRequestData() {
return requestData;
}
public void setRequestData(Object requestData) {
this.requestData = requestData;
}
public String getResponseId() {
return responseId;
}
public void setResponseId(String responseId) {
this.responseId = responseId;
}
/**
* Lists of images acquired.
* @return
*/
public List getImageItems() {
return images;
}
/**
* List of all output items.
* @return
*/
public List getOutputItems() {
return outputItems;
}
/**
* Finds the first output item of the given type.
* @param outputType any of {@linkplain Imaging#OUTPUT_SAVE} ...
* @return the first output item of the list sorted by record count descending then format (JPG, PNG, BMP, TIF, PDF).
*/
public ResultOutputItem getOutputItem(String outputType) {
for(int i = 0; outputItems != null && i < outputItems.size(); i++) {
ResultOutputItem outputItem = outputItems.get(i);
if(outputItem == null) {
continue;
}
if(outputType.equals(outputItem.getType())) {
return outputItem;
}
}
return null;
}
/**
* Finds the all output items of the given type.
* @param outputType any of {@linkplain Imaging#OUTPUT_SAVE} ...
* @return All output items of the given type sorted by record count descending then format (JPG, PNG, BMP, TIF, PDF).
*/
public List getOutputItems(String outputType) {
List filtered = new ArrayList();
for(int i = 0; outputItems != null && i < outputItems.size(); i++) {
ResultOutputItem outputItem = outputItems.get(i);
if(outputItem == null) {
continue;
}
if(outputType.equals(outputItem.getType())) {
filtered.add(outputItem);
}
}
// order by number of record counts
Collections.sort(filtered, new Comparator() {
public int compare(ResultOutputItem o1, ResultOutputItem o2) {
int recordDiff = o2.getOutputRecordCount() - o1.getOutputRecordCount();
if(recordDiff != 0) {
return recordDiff;
}
return getFileFormatRanking(o1.getFormat()) - getFileFormatRanking(o2.getFormat());
}
});
return filtered;
}
private static int getFileFormatRanking(String format) {
if(Imaging.FORMAT_JPG.equalsIgnoreCase(format)) {
return 1;
} else if(Imaging.FORMAT_PNG.equalsIgnoreCase(format)) {
return 2;
} else if(Imaging.FORMAT_BMP.equalsIgnoreCase(format)) {
return 3;
} else if(Imaging.FORMAT_TIF.equalsIgnoreCase(format)) {
return 4;
} else if(Imaging.FORMAT_PDF.equalsIgnoreCase(format)) {
return 5;
} else {
return 6;
}
}
/**
* Gets the list of image files acquired through {@linkplain Imaging#OUTPUT_SAVE}
* The
* @return the list of image files or null if no {@linkplain Imaging#OUTPUT_SAVE} record found.
*/
public List getImageFiles() {
ResultOutputItem outputSave = getOutputItem(Imaging.OUTPUT_SAVE);
return outputSave == null ? null : outputSave.getFiles();
}
/**
* Gets the list of image files acquired through {@linkplain Imaging#OUTPUT_SAVE_THUMB}
* @return the list of image files or null if no {@linkplain Imaging#OUTPUT_SAVE_THUMB} record found.
*/
public List getThumbnailFiles() {
ResultOutputItem outputSave = getOutputItem(Imaging.OUTPUT_SAVE_THUMB);
return outputSave == null ? null : outputSave.getFiles();
}
/**
* Gets the PDF file acquired through {@linkplain Imaging#OUTPUT_SAVE}.
* @return the PDF file acquired through {@linkplain Imaging#OUTPUT_SAVE} or null if none is found.
*/
public File getPdfFile() {
List outputItems = getOutputItems(Imaging.OUTPUT_SAVE);
for(int i = 0; outputItems != null && i < outputItems.size(); i++) {
ResultOutputItem item = outputItems.get(i);
if(Imaging.FORMAT_PDF.equalsIgnoreCase(item.getFormat())) {
List files = item.getFiles();
return files != null && files.size() > 0 ? files.get(0) : null;
}
}
return null;
}
/**
* Gets the TIFF file acquired through {@linkplain Imaging#OUTPUT_SAVE}.
* @return the TIFF file acquired through {@linkplain Imaging#OUTPUT_SAVE} or null if none is found.
*/
public File getTiffFile() {
List outputItems = getOutputItems(Imaging.OUTPUT_SAVE);
for(int i = 0; outputItems != null && i < outputItems.size(); i++) {
ResultOutputItem item = outputItems.get(i);
if(item.getFormat() != null && item.getFormat().toLowerCase().contains(Imaging.FORMAT_TIF)) {
List files = item.getFiles();
return files != null && files.size() > 0 ? files.get(0) : null;
}
}
return null;
}
/**
* Gets the original image of the given index by searching the output in order: {@linkplain Imaging#OUTPUT_RETURN_BASE64}, {@linkplain Imaging#OUTPUT_SAVE}
* This is a shortcut method, use {@linkplain #getOutputItems()} if you need more control.
* @param index
* @return the image of the given index or null if not available.
* @throws IndexOutOfBoundsException if index is invalid
* @throws TwainException if failed to load.
*/
public BufferedImage getImage(int index) {
List outputReturnBase64s = getOutputItems(Imaging.OUTPUT_RETURN_BASE64);
if(outputReturnBase64s != null && outputReturnBase64s.size() > 0) {
return outputReturnBase64s.get(0).loadImage(index);
}
List outputSaves = getOutputItems(Imaging.OUTPUT_SAVE);
if(outputSaves != null && outputSaves.size() > 0) {
return outputSaves.get(0).loadImage(index);
}
return null;
}
/**
* Returns all scanned images.
* @return
*/
public List getImages() {
List images = new ArrayList();
for(int i = 0; i < getImageCount(); i++) {
images.add(getImage(i));
}
return images;
}
/**
* Gets the thumbnail image of the given index by searching the output in order: {@linkplain Imaging#OUTPUT_RETURN_BASE64_THUMB}, {@linkplain Imaging#OUTPUT_SAVE_THUMB}
* This is a shortcut method, use {@linkplain #getOutputItems()} if you need more control.
* @param index
* @return the image of the given index or null if not available.
* @throws IndexOutOfBoundsException if index is invalid
* @throws TwainException if failed to load.
*/
public BufferedImage getThumbnail(int index) {
List outputReturnBase64Thumbs = getOutputItems(Imaging.OUTPUT_RETURN_BASE64_THUMB);
if(outputReturnBase64Thumbs != null && outputReturnBase64Thumbs.size() > 0) {
return outputReturnBase64Thumbs.get(0).loadImage(index);
}
List outputSaveThumbs = getOutputItems(Imaging.OUTPUT_SAVE_THUMB);
if(outputSaveThumbs != null && outputSaveThumbs.size() > 0) {
return outputSaveThumbs.get(0).loadImage(index);
}
return null;
}
/**
* Returns the upload response if available.
* This is a shortcut method, use {@linkplain #getOutputItems()} if you need more control.
* @return the upload response or null if not available.
*/
public String getUploadResponse() {
ResultOutputItem outputItemUpload = getOutputItem(Imaging.OUTPUT_UPLOAD);
return outputItemUpload == null || outputItemUpload.getOutputRecordCount() == 0 ?
null : outputItemUpload.getOutputRecords().get(0);
}
/**
* Returns the thumbnail upload response if available.
* This is a shortcut method, use {@linkplain #getOutputItems()} if you need more control.
* @return the thumbnail upload response or null if not available.
*/
public String getThumbnailUploadResponse() {
ResultOutputItem outputItemUpload = getOutputItem(Imaging.OUTPUT_UPLOAD_THUMB);
return outputItemUpload == null || outputItemUpload.getOutputRecordCount() == 0 ?
null : outputItemUpload.getOutputRecords().get(0);
}
public List getBarcodes() {
List list = new ArrayList();
for(int i = 0; i < getImageItems().size(); i++) {
ResultImageItem item = getImageItems().get(i);
List barcodes = item.getBarcodes();
if(barcodes == null || barcodes.size() == 0) {
continue;
}
for(int b = 0; b < barcodes.size(); b++) {
Map barcode = (Map) barcodes.get(b);
StringBuilder sb = new StringBuilder().append("barcodeIndex=").append(list.size()).append(", pageIndex=").append(i).append(", ").append(mapToString(barcode));
list.add(sb.toString());
}
}
return list;
}
static String mapToString(Map map) {
StringBuilder sb = new StringBuilder();
for (Object key : map.keySet()) {
if(sb.length() > 0) {
sb.append(", ");
}
sb.append(String.valueOf(key)).append("=").append(String.valueOf(map.get(key)));
}
return sb.toString();
}
@Override
public String toString() {
return "Result{" +
"device='" + device + '\'' +
", imageCount=" + imageCount +
", lastTransferResultCode=" + lastTransferResultCode +
", requestId='" + requestId + '\'' +
", requestData='" + requestData + '\'' +
", responseId='" + responseId + '\'' +
", images=" + images +
", outputItems=" + outputItems +
", info=" + info +
'}';
}
}