1. 首次提交
This commit is contained in:
commit
f1dc9ca1f7
|
@ -0,0 +1,47 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.aohe</groupId>
|
||||
<artifactId>sane-service</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
<maven.compiler.target>21</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.jfreesane</groupId>
|
||||
<artifactId>jfreesane</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.4.14</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>2.0.37</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.25</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
<version>1.18.30</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
|
@ -0,0 +1,7 @@
|
|||
package org.aohe;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
package org.aohe.core;
|
|
@ -0,0 +1,39 @@
|
|||
package org.aohe.core.result;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class R<T> {
|
||||
|
||||
private String code;
|
||||
|
||||
private String msg;
|
||||
|
||||
private T data;
|
||||
|
||||
private boolean success;
|
||||
|
||||
public R(String code, String msg, T data, boolean success) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
this.data = data;
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public static R ok(Object data) {
|
||||
return new R<>("200", "ok", data, true);
|
||||
}
|
||||
|
||||
public static R ok() {
|
||||
return new R<>("200", "ok", null, true);
|
||||
}
|
||||
|
||||
public static R fail(String msg) {
|
||||
return new R<>("500", msg, null, false);
|
||||
}
|
||||
|
||||
public String toJsonStr() {
|
||||
return JSONUtil.toJsonStr(this);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,129 @@
|
|||
package org.aohe.core.sane;
|
||||
|
||||
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.core.result.R;
|
||||
import org.aohe.core.sane.utils.SaneSessionUtils;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package org.aohe.core.sane.utils;
|
||||
|
||||
import cn.hutool.setting.Setting;
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
@Data
|
||||
public class RemoteConfig {
|
||||
|
||||
private String ip = "127.0.0.1";
|
||||
|
||||
RemoteConfig() {
|
||||
}
|
||||
|
||||
RemoteConfig(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public static RemoteConfig getInstance(){
|
||||
String ip = new Setting("config.setting").get("ip");
|
||||
if(ip != null){
|
||||
return new RemoteConfig(ip);
|
||||
}
|
||||
return new RemoteConfig();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,173 @@
|
|||
package org.aohe.core.sane.utils;
|
||||
|
||||
import au.com.southsky.jfreesane.SaneDevice;
|
||||
import au.com.southsky.jfreesane.SaneException;
|
||||
import au.com.southsky.jfreesane.SaneSession;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class SaneSessionUtils {
|
||||
|
||||
private static final Object lock = new Object();
|
||||
|
||||
private static volatile SaneSession saneSession;
|
||||
|
||||
private static volatile SaneDevice saneDevice;
|
||||
|
||||
private static final RemoteConfig remoteConfig = RemoteConfig.getInstance();
|
||||
|
||||
/**
|
||||
* 获取一个SaneSession实例,用于与SANE设备进行通信
|
||||
* @return SaneSession实例
|
||||
* @throws IOException 如果获取SaneSession实例时发生I/O错误
|
||||
*/
|
||||
public static SaneSession getSaneSession() throws IOException {
|
||||
log.info("saneSession状态:{}", saneSession != null);
|
||||
if (saneSession == null) {
|
||||
synchronized(lock){
|
||||
if (saneSession == null) {
|
||||
log.info("jFreeSaneConfig:" + JSON.toJSONString(remoteConfig));
|
||||
InetAddress address = InetAddress.getByName(remoteConfig.getIp());
|
||||
saneSession = SaneSession.withRemoteSane(address);
|
||||
log.info("初始化saneSession完成,状态:{}", saneSession != null);
|
||||
}
|
||||
}
|
||||
}
|
||||
return saneSession;
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭SaneSession对象,如果对象不为null
|
||||
*/
|
||||
public static void closeSaneSession() {
|
||||
if (saneSession != null) {
|
||||
try {
|
||||
saneSession.close();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
saneSession = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的SANE驱动器设备列表
|
||||
*
|
||||
* @return SANE驱动器设备列表
|
||||
* @throws IOException 如果在通信中发生I/O错误
|
||||
* @throws SaneException 如果在与SANE交互中发生错误
|
||||
*/
|
||||
public static List<SaneDevice> getSaneDrivers() throws IOException, SaneException {
|
||||
return getSaneSession().listDevices();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置Sane设备
|
||||
* @param name 设备名称
|
||||
* @return 设备对象
|
||||
* @throws IOException 输入/输出异常
|
||||
* @throws SaneException Sane异常
|
||||
*/
|
||||
public static SaneDevice setSaneDevice(String name) throws IOException, SaneException {
|
||||
if (saneDevice == null || !saneDevice.getName().equals(name)) {
|
||||
if (saneDevice != null && saneDevice.isOpen()) {
|
||||
// 关闭当前在使用的SANE设备
|
||||
saneDevice.close();
|
||||
}
|
||||
saneDevice = saneSession
|
||||
.listDevices().stream().filter(s -> name.equals(s.getName())).findFirst().orElse(null);
|
||||
}
|
||||
return saneDevice;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Sane设备(如果不存在则返回null)
|
||||
* @return Sane设备
|
||||
*/
|
||||
public static SaneDevice getDevice() {
|
||||
return saneDevice;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定名称的Sane设备,如果设备不存在则返回null
|
||||
* @param name 设备名称
|
||||
* @return 指定名称的Sane设备
|
||||
* @throws RuntimeException 运行时异常
|
||||
*/
|
||||
public static SaneDevice getDevice(String name) {
|
||||
try {
|
||||
return setSaneDevice(name);
|
||||
} catch (IOException | SaneException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取一个正常的文件
|
||||
* 打开Sane设备
|
||||
* 通过Sane设备获取图像
|
||||
* 创建或更新最后访问时间的文件
|
||||
* 将图像写入文件
|
||||
* 返回文件
|
||||
* @throws RuntimeException - 如果发生I/O错误
|
||||
*/
|
||||
public static File getSaneFile() throws IOException {
|
||||
try {
|
||||
saneDevice.open();
|
||||
BufferedImage bufferedImage = saneDevice.acquireImage();
|
||||
File file = FileUtil.createTempFile("sane","png",true);
|
||||
ImageIO.write(bufferedImage, "png", file);
|
||||
return file;
|
||||
} catch (IOException | SaneException e) {
|
||||
throw new RuntimeException(e);
|
||||
}finally {
|
||||
if(saneDevice != null){
|
||||
saneDevice.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放资源
|
||||
*/
|
||||
public static void resource(){
|
||||
try {
|
||||
if(saneDevice != null){
|
||||
closeSaneDevice();
|
||||
}
|
||||
if(saneSession != null){
|
||||
closeSaneSession();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭Sane设备
|
||||
*/
|
||||
public static void closeSaneDevice() throws IOException {
|
||||
if(saneDevice != null){
|
||||
try {
|
||||
saneDevice.close();
|
||||
} catch (IOException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
saneDevice = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package org.aohe.core.sane.utils;
|
||||
|
||||
public class SystemUtils {
|
||||
|
||||
/**
|
||||
* 判断操作系统是否是 Windows
|
||||
*
|
||||
* @return true:操作系统是 Windows
|
||||
* false:其它操作系统
|
||||
*/
|
||||
public static boolean isWindows() {
|
||||
String osName = getOsName();
|
||||
|
||||
return osName != null && osName.startsWith("Windows");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断操作系统是否是 MacOS
|
||||
*
|
||||
* @return true:操作系统是 MacOS
|
||||
* false:其它操作系统
|
||||
*/
|
||||
public static boolean isMacOs() {
|
||||
String osName = getOsName();
|
||||
|
||||
return osName != null && osName.startsWith("Mac");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断操作系统是否是 Linux
|
||||
*
|
||||
* @return true:操作系统是 Linux
|
||||
* false:其它操作系统
|
||||
*/
|
||||
public static boolean isLinux() {
|
||||
String osName = getOsName();
|
||||
|
||||
return (osName != null && osName.startsWith("Linux")) || (!isWindows() && !isMacOs());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取操作系统名称
|
||||
* @return os.name 属性值
|
||||
*/
|
||||
public static String getOsName() {
|
||||
return System.getProperty("os.name");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue