Browse Source

fix & feat:

- 摄像头流刷新出现残影问题
- 实时数据图表显示采样频率
master
qinjian 4 days ago
parent
commit
af4741fb39
  1. 98
      client/src/sections/wuyuanbiaoba/components/CameraView.jsx
  2. 73
      client/src/sections/wuyuanbiaoba/components/RealtimeCharts.jsx
  3. 4
      client/src/sections/wuyuanbiaoba/hooks/useTargetStorage.js

98
client/src/sections/wuyuanbiaoba/components/CameraView.jsx

@ -16,8 +16,8 @@ const CameraView = ({
const [scale, setScale] = useState(1.0); const [scale, setScale] = useState(1.0);
const [translateX, setTranslateX] = useState(0); const [translateX, setTranslateX] = useState(0);
const [translateY, setTranslateY] = useState(0); const [translateY, setTranslateY] = useState(0);
const [videoNaturalWidth, setVideoNaturalWidth] = useState(1920); const [videoNaturalWidth, setVideoNaturalWidth] = useState(0);
const [videoNaturalHeight, setVideoNaturalHeight] = useState(1080); const [videoNaturalHeight, setVideoNaturalHeight] = useState(0);
const [mousePos, setMousePos] = useState({ x: 0, y: 0 }); const [mousePos, setMousePos] = useState({ x: 0, y: 0 });
// //
@ -37,6 +37,7 @@ const CameraView = ({
const [currentDrawingRect, setCurrentDrawingRect] = useState(null); const [currentDrawingRect, setCurrentDrawingRect] = useState(null);
const [hoveredRectIndex, setHoveredRectIndex] = useState(-1); const [hoveredRectIndex, setHoveredRectIndex] = useState(-1);
const [isSaving, setIsSaving] = useState(false); // const [isSaving, setIsSaving] = useState(false); //
const [streamError, setStreamError] = useState(false); //
// 使WebSocket // 使WebSocket
const { isConnected, sendMessage } = useWebSocket(); const { isConnected, sendMessage } = useWebSocket();
@ -76,7 +77,7 @@ const CameraView = ({
canvas.style.width = rect.width + "px"; canvas.style.width = rect.width + "px";
canvas.style.height = rect.height + "px"; canvas.style.height = rect.height + "px";
// console.log("resizeCanvas: ", { width: rect.width, height: rect.height }); // console.log("resizeCanvas: ", { width: rect.width, height: rect.height });
// applyTransform // applyTransform
setTimeout(() => { setTimeout(() => {
@ -87,12 +88,13 @@ const CameraView = ({
// //
const handleImageLoad = () => { const handleImageLoad = () => {
setStreamError(false); //
if (imgRef.current) { if (imgRef.current) {
const img = imgRef.current; const img = imgRef.current;
if (img.naturalWidth && img.naturalHeight) { if (img.naturalWidth && img.naturalHeight) {
setVideoNaturalWidth(img.naturalWidth); setVideoNaturalWidth(img.naturalWidth);
setVideoNaturalHeight(img.naturalHeight); setVideoNaturalHeight(img.naturalHeight);
// console.log(`handleImageLoad: : ${img.naturalWidth}x${img.naturalHeight}`); // console.log(`handleImageLoad: : ${img.naturalWidth}x${img.naturalHeight}`);
} }
resizeCanvas(); resizeCanvas();
// //
@ -103,6 +105,18 @@ const CameraView = ({
} }
}; };
//
const handleImageError = () => {
setStreamError(true);
// 3src
setTimeout(() => {
setStreamError(false);
if (imgRef.current) {
imgRef.current.src = streamUrl + "?t=" + Date.now();
}
}, 3000);
};
// //
const handleMouseDown = (e) => { const handleMouseDown = (e) => {
const container = containerRef.current; const container = containerRef.current;
@ -224,7 +238,7 @@ const CameraView = ({
(target) => target.id === clickedRectangle.id (target) => target.id === clickedRectangle.id
); );
if (targetData) { if (targetData) {
// console.log(":", targetData); // console.log(":", targetData);
onRectangleClick(targetData); onRectangleClick(targetData);
} }
} }
@ -272,7 +286,8 @@ const CameraView = ({
const templateParams = selectedTemplate const templateParams = selectedTemplate
? { ? {
// //
name: selectedTemplate.name || `target${rectangles.length + 1}`, name:
selectedTemplate.name || `target${rectangles.length + 1}`,
radius: selectedTemplate.physicalRadius || 40.0, radius: selectedTemplate.physicalRadius || 40.0,
isReferencePoint: selectedTemplate.isBaseline || false, isReferencePoint: selectedTemplate.isBaseline || false,
gradientThreshold: gradientThreshold:
@ -312,13 +327,13 @@ const CameraView = ({
setRectangles((prev) => [...prev, newRect]); setRectangles((prev) => [...prev, newRect]);
// console.log(":", newRect); // console.log(":", newRect);
if (selectedTemplate) { if (selectedTemplate) {
// console.log("使:", selectedTemplate); // console.log("使:", selectedTemplate);
} else { } else {
// console.log("使"); // console.log("使");
} }
// logRectangleData(); // logRectangleData();
} }
}; // }; //
const dragExistingRectangle = (mouseX, mouseY) => { const dragExistingRectangle = (mouseX, mouseY) => {
@ -542,11 +557,15 @@ const CameraView = ({
ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.clearRect(0, 0, canvas.width, canvas.height);
// //
if (!videoNaturalWidth || !videoNaturalHeight) { if (
console.log("redrawAllRectangles: 视频尺寸未准备好", { !videoNaturalWidth ||
videoNaturalWidth, !videoNaturalHeight ||
videoNaturalHeight, videoNaturalWidth < 100 ||
}); videoNaturalHeight < 100
) {
//
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
return; return;
} }
@ -790,7 +809,14 @@ const CameraView = ({
setIsSaving(false); setIsSaving(false);
}, 1000); }, 1000);
} }
}, [rectangles, isConnected, sendMessage, refreshTargets, onClearSelection, isSaving]); }, [
rectangles,
isConnected,
sendMessage,
refreshTargets,
onClearSelection,
isSaving,
]);
// alt+ // alt+
useEffect(() => { useEffect(() => {
@ -843,7 +869,6 @@ const CameraView = ({
return () => cancelAnimationFrame(rafId); return () => cancelAnimationFrame(rafId);
}, [rectangles]); }, [rectangles]);
// //
useEffect(() => { useEffect(() => {
// console.log("targets loading :", { // console.log("targets loading :", {
@ -852,7 +877,13 @@ const CameraView = ({
// targetsLength: targets?.length, // targetsLength: targets?.length,
// }); // });
if (!loading && targets && targets.length > 0) { if (
!loading &&
targets &&
targets.length > 0 &&
videoNaturalWidth > 0 &&
videoNaturalHeight > 0
) {
// console.log(":", targets); // console.log(":", targets);
const initialRectangles = targets const initialRectangles = targets
@ -912,7 +943,7 @@ const CameraView = ({
setRectangles([]); setRectangles([]);
// console.log(""); // console.log("");
} }
}, [targets, loading]); // targetsloading }, [targets, loading, videoNaturalWidth, videoNaturalHeight]); // targetsloading
// rectangles // rectangles
useEffect(() => { useEffect(() => {
@ -1058,7 +1089,7 @@ const CameraView = ({
src={streamUrl} src={streamUrl}
alt="MJPEG 视频流" alt="MJPEG 视频流"
onLoad={handleImageLoad} onLoad={handleImageLoad}
onError={() => console.error("视频流加载失败")} onError={handleImageError}
style={{ style={{
position: "absolute", position: "absolute",
left: 0, left: 0,
@ -1070,6 +1101,27 @@ const CameraView = ({
pointerEvents: "none", pointerEvents: "none",
}} }}
/> />
{/* 视频流断开提示 */}
{streamError && (
<div
style={{
position: "absolute",
left: 0,
top: 0,
width: "100%",
height: "100%",
background: "rgba(0,0,0,0.7)",
color: "#fff",
display: "flex",
alignItems: "center",
justifyContent: "center",
fontSize: 24,
zIndex: 100,
}}
>
视频流断开正在尝试重连...
</div>
)}
</div> </div>
{/* 绘制画布 */} {/* 绘制画布 */}
@ -1121,7 +1173,9 @@ const CameraView = ({
position: "absolute", position: "absolute",
top: "10px", top: "10px",
right: "10px", right: "10px",
backgroundColor: isSaving ? "rgba(128, 128, 128, 0.7)" : "rgba(0, 100, 0, 0.7)", backgroundColor: isSaving
? "rgba(128, 128, 128, 0.7)"
: "rgba(0, 100, 0, 0.7)",
color: "white", color: "white",
border: "1px solid rgba(255, 255, 255, 0.3)", border: "1px solid rgba(255, 255, 255, 0.3)",
borderRadius: "4px", borderRadius: "4px",
@ -1162,7 +1216,7 @@ const CameraView = ({
}} }}
> >
<div> <div>
分辨率: {videoNaturalWidth} × {videoNaturalHeight} 摄像头分辨率: {videoNaturalWidth} × {videoNaturalHeight}
</div> </div>
<div>缩放: {Math.round(scale * 100)}%</div> <div>缩放: {Math.round(scale * 100)}%</div>
<div> <div>

73
client/src/sections/wuyuanbiaoba/components/RealtimeCharts.jsx

@ -60,7 +60,7 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
labels: [], labels: [],
deviceIds: [], deviceIds: [],
timeGroups: {}, timeGroups: {},
sortedTimes: [] sortedTimes: [],
}; };
} }
@ -70,7 +70,9 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
data.forEach((item) => { data.forEach((item) => {
if (!item || !item.updateTime) return; if (!item || !item.updateTime) return;
const timeKey = Math.floor(new Date(item.updateTime).getTime() / 1000); // const timeKey = Math.floor(
new Date(item.updateTime).getTime() / 1000
); //
if (!timeGroups[timeKey]) { if (!timeGroups[timeKey]) {
timeGroups[timeKey] = []; timeGroups[timeKey] = [];
} }
@ -84,25 +86,30 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
.reverse(); .reverse();
// ID // ID
const deviceIds = [...new Set(data.map((item) => item?.deviceId).filter(Boolean))].sort(); const deviceIds = [
...new Set(data.map((item) => item?.deviceId).filter(Boolean)),
].sort();
// //
const labels = sortedTimes.map((timeKey) => { const labels = sortedTimes.map((timeKey) => {
return new Date(Number(timeKey) * 1000).toLocaleTimeString("zh-CN", { return new Date(Number(timeKey) * 1000).toLocaleTimeString(
hour: "2-digit", "zh-CN",
minute: "2-digit", {
second: "2-digit", hour: "2-digit",
}); minute: "2-digit",
second: "2-digit",
}
);
}); });
return { labels, deviceIds, timeGroups, sortedTimes }; return { labels, deviceIds, timeGroups, sortedTimes };
} catch (error) { } catch (error) {
console.error('数据采样出错:', error); console.error("数据采样出错:", error);
return { return {
labels: [], labels: [],
deviceIds: [], deviceIds: [],
timeGroups: {}, timeGroups: {},
sortedTimes: [] sortedTimes: [],
}; };
} }
}; };
@ -110,7 +117,8 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
// X // X
const xChartData = useMemo(() => { const xChartData = useMemo(() => {
try { try {
const { labels, deviceIds, timeGroups, sortedTimes } = sampleData(tableData); const { labels, deviceIds, timeGroups, sortedTimes } =
sampleData(tableData);
if (!deviceIds || deviceIds.length === 0) { if (!deviceIds || deviceIds.length === 0) {
return { labels: [], datasets: [] }; return { labels: [], datasets: [] };
@ -122,7 +130,9 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
const deviceData = timeData.find( const deviceData = timeData.find(
(item) => item.deviceId === deviceId (item) => item.deviceId === deviceId
); );
return deviceData && deviceData.xValue !== undefined ? parseFloat(deviceData.xValue) : null; return deviceData && deviceData.xValue !== undefined
? parseFloat(deviceData.xValue)
: null;
}); });
return { return {
@ -140,7 +150,7 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
return { labels, datasets }; return { labels, datasets };
} catch (error) { } catch (error) {
console.error('准备X轴图表数据出错:', error); console.error("准备X轴图表数据出错:", error);
return { labels: [], datasets: [] }; return { labels: [], datasets: [] };
} }
}, [tableData]); }, [tableData]);
@ -148,7 +158,8 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
// Y // Y
const yChartData = useMemo(() => { const yChartData = useMemo(() => {
try { try {
const { labels, deviceIds, timeGroups, sortedTimes } = sampleData(tableData); const { labels, deviceIds, timeGroups, sortedTimes } =
sampleData(tableData);
if (!deviceIds || deviceIds.length === 0) { if (!deviceIds || deviceIds.length === 0) {
return { labels: [], datasets: [] }; return { labels: [], datasets: [] };
@ -160,7 +171,9 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
const deviceData = timeData.find( const deviceData = timeData.find(
(item) => item.deviceId === deviceId (item) => item.deviceId === deviceId
); );
return deviceData && deviceData.yValue !== undefined ? parseFloat(deviceData.yValue) : null; return deviceData && deviceData.yValue !== undefined
? parseFloat(deviceData.yValue)
: null;
}); });
return { return {
@ -178,7 +191,7 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
return { labels, datasets }; return { labels, datasets };
} catch (error) { } catch (error) {
console.error('准备Y轴图表数据出错:', error); console.error("准备Y轴图表数据出错:", error);
return { labels: [], datasets: [] }; return { labels: [], datasets: [] };
} }
}, [tableData]); }, [tableData]);
@ -301,9 +314,9 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
// //
// useEffect(() => { // useEffect(() => {
// console.log('RealtimeCharts - tableData:', tableData); // console.log('RealtimeCharts - tableData:', tableData);
// console.log('RealtimeCharts - xChartData:', xChartData); // console.log('RealtimeCharts - xChartData:', xChartData);
// console.log('RealtimeCharts - yChartData:', yChartData); // console.log('RealtimeCharts - yChartData:', yChartData);
// }, [tableData, xChartData, yChartData]); // }, [tableData, xChartData, yChartData]);
// //
@ -325,13 +338,15 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
style={{ marginLeft: "16px", fontSize: "12px" }} style={{ marginLeft: "16px", fontSize: "12px" }}
/> />
</Title> </Title>
<div style={{ <div
flex: 1, style={{
display: "flex", flex: 1,
alignItems: "center", display: "flex",
justifyContent: "center", alignItems: "center",
minHeight: "500px" justifyContent: "center",
}}> minHeight: "500px",
}}
>
<div style={{ textAlign: "center", color: "#999" }}> <div style={{ textAlign: "center", color: "#999" }}>
<p>等待实时数据...</p> <p>等待实时数据...</p>
</div> </div>
@ -356,7 +371,13 @@ const RealtimeCharts = ({ tableData, lastUpdateTime }) => {
text={`实时更新 - 最后更新: ${lastUpdateTime.toLocaleTimeString()}`} text={`实时更新 - 最后更新: ${lastUpdateTime.toLocaleTimeString()}`}
style={{ marginLeft: "16px", fontSize: "12px" }} style={{ marginLeft: "16px", fontSize: "12px" }}
/> />
<Badge
status="default"
text={`图表数据采样频率:1Hz`}
style={{ marginLeft: "16px", fontSize: "12px" }}
/>
</Title> </Title>
<div <div
style={{ style={{
flex: 1, flex: 1,

4
client/src/sections/wuyuanbiaoba/hooks/useTargetStorage.js

@ -59,7 +59,7 @@ export const useTargetStorage = () => {
if (targetDataResponse && targetDataResponse.values) { if (targetDataResponse && targetDataResponse.values) {
try { try {
const responseValues = targetDataResponse.values; const responseValues = targetDataResponse.values;
console.log('收到标靶数据:', responseValues); // console.log('收到标靶数据:', responseValues);
// 检查是否为空对象 // 检查是否为空对象
if (!responseValues || Object.keys(responseValues).length === 0) { if (!responseValues || Object.keys(responseValues).length === 0) {
@ -101,7 +101,7 @@ export const useTargetStorage = () => {
setTargets(formattedTargets); setTargets(formattedTargets);
setError(null); setError(null);
console.log('解析到标靶数据:', formattedTargets); // console.log('解析到标靶数据:', formattedTargets);
} catch (err) { } catch (err) {
console.error('处理标靶数据失败:', err); console.error('处理标靶数据失败:', err);
setError('处理标靶数据失败'); setError('处理标靶数据失败');

Loading…
Cancel
Save