2025-11-21 10:12:42 +00:00
|
|
|
import { useEffect, useRef } from "react";
|
|
|
|
|
import { useGetVideoFeed } from "./useGetVideoFeed";
|
2025-11-27 10:43:56 +00:00
|
|
|
import { useCameraFeedContext } from "../../../app/context/CameraFeedContext";
|
2025-11-21 10:12:42 +00:00
|
|
|
|
|
|
|
|
export const useCreateVideoSnapshot = () => {
|
2025-11-27 10:43:56 +00:00
|
|
|
const { state } = useCameraFeedContext();
|
|
|
|
|
const cameraFeedID = state?.cameraFeedID;
|
2025-12-09 12:39:03 +00:00
|
|
|
const mode = state.modeByCamera[cameraFeedID];
|
2025-11-21 10:12:42 +00:00
|
|
|
const latestBitmapRef = useRef<ImageBitmap | null>(null);
|
2025-12-09 12:39:03 +00:00
|
|
|
const { targetDetectionQuery, videoFeedQuery } = useGetVideoFeed(cameraFeedID, mode);
|
2025-11-21 10:12:42 +00:00
|
|
|
|
2025-12-09 12:39:03 +00:00
|
|
|
let snapShot = targetDetectionQuery?.data;
|
|
|
|
|
const isloading = targetDetectionQuery.isPending;
|
|
|
|
|
|
|
|
|
|
const videoSnapShot = videoFeedQuery?.data;
|
|
|
|
|
const isVideoLoading = videoFeedQuery.isPending;
|
|
|
|
|
|
2025-12-10 13:09:07 +00:00
|
|
|
if ((isVideoLoading === false && videoSnapShot && mode === "magnify") || mode === "zoom") {
|
2025-12-09 12:39:03 +00:00
|
|
|
snapShot = videoSnapShot;
|
|
|
|
|
}
|
2025-11-21 10:12:42 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
async function createBitmap() {
|
|
|
|
|
if (!snapShot) return;
|
|
|
|
|
|
|
|
|
|
try {
|
2025-12-12 12:18:17 +00:00
|
|
|
const bitmap = await createImageBitmap(snapShot, {
|
|
|
|
|
resizeWidth: 720,
|
|
|
|
|
resizeHeight: 1080,
|
|
|
|
|
resizeQuality: "high",
|
|
|
|
|
});
|
2025-11-21 16:01:34 +00:00
|
|
|
if (!bitmap) return;
|
2025-11-21 10:12:42 +00:00
|
|
|
latestBitmapRef.current = bitmap;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
createBitmap();
|
|
|
|
|
}, [snapShot]);
|
|
|
|
|
|
2025-11-24 12:19:51 +00:00
|
|
|
return { latestBitmapRef, isloading };
|
2025-11-21 10:12:42 +00:00
|
|
|
};
|