Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | 'use client' import { useCallback, useEffect, useState } from 'react' import type { CalibrationGrid, StoredCalibration } from '@/types/vision' import { CALIBRATION_STORAGE_KEY } from '@/types/vision' export interface UseCameraCalibrationReturn { /** Whether a valid calibration exists */ isCalibrated: boolean /** Current calibration grid */ calibration: CalibrationGrid | null /** Whether currently in calibration mode */ isCalibrating: boolean /** Start interactive calibration mode */ startCalibration: () => void /** Update calibration during drag */ updateCalibration: (partial: Partial<CalibrationGrid>) => void /** Finish and save calibration */ finishCalibration: () => void /** Cancel calibration without saving */ cancelCalibration: () => void /** Reset/clear saved calibration */ resetCalibration: () => void /** Load calibration from localStorage */ loadCalibration: (deviceId?: string) => CalibrationGrid | null /** Create default calibration for given dimensions */ createDefaultCalibration: ( videoWidth: number, videoHeight: number, columnCount: number ) => CalibrationGrid /** Set the current device ID for saving calibration */ setDeviceId: (deviceId: string) => void } /** * Hook for managing camera calibration with localStorage persistence */ export function useCameraCalibration(): UseCameraCalibrationReturn { const [calibration, setCalibration] = useState<CalibrationGrid | null>(null) const [isCalibrating, setIsCalibrating] = useState(false) const [currentDeviceId, setCurrentDeviceId] = useState<string | null>(null) const isCalibrated = calibration !== null /** * Create a default calibration grid centered in the video */ const createDefaultCalibration = useCallback( (videoWidth: number, videoHeight: number, columnCount: number): CalibrationGrid => { // Default to center 60% of video const roiWidth = videoWidth * 0.6 const roiHeight = videoHeight * 0.7 const roiX = (videoWidth - roiWidth) / 2 const roiY = (videoHeight - roiHeight) / 2 // Create evenly-spaced column dividers const columnDividers: number[] = [] for (let i = 1; i < columnCount; i++) { columnDividers.push(i / columnCount) } return { roi: { x: roiX, y: roiY, width: roiWidth, height: roiHeight, }, columnCount, columnDividers, rotation: 0, } }, [] ) /** * Load calibration from localStorage for a specific device */ const loadCalibration = useCallback((deviceId?: string): CalibrationGrid | null => { try { const stored = localStorage.getItem(CALIBRATION_STORAGE_KEY) if (!stored) return null const data = JSON.parse(stored) as StoredCalibration if (data.version !== 1) return null // If deviceId specified, only use if it matches if (deviceId && data.deviceId !== deviceId) { return null } return data.grid } catch { return null } }, []) /** * Save calibration to localStorage */ const saveCalibration = useCallback((grid: CalibrationGrid, deviceId: string) => { const stored: StoredCalibration = { version: 1, grid, createdAt: new Date().toISOString(), deviceId, } localStorage.setItem(CALIBRATION_STORAGE_KEY, JSON.stringify(stored)) }, []) /** * Start calibration mode */ const startCalibration = useCallback(() => { setIsCalibrating(true) }, []) /** * Update calibration during interactive adjustment * Can also set a complete new calibration if none exists */ const updateCalibration = useCallback((partial: Partial<CalibrationGrid>) => { setCalibration((prev) => { // If we have a complete grid (has all required fields), use it directly if ('roi' in partial && 'columnCount' in partial && 'columnDividers' in partial) { return partial as CalibrationGrid } // Otherwise merge with existing if (!prev) return prev return { ...prev, ...partial } }) }, []) /** * Finish calibration and save */ const finishCalibration = useCallback(() => { if (calibration && currentDeviceId) { saveCalibration(calibration, currentDeviceId) } setIsCalibrating(false) }, [calibration, currentDeviceId, saveCalibration]) /** * Cancel calibration without saving */ const cancelCalibration = useCallback(() => { // Reload saved calibration const saved = loadCalibration(currentDeviceId ?? undefined) setCalibration(saved) setIsCalibrating(false) }, [currentDeviceId, loadCalibration]) /** * Reset/clear saved calibration */ const resetCalibration = useCallback(() => { localStorage.removeItem(CALIBRATION_STORAGE_KEY) setCalibration(null) setIsCalibrating(false) }, []) /** * Initialize calibration with device ID and optionally load from storage */ const initializeCalibration = useCallback( (deviceId: string, videoWidth: number, videoHeight: number, columnCount: number) => { setCurrentDeviceId(deviceId) // Try to load saved calibration const saved = loadCalibration(deviceId) if (saved) { setCalibration(saved) } else { // Create default calibration setCalibration(createDefaultCalibration(videoWidth, videoHeight, columnCount)) } }, [loadCalibration, createDefaultCalibration] ) /** * Set the device ID for saving calibration */ const setDeviceId = useCallback((deviceId: string) => { setCurrentDeviceId(deviceId) }, []) return { isCalibrated, calibration, isCalibrating, startCalibration, updateCalibration, finishCalibration, cancelCalibration, resetCalibration, loadCalibration, createDefaultCalibration, setDeviceId, } } |