feat(webcam): 实现摄像头数据采集和实时显示
- 新增 WebcamShowTask2 类,用于定时获取摄像头图像并发送到客户端- 在 CamSocketServer 和 SocketServer 中添加新的广播方法,支持发送 ByteBuffer 和 Framedata - 在 WebCamUtils 中添加 getImageByteBuffer 方法,用于获取摄像头图像的字节流
This commit is contained in:
parent
0efb720dfc
commit
643ad3476e
|
@ -4,10 +4,12 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.aohe.result.R;
|
||||
import org.aohe.webcam.WebCamUtils;
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.java_websocket.framing.Framedata;
|
||||
import org.java_websocket.handshake.ClientHandshake;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -86,5 +88,27 @@ public class CamSocketServer extends SocketServer {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcastNew(ByteBuffer bytes) {
|
||||
if (bytes == null){
|
||||
return;
|
||||
}
|
||||
WebSocket zero = map.get("zero");
|
||||
if(zero != null && zero.isOpen()){
|
||||
zero.send(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void broadcastNew(Framedata data) {
|
||||
if (data == null){
|
||||
return;
|
||||
}
|
||||
WebSocket zero = map.get("zero");
|
||||
if(zero != null && zero.isOpen()){
|
||||
zero.sendFrame(data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -3,11 +3,13 @@ package org.aohe.web;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aohe.result.R;
|
||||
import org.java_websocket.WebSocket;
|
||||
import org.java_websocket.framing.Framedata;
|
||||
import org.java_websocket.handshake.ClientHandshake;
|
||||
import org.java_websocket.server.WebSocketServer;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import static org.aohe.control.Operational.selectOperational;
|
||||
|
||||
|
@ -70,4 +72,11 @@ public class SocketServer extends WebSocketServer {
|
|||
public void broadcastNew(byte[] bytes) {
|
||||
}
|
||||
|
||||
public void broadcastNew(ByteBuffer bytes) {
|
||||
}
|
||||
|
||||
public void broadcastNew(Framedata data) {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import javax.imageio.ImageIO;
|
|||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -89,6 +90,19 @@ public class WebCamUtils {
|
|||
return webcam.getImage();
|
||||
}
|
||||
|
||||
public static byte[] 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;
|
||||
}
|
||||
|
||||
public static InputStream getImageInputStream() {
|
||||
BufferedImage bi = getImage();
|
||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||
|
|
|
@ -0,0 +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;
|
||||
// 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(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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue