feat(web): 优化 WebSocket 服务并添加启动日志

- 修改 CamSocketServer 和 SocketServer 的启动日志
- 优化 WebcamShowTask 中的图像处理逻辑
- 移除 WebcamShowTask2 中的冗余代码
- 更新 WebCamUtils 中的图像获取方法
- 在 Main 类中添加 WebSocket服务启动日志
- 更新项目版本号至 0.1.6
This commit is contained in:
李建国 2025-02-11 15:01:39 +08:00
parent 0b7ebfeec0
commit 9f14f01507
7 changed files with 137 additions and 136 deletions

View File

@ -6,7 +6,7 @@
<groupId>org.aohe</groupId> <groupId>org.aohe</groupId>
<artifactId>twain-service</artifactId> <artifactId>twain-service</artifactId>
<version>0.1.5</version> <version>0.1.6</version>
<properties> <properties>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.source>8</maven.compiler.source>

View File

@ -1,5 +1,6 @@
package org.aohe; package org.aohe;
import cn.hutool.core.net.NetUtil;
import com.formdev.flatlaf.FlatDarculaLaf; import com.formdev.flatlaf.FlatDarculaLaf;
import io.github.sanyarnd.applocker.AppLocker; import io.github.sanyarnd.applocker.AppLocker;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -21,9 +22,8 @@ public class Main {
private static final String JAVA_IO_TMPDIR = System.getProperty("java.io.tmpdir"); private static final String JAVA_IO_TMPDIR = System.getProperty("java.io.tmpdir");
public static void main(String[] args) throws Exception { 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(); FlatDarculaLaf.setup();
//UIManager.setLookAndFeel(new FlatDarculaLaf());
try { try {
locker.lock(); locker.lock();
}catch (Exception e){ }catch (Exception e){
@ -32,7 +32,7 @@ public class Main {
log.error("程序已被其他应用占用"); log.error("程序已被其他应用占用");
System.exit(0); System.exit(0);
} }
log.info("启动成功"); log.info("启动中....");
SocketUtils.start(SocketEnum.CAM_SOCKET); SocketUtils.start(SocketEnum.CAM_SOCKET);
Control.WEBCAM_SHOW_TASK = new WebcamShowTask(); Control.WEBCAM_SHOW_TASK = new WebcamShowTask();
Control.WEBCAM_SHOW_TASK.start(); Control.WEBCAM_SHOW_TASK.start();
@ -40,6 +40,18 @@ public class Main {
watchDog.setDaemon(true); watchDog.setDaemon(true);
watchDog.start(); watchDog.start();
TrayFrameUtf8.initSystemTrayUTF8(); 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());
} }

View File

@ -60,7 +60,7 @@ public class CamSocketServer extends SocketServer {
@Override @Override
public void onStart() { public void onStart() {
log.info("Server started!"); log.info("CamSocketServer started!");
setConnectionLostTimeout(0); setConnectionLostTimeout(0);
setConnectionLostTimeout(100); setConnectionLostTimeout(100);

View File

@ -60,7 +60,7 @@ public class SocketServer extends WebSocketServer {
@Override @Override
public void onStart() { public void onStart() {
log.info("Server started!"); log.info("Default SocketServer started!");
setConnectionLostTimeout(0); setConnectionLostTimeout(0);
setConnectionLostTimeout(100); setConnectionLostTimeout(100);

View File

@ -90,17 +90,19 @@ public class WebCamUtils {
return webcam.getImage(); return webcam.getImage();
} }
public static byte[] getImageByteBuffer(){ /**
* 使用 Native Driver 时不生效
* @return
*/
public static ByteBuffer getImageByteBuffer(){
if (!webcam.isOpen()){ if (!webcam.isOpen()){
webcam.open(); webcam.open();
} }
ByteBuffer imageBytes = webcam.getImageBytes(); ByteBuffer imageBytes = webcam.getImageBytes();
imageBytes.flip();
System.out.println("imageBytes.remaining():"+imageBytes.remaining());
byte[] byteArray = new byte[imageBytes.remaining()]; byte[] byteArray = new byte[imageBytes.remaining()];
imageBytes.get(byteArray); imageBytes.get(byteArray);
System.out.println("byteArray.length:"+byteArray.length);
return byteArray; return imageBytes;
} }
public static InputStream getImageInputStream() { public static InputStream getImageInputStream() {

View File

@ -3,17 +3,12 @@ package org.aohe.webcam;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aohe.constant.Control; import org.aohe.constant.Control;
import org.aohe.constant.SocketEnum; import org.aohe.constant.SocketEnum;
import org.aohe.result.R;
import org.aohe.web.SocketUtils; import org.aohe.web.SocketUtils;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; 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.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -41,15 +36,17 @@ public class WebcamShowTask extends Thread {
bi = null; bi = null;
} }
} }
String base64 = null; // String base64 = null;
try { // try {
base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8"); // base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
} catch (UnsupportedEncodingException e) { // } catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e); // log.error(e.getMessage(), e);
}finally { // }finally {
baos.close(); // baos.close();
} // }
SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(R.ok(base64).toJsonStr());
SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(baos.toByteArray());
baos.close();
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
@ -64,23 +61,13 @@ public class WebcamShowTask extends Thread {
} }
public static void main(String[] args) { // public static void main(String[] args) {
SocketUtils.start(SocketEnum.CAM_SOCKET); // SocketUtils.start(SocketEnum.CAM_SOCKET);
File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png"); // WebCamUtils.setWebcam(0);
while (true){ // WebCamUtils.open();
// //File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png");
ByteArrayOutputStream baos = new ByteArrayOutputStream(); // while (true){
try { // SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(WebCamUtils.getImageByteBuffer());
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);
}
}
} }

View File

@ -1,95 +1,95 @@
package org.aohe.webcam; //package org.aohe.webcam;
//
import lombok.extern.slf4j.Slf4j; //import lombok.extern.slf4j.Slf4j;
import org.aohe.constant.Control; //import org.aohe.constant.Control;
import org.aohe.constant.SocketEnum; //import org.aohe.constant.SocketEnum;
import org.aohe.web.SocketUtils; //import org.aohe.web.SocketUtils;
import org.java_websocket.framing.BinaryFrame; //import org.java_websocket.framing.BinaryFrame;
//
import javax.imageio.ImageIO; //import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; //import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream; //import java.io.ByteArrayOutputStream;
import java.io.IOException; //import java.io.IOException;
import java.nio.ByteBuffer; //import java.nio.ByteBuffer;
import java.util.concurrent.Executors; //import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; //import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; //import java.util.concurrent.TimeUnit;
//
@Slf4j //@Slf4j
public class WebcamShowTask2 extends Thread { //public class WebcamShowTask2 extends Thread {
//
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); // private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
//
@Override // @Override
public void run() { // public void run() {
scheduler.scheduleAtFixedRate(() -> { // scheduler.scheduleAtFixedRate(() -> {
try { // try {
while (Control.SHOW_CAMERA) { // while (Control.SHOW_CAMERA) {
WebCamUtils.open(); // WebCamUtils.open();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); // ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedImage bi = WebCamUtils.getImage(); // 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;
// try { // try {
// base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8"); // ImageIO.write(bi, "JPG", baos);
// } catch (UnsupportedEncodingException e) { // } catch (IOException e) {
// log.error(e.getMessage(), e); // //log.error(e.getMessage(), e);
// }finally { // }finally {
// baos.close(); // if (bi != null){
// bi.flush();
// bi = null;
// }
// } // }
//// String base64 = null;
SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(WebCamUtils.getImageByteBuffer()); //// try {
} //// base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
} catch (Exception e) { //// } catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e); //// log.error(e.getMessage(), e);
} //// }finally {
}, 0, 100, TimeUnit.MILLISECONDS); //// baos.close();
//// }
try { //
Thread.currentThread().join(); // SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(WebCamUtils.getImageByteBuffer());
} catch (InterruptedException e) { // }
log.error("Main thread interrupted", e); // } catch (Exception e) {
} // log.error(e.getMessage(), e);
// }
} // }, 0, 100, TimeUnit.MILLISECONDS);
//
public static void main(String[] args) { // try {
SocketUtils.start(SocketEnum.CAM_SOCKET); // Thread.currentThread().join();
WebCamUtils.setWebcam(0); // } catch (InterruptedException e) {
WebCamUtils.open(); // log.error("Main thread interrupted", e);
//File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png"); // }
while (true){ //
ByteArrayOutputStream baos = new ByteArrayOutputStream(); // }
BufferedImage bi = WebCamUtils.getImage(); //
try { // public static void main(String[] args) {
ImageIO.write(bi, "JPG", baos); // SocketUtils.start(SocketEnum.CAM_SOCKET);
} catch (IOException e) { // WebCamUtils.setWebcam(1);
//log.error(e.getMessage(), e); // WebCamUtils.open();
}finally { // //File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png");
if (bi != null){ // while (true){
bi.flush(); // ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi = null; // BufferedImage bi = WebCamUtils.getImage();
} // try {
} // ImageIO.write(bi, "JPG", baos);
// try { // } catch (IOException e) {
// base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8"); // //log.error(e.getMessage(), e);
// } catch (UnsupportedEncodingException e) { // }finally {
// log.error(e.getMessage(), e); // if (bi != null){
// }finally { // bi.flush();
// baos.close(); // bi = null;
// } // }
BinaryFrame frame = new BinaryFrame(); // }
frame.setPayload(ByteBuffer.wrap(baos.toByteArray())); //// try {
SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(frame); //// 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);
// }
// }
//}