Introduction
React WebCam is a 3rd party plugin for operating a webcam in React Projects
Open a command prompt. Create a directory for the SPFx solution.
md spfx-React-Webcam
Navigate to the above-created directory.
cd spfx-React-Webcam
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter for a default name, (spfx-React-Webcam in this case), or type in any other name for your solution.
Selected choice - Hit Enter
Target for the component
Here, we can select the target environment where we are planning to deploy the client web part, i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest)
Place of files
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - Same folder
Deployment option
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly)
Permissions to access web APIs
Choose if the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
Type of client-side component to create
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
Web part name
Hit Enter to select the default name or type in any other name.
Selected choice - SpfxReactWebcam
Web part description
Hit Enter to select the default description or type in any other value.
Framework to use
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - React
Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command:
npm shrinkwrap
In the command prompt, type below command to open the solution in the code editor of your choice.
code .
NPM Packages Used,
On the command prompt, run below command.
  1. npm i @pnp/logging @pnp/common @pnp/odata @pnp/sp --save
for Pollyfills
  1. npm install --save @pnp/polyfill-ie11
for react Webcam
  1. npm install react-webcam
Code Piece for Intialize Webcam
  1. import Webcam from "react-webcam";
  2. <Webcam
  3. audio={false}
  4. height={550}
  5. ref={webcamRef}
  6. screenshotFormat="image/jpeg"
  7. width={550}
  8. videoConstraints={videoConstraints}
  9. />
in SpfxReactWebcam.tsx
  1. import * as React from 'react';
  2. /*import styles from './SpfxReactWebcam.module.scss';*/
  3. import { ISpfxReactWebcamProps } from './ISpfxReactWebcamProps';
  4. import { escape } from '@microsoft/sp-lodash-subset';
  5. import {WebcamCapture} from './reactwebcam';
  6. export default class SpfxReactWebcam extends React.Component<ISpfxReactWebcamProps, {}> {
  7. public render(): React.ReactElement<ISpfxReactWebcamProps> {
  8. return (
  9. <div>
  10. <WebcamCapture/>
  11. </div>
  12. );
  13. }
  14. }
in reactwebcam.tsx(Here i am using Hooks Concept)
  1. import * as React from "react";
  2. import Webcam from "react-webcam";
  3. import { sp } from "@pnp/sp";
  4. const videoConstraints = {
  5. width: 1280,
  6. height: 720,
  7. facingMode: "user"
  8. };
  9. export const WebcamCapture = () => {
  10. const [datacol, setdatacol] = React.useState();
  11. const webcamRef = React.useRef(null);
  12. const capture = React.useCallback(
  13. () => {
  14. setdatacol(webcamRef.current.getScreenshot());
  15. },
  16. [webcamRef]
  17. );
  18. const remove = React.useCallback(
  19. () => {
  20. setdatacol("");
  21. },
  22. []
  23. );
  24. let Save= ()=>{
  25. var myblob= b64ToBlob(datacol);
  26. sp.web.getFolderByServerRelativeUrl("/sites/SPCapabilityTeam/MadhanNC/PnpLibrary/").files.add("Madhan.jpg", myblob as Blob, true).then(output => {
  27. });
  28. /*
  29. import { sp } from '@pnp/sp';
  30. (async () => {
  31. const folder = sp.web.getFolderByServerRelativeUrl('/sites/site/Shared Documents');
  32. const { file, data } = await folder.files.add('new-one.txt', 'data');
  33. const item = await file.getItem(`Id&_hash=${new Date().toISOString()}`); // force no-cashed response
  34. await item.update({ Title: new Date().toISOString() });
  35. })().catch(console.warn);
  36. web.getFolderByServerRelativeUrl("/sites/dev/Shared Documents/test/").files.add("abcd.txt", file, true).then(f => {
  37. f.file.listItemAllFields.get(new ODataDefaultParser(), {
  38. headers: {
  39. "Cache-Control": "no-cache",
  40. }
  41. }).then(function (item) {
  42. item.update({
  43. Title: "Updated Title",
  44. Description: "Updated desc"
  45. });
  46. });
  47. });
  48. });
  49. */
  50. };
  51. return (
  52. <>
  53. <Webcam
  54. audio={false}
  55. height={550}
  56. ref={webcamRef}
  57. screenshotFormat="image/jpeg"
  58. width={550}
  59. videoConstraints={videoConstraints}
  60. />
  61. <div> {datacol ? <img src={datacol} /> : null} </div>
  62. <button onClick={capture}>Capture photo</button>
  63. <button onClick={remove}>remove photo</button>
  64. <button onClick={Save}>Save photo</button>
  65. </>
  66. );
  67. };
  68. export const b64ToBlob = (base64: string, type: string = 'application/octet-stream'): Blob => {
  69. const byteArray = Uint8Array.from(
  70. window.atob(base64.replace("data:image/jpeg;base64,",""))
  71. .split('')
  72. .map((char) => char.charCodeAt(0))
  73. );
  74. return new Blob([byteArray], { type });
  75. };
  76. export const b64ToArrayBuffer = (base64: string): ArrayBuffer => {
  77. const binaryString = window.atob(base64.replace("data:image/jpeg;base64,",""));
  78. const len = binaryString.length;
  79. const bytes = new Uint8Array(len);
  80. for (let i = 0; i < len; i++) {
  81. bytes[i] = binaryString.charCodeAt(i);
  82. }
  83. return bytes.buffer;
  84. };
webcamRef.current.getScreenshot() method gets the captured image value in the form of base64.
To upload a file to the sharepoint document library, we
either need to convert it to blob, or arraybuffer. I've mentioned both methods above

The code for uploading an image:
  1. var myblob= b64ToBlob(datacol);
  2. sp.web.getFolderByServerRelativeUrl("/sites/SPCapabilityTeam/MadhanNC/PnpLibrary/").files.add("Madhan.jpg", myblob as Blob, true).then(output => { });
or...
  1. var myarraybuffer= b64ToArrayBuffer(datacol);
  2. sp.web.getFolderByServerRelativeUrl("/sites/SPCapabilityTeam/MadhanNC/PnpLibrary/").files.add("Madhan.jpg", myarraybuffer as ArrayBuffer, true).then(output => { });
Here i am using PnpLibrary as my document library and SPcapabilityTeam/MadhanNC as the site
Here, the first image is a live image and the second one is a captured image.
I hope that this helps someone. Happy Coding :)