WebcamCapture.jsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {useRef, useState, useCallback} from 'react';
  2. import Webcam from "react-webcam";
  3. import {Button} from '@mui/material';
  4. import { fileMessage } from '../../../../reducers/messageReducer';
  5. import { useDispatch } from 'react-redux';
  6. const WebcamCapture = () => {
  7. const dispatch = useDispatch();
  8. const webcamRef = useRef(null);
  9. const [imgSrc, setImgSrc] = useState(null);
  10. const capture = useCallback(() => {
  11. const imageSrc = webcamRef.current.getScreenshot();
  12. setImgSrc(imageSrc);
  13. // dispatch(fileMessage( imageSrc))
  14. }, [webcamRef, setImgSrc]);
  15. return (
  16. <div style={{
  17. 'maxHeight': '350px',
  18. 'maxWidth': '350px',
  19. 'position': 'absolute',
  20. 'top': '10%',
  21. "left": '10%',
  22. 'zIndex': 9999,
  23. }}>
  24. <Webcam
  25. audio={false}
  26. ref={webcamRef}
  27. screenshotFormat="image/jpeg"
  28. style={{
  29. 'maxHeight': '350px',
  30. 'maxWidth': '350px'
  31. }}
  32. />
  33. <Button
  34. onClick={capture}
  35. variant="contained"
  36. component="label"
  37. >
  38. New photo
  39. </Button> {imgSrc && (
  40. <>
  41. <Button
  42. variant="contained"
  43. component="label"
  44. >
  45. Send
  46. </Button>
  47. <Button
  48. variant="contained"
  49. component="label"
  50. >
  51. Save avatar
  52. </Button>
  53. <img
  54. src={imgSrc}
  55. style={{
  56. 'border':'4px solid blue',
  57. 'borderRadius': '10px'
  58. }}
  59. />
  60. </>
  61. )}
  62. </div>
  63. );
  64. };
  65. export default WebcamCapture;