feat(web): 优化 WebSocket 服务并添加启动日志
- 修改 CamSocketServer 和 SocketServer 的启动日志 - 优化 WebcamShowTask 中的图像处理逻辑 - 移除 WebcamShowTask2 中的冗余代码 - 更新 WebCamUtils 中的图像获取方法 - 在 Main 类中添加 WebSocket服务启动日志 - 更新项目版本号至 0.1.6
This commit is contained in:
parent
0b7ebfeec0
commit
9f14f01507
2
pom.xml
2
pom.xml
|
@ -6,7 +6,7 @@
|
|||
|
||||
<groupId>org.aohe</groupId>
|
||||
<artifactId>twain-service</artifactId>
|
||||
<version>0.1.5</version>
|
||||
<version>0.1.6</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.aohe;
|
||||
|
||||
import cn.hutool.core.net.NetUtil;
|
||||
import com.formdev.flatlaf.FlatDarculaLaf;
|
||||
import io.github.sanyarnd.applocker.AppLocker;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -21,9 +22,8 @@ public class Main {
|
|||
private static final String JAVA_IO_TMPDIR = System.getProperty("java.io.tmpdir");
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
AppLocker locker = AppLocker.create("aoheSaneServiceLock").setPath(Paths.get(JAVA_IO_TMPDIR)).build();
|
||||
AppLocker locker = AppLocker.create("AoheSaneServiceLock").setPath(Paths.get(JAVA_IO_TMPDIR)).build();
|
||||
FlatDarculaLaf.setup();
|
||||
//UIManager.setLookAndFeel(new FlatDarculaLaf());
|
||||
try {
|
||||
locker.lock();
|
||||
}catch (Exception e){
|
||||
|
@ -32,7 +32,7 @@ public class Main {
|
|||
log.error("程序已被其他应用占用");
|
||||
System.exit(0);
|
||||
}
|
||||
log.info("启动成功");
|
||||
log.info("启动中....");
|
||||
SocketUtils.start(SocketEnum.CAM_SOCKET);
|
||||
Control.WEBCAM_SHOW_TASK = new WebcamShowTask();
|
||||
Control.WEBCAM_SHOW_TASK.start();
|
||||
|
@ -40,6 +40,18 @@ public class Main {
|
|||
watchDog.setDaemon(true);
|
||||
watchDog.start();
|
||||
TrayFrameUtf8.initSystemTrayUTF8();
|
||||
StartLog();
|
||||
}
|
||||
|
||||
private static void StartLog() {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
buffer.append("WebSocket 启动完成 \n");
|
||||
buffer.append("---------------------------------------------------------------\n");
|
||||
for(String ip : NetUtil.localIpv4s()){
|
||||
buffer.append(" ws://").append(ip).append(":").append(SocketEnum.SCAN_SOCKET.getPort()).append("\n");
|
||||
}
|
||||
buffer.append("---------------------------------------------------------------\n");
|
||||
log.info(buffer.toString());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ public class CamSocketServer extends SocketServer {
|
|||
|
||||
@Override
|
||||
public void onStart() {
|
||||
log.info("Server started!");
|
||||
log.info("CamSocketServer started!");
|
||||
setConnectionLostTimeout(0);
|
||||
setConnectionLostTimeout(100);
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ public class SocketServer extends WebSocketServer {
|
|||
|
||||
@Override
|
||||
public void onStart() {
|
||||
log.info("Server started!");
|
||||
log.info("Default SocketServer started!");
|
||||
setConnectionLostTimeout(0);
|
||||
setConnectionLostTimeout(100);
|
||||
|
||||
|
|
|
@ -90,17 +90,19 @@ public class WebCamUtils {
|
|||
return webcam.getImage();
|
||||
}
|
||||
|
||||
public static byte[] getImageByteBuffer(){
|
||||
/**
|
||||
* 使用 Native Driver 时不生效
|
||||
* @return
|
||||
*/
|
||||
public static ByteBuffer getImageByteBuffer(){
|
||||
if (!webcam.isOpen()){
|
||||
webcam.open();
|
||||
}
|
||||
ByteBuffer imageBytes = webcam.getImageBytes();
|
||||
imageBytes.flip();
|
||||
System.out.println("imageBytes.remaining():"+imageBytes.remaining());
|
||||
byte[] byteArray = new byte[imageBytes.remaining()];
|
||||
imageBytes.get(byteArray);
|
||||
System.out.println("byteArray.length:"+byteArray.length);
|
||||
return byteArray;
|
||||
|
||||
return imageBytes;
|
||||
}
|
||||
|
||||
public static InputStream getImageInputStream() {
|
||||
|
|
|
@ -3,17 +3,12 @@ package org.aohe.webcam;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aohe.constant.Control;
|
||||
import org.aohe.constant.SocketEnum;
|
||||
import org.aohe.result.R;
|
||||
import org.aohe.web.SocketUtils;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -41,15 +36,17 @@ public class WebcamShowTask extends Thread {
|
|||
bi = null;
|
||||
}
|
||||
}
|
||||
String base64 = null;
|
||||
try {
|
||||
base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}finally {
|
||||
baos.close();
|
||||
}
|
||||
SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(R.ok(base64).toJsonStr());
|
||||
// String base64 = null;
|
||||
// try {
|
||||
// base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
|
||||
// } catch (UnsupportedEncodingException e) {
|
||||
// log.error(e.getMessage(), e);
|
||||
// }finally {
|
||||
// baos.close();
|
||||
// }
|
||||
|
||||
SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(baos.toByteArray());
|
||||
baos.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
|
@ -64,23 +61,13 @@ public class WebcamShowTask extends Thread {
|
|||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SocketUtils.start(SocketEnum.CAM_SOCKET);
|
||||
File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png");
|
||||
while (true){
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try {
|
||||
BufferedImage image = ImageIO.read(file);
|
||||
ImageIO.write(image, "PNG", baos);
|
||||
} catch (IOException e) {
|
||||
//log.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
String base64 = null;
|
||||
base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), StandardCharsets.UTF_8);
|
||||
|
||||
SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(base64);
|
||||
}
|
||||
}
|
||||
// public static void main(String[] args) {
|
||||
// SocketUtils.start(SocketEnum.CAM_SOCKET);
|
||||
// WebCamUtils.setWebcam(0);
|
||||
// WebCamUtils.open();
|
||||
// //File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png");
|
||||
// while (true){
|
||||
// SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(WebCamUtils.getImageByteBuffer());
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -1,95 +1,95 @@
|
|||
package org.aohe.webcam;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aohe.constant.Control;
|
||||
import org.aohe.constant.SocketEnum;
|
||||
import org.aohe.web.SocketUtils;
|
||||
import org.java_websocket.framing.BinaryFrame;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
public class WebcamShowTask2 extends Thread {
|
||||
|
||||
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
scheduler.scheduleAtFixedRate(() -> {
|
||||
try {
|
||||
while (Control.SHOW_CAMERA) {
|
||||
WebCamUtils.open();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
BufferedImage bi = WebCamUtils.getImage();
|
||||
try {
|
||||
ImageIO.write(bi, "JPG", baos);
|
||||
} catch (IOException e) {
|
||||
//log.error(e.getMessage(), e);
|
||||
}finally {
|
||||
if (bi != null){
|
||||
bi.flush();
|
||||
bi = null;
|
||||
}
|
||||
}
|
||||
// String base64 = null;
|
||||
//package org.aohe.webcam;
|
||||
//
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.aohe.constant.Control;
|
||||
//import org.aohe.constant.SocketEnum;
|
||||
//import org.aohe.web.SocketUtils;
|
||||
//import org.java_websocket.framing.BinaryFrame;
|
||||
//
|
||||
//import javax.imageio.ImageIO;
|
||||
//import java.awt.image.BufferedImage;
|
||||
//import java.io.ByteArrayOutputStream;
|
||||
//import java.io.IOException;
|
||||
//import java.nio.ByteBuffer;
|
||||
//import java.util.concurrent.Executors;
|
||||
//import java.util.concurrent.ScheduledExecutorService;
|
||||
//import java.util.concurrent.TimeUnit;
|
||||
//
|
||||
//@Slf4j
|
||||
//public class WebcamShowTask2 extends Thread {
|
||||
//
|
||||
// private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
//
|
||||
// @Override
|
||||
// public void run() {
|
||||
// scheduler.scheduleAtFixedRate(() -> {
|
||||
// try {
|
||||
// while (Control.SHOW_CAMERA) {
|
||||
// WebCamUtils.open();
|
||||
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
// BufferedImage bi = WebCamUtils.getImage();
|
||||
// try {
|
||||
// base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
|
||||
// } catch (UnsupportedEncodingException e) {
|
||||
// log.error(e.getMessage(), e);
|
||||
// ImageIO.write(bi, "JPG", baos);
|
||||
// } catch (IOException e) {
|
||||
// //log.error(e.getMessage(), e);
|
||||
// }finally {
|
||||
// baos.close();
|
||||
// if (bi != null){
|
||||
// bi.flush();
|
||||
// bi = null;
|
||||
// }
|
||||
// }
|
||||
|
||||
SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(WebCamUtils.getImageByteBuffer());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}, 0, 100, TimeUnit.MILLISECONDS);
|
||||
|
||||
try {
|
||||
Thread.currentThread().join();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("Main thread interrupted", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SocketUtils.start(SocketEnum.CAM_SOCKET);
|
||||
WebCamUtils.setWebcam(0);
|
||||
WebCamUtils.open();
|
||||
//File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png");
|
||||
while (true){
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
BufferedImage bi = WebCamUtils.getImage();
|
||||
try {
|
||||
ImageIO.write(bi, "JPG", baos);
|
||||
} catch (IOException e) {
|
||||
//log.error(e.getMessage(), e);
|
||||
}finally {
|
||||
if (bi != null){
|
||||
bi.flush();
|
||||
bi = null;
|
||||
}
|
||||
}
|
||||
// try {
|
||||
// base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
|
||||
// } catch (UnsupportedEncodingException e) {
|
||||
// log.error(e.getMessage(), e);
|
||||
// }finally {
|
||||
// baos.close();
|
||||
// }
|
||||
BinaryFrame frame = new BinaryFrame();
|
||||
frame.setPayload(ByteBuffer.wrap(baos.toByteArray()));
|
||||
SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
//// String base64 = null;
|
||||
//// try {
|
||||
//// base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
|
||||
//// } catch (UnsupportedEncodingException e) {
|
||||
//// log.error(e.getMessage(), e);
|
||||
//// }finally {
|
||||
//// baos.close();
|
||||
//// }
|
||||
//
|
||||
// SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(WebCamUtils.getImageByteBuffer());
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// log.error(e.getMessage(), e);
|
||||
// }
|
||||
// }, 0, 100, TimeUnit.MILLISECONDS);
|
||||
//
|
||||
// try {
|
||||
// Thread.currentThread().join();
|
||||
// } catch (InterruptedException e) {
|
||||
// log.error("Main thread interrupted", e);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public static void main(String[] args) {
|
||||
// SocketUtils.start(SocketEnum.CAM_SOCKET);
|
||||
// WebCamUtils.setWebcam(1);
|
||||
// WebCamUtils.open();
|
||||
// //File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png");
|
||||
// while (true){
|
||||
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
// BufferedImage bi = WebCamUtils.getImage();
|
||||
// try {
|
||||
// ImageIO.write(bi, "JPG", baos);
|
||||
// } catch (IOException e) {
|
||||
// //log.error(e.getMessage(), e);
|
||||
// }finally {
|
||||
// if (bi != null){
|
||||
// bi.flush();
|
||||
// bi = null;
|
||||
// }
|
||||
// }
|
||||
//// try {
|
||||
//// base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
|
||||
//// } catch (UnsupportedEncodingException e) {
|
||||
//// log.error(e.getMessage(), e);
|
||||
//// }finally {
|
||||
//// baos.close();
|
||||
//// }
|
||||
// BinaryFrame frame = new BinaryFrame();
|
||||
// frame.setPayload(ByteBuffer.wrap(baos.toByteArray()));
|
||||
// SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(frame);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
|
Loading…
Reference in New Issue