-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: image input and filter hook.js warning
compatible with current backend
- Loading branch information
Showing
6 changed files
with
143 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useCallback } from 'react'; | ||
import { useCamera } from '@/context/camera-context'; | ||
import { useScreenCaptureContext } from '@/context/screen-capture-context'; | ||
|
||
// Add type definition for ImageCapture | ||
declare class ImageCapture { | ||
constructor(track: MediaStreamTrack); | ||
|
||
grabFrame(): Promise<ImageBitmap>; | ||
} | ||
|
||
interface ImageData { | ||
source: 'camera' | 'screen'; | ||
data: string; | ||
mime_type: string; | ||
} | ||
|
||
export function useMediaCapture() { | ||
const { stream: cameraStream } = useCamera(); | ||
const { stream: screenStream } = useScreenCaptureContext(); | ||
|
||
const captureFrame = useCallback(async (stream: MediaStream | null) => { | ||
if (!stream) return null; | ||
|
||
const videoTrack = stream.getVideoTracks()[0]; | ||
if (!videoTrack) return null; | ||
|
||
const imageCapture = new ImageCapture(videoTrack); | ||
try { | ||
const bitmap = await imageCapture.grabFrame(); | ||
const canvas = document.createElement('canvas'); | ||
canvas.width = bitmap.width; | ||
canvas.height = bitmap.height; | ||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) return null; | ||
|
||
ctx.drawImage(bitmap, 0, 0); | ||
return canvas.toDataURL('image/jpeg', 0.8); | ||
} catch (error) { | ||
console.error('Error capturing frame:', error); | ||
return null; | ||
} | ||
}, []); | ||
|
||
const captureAllMedia = useCallback(async () => { | ||
const images: ImageData[] = []; | ||
|
||
// Capture camera frame | ||
if (cameraStream) { | ||
const cameraFrame = await captureFrame(cameraStream); | ||
if (cameraFrame) { | ||
images.push({ | ||
source: 'camera', | ||
data: cameraFrame, | ||
mime_type: 'image/jpeg', | ||
}); | ||
} | ||
} | ||
|
||
// Capture screen frame | ||
if (screenStream) { | ||
const screenFrame = await captureFrame(screenStream); | ||
if (screenFrame) { | ||
images.push({ | ||
source: 'screen', | ||
data: screenFrame, | ||
mime_type: 'image/jpeg', | ||
}); | ||
} | ||
} | ||
|
||
console.log("images: ", images); | ||
|
||
return images; | ||
}, [cameraStream, screenStream, captureFrame]); | ||
|
||
return { | ||
captureAllMedia, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters