130 lines
4.0 KiB
Java
130 lines
4.0 KiB
Java
package org.aohe.scanservice.control;
|
||
|
||
import au.com.southsky.jfreesane.SaneDevice;
|
||
import au.com.southsky.jfreesane.SaneException;
|
||
import cn.hutool.core.codec.Base64;
|
||
import com.alibaba.fastjson2.JSONObject;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.aohe.scanservice.core.SaneSessionUtils;
|
||
import org.aohe.scanservice.result.R;
|
||
|
||
import java.io.File;
|
||
import java.io.IOException;
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import java.util.stream.Collectors;
|
||
|
||
@Slf4j
|
||
public class SaneOperational {
|
||
|
||
public static String selectOperational(String path) {
|
||
|
||
JSONObject json = JSONObject.parse(path);
|
||
|
||
//操作符
|
||
String function = json.getString("function");
|
||
|
||
//参数符
|
||
JSONObject param = json.getJSONObject("params");
|
||
|
||
R r = R.ok();
|
||
try {
|
||
if("001001".equals(function)){
|
||
//获取扫描仪列表
|
||
r = getDeviceList();
|
||
} else if ("001002".equals(function)) {
|
||
//选择扫描仪
|
||
r = setScanner(param.getString("scannerId"));
|
||
}else if ("001003".equals(function)){
|
||
//获取扫描仪操作符列表
|
||
// r = getDeviceOperations();
|
||
}else if ("001004".equals(function)){
|
||
//r = setDeviceOperations();
|
||
}else if ("001007".equals(function)){
|
||
//r = setDeviceOperations(param);
|
||
}else if ("001008".equals(function)){
|
||
r = startScan(param);
|
||
}else if ("001012".equals(function)){
|
||
//closeTwSource();
|
||
}else if ("001013".equals(function)){
|
||
//closeTwSource();
|
||
}else if ("001015".equals(function)){
|
||
//r = startScan(param.getString("scannerId"),true);
|
||
}else if ("001016".equals(function)){
|
||
//r = startScan(param.getString("scannerId"),true);
|
||
}
|
||
}catch (Exception e){
|
||
log.info("Error , ", e);
|
||
}
|
||
|
||
|
||
return r.toJsonStr();
|
||
}
|
||
|
||
/**
|
||
* 001001 获取扫描仪列表
|
||
* @return 返回扫描仪列表
|
||
*/
|
||
public static R getDeviceList(){
|
||
try {
|
||
List<SaneDevice> saneDevices = SaneSessionUtils.getSaneDrivers();
|
||
List<String> names = saneDevices.stream().map(SaneDevice::getName).collect(Collectors.toList());
|
||
return R.ok(names);
|
||
} catch (IOException | SaneException e) {
|
||
log.error("Not found scan list", e);
|
||
return R.fail("Not found scan list");
|
||
}
|
||
}
|
||
|
||
private static String saneDrivers = null;
|
||
|
||
/**
|
||
* 001002 选择扫描仪
|
||
* @param scannerId 扫描仪id
|
||
* @return 返回成功
|
||
*/
|
||
public static R setScanner(String scannerId){
|
||
try {
|
||
if(saneDrivers == null){
|
||
saneDrivers = scannerId;
|
||
}
|
||
|
||
SaneSessionUtils.setSaneDevice(scannerId);
|
||
return R.ok();
|
||
} catch (SaneException | IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 001007 设置扫描仪参数
|
||
* @param params 扫描仪参数
|
||
* @return 返回成功
|
||
*/
|
||
// public static R setDeviceOperations(JSONObject params){
|
||
// try {
|
||
// SaneSeesionUtils.setOption(params);
|
||
// } catch (TwainException e) {
|
||
// log.error("scan setting is not set", e);
|
||
// return R.fail("scan setting is not set, please select scan ");
|
||
// }
|
||
// return R.ok();
|
||
// }
|
||
|
||
public static R startScan(JSONObject params){
|
||
try {
|
||
File files = SaneSessionUtils.getSaneFile();
|
||
Map<String,String> map = new HashMap<>();
|
||
map.put(files.getName(),Base64.encode(files));
|
||
List<Map<String,String>> list = new ArrayList<>();
|
||
list.add(map);
|
||
|
||
return R.ok(list);
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
}
|