How To Open Web/Mobile Camera And Take a Photo From SPFx WebPart
- Create a project folder
- run below command
yo @microsoft/sharepoint
Use the below standard options. Please make sure you select React as Javascript framework
npm install react-webcam
Then run the below command.
npm install @types/react-webcam
import * as Webcam from "react-webcam";
import * as ReactDom from 'react-dom';
Please note that package documentation has different syntax to import react-webcam package, but won’t work with SPFx hence, use * to import all components in react-webcam.
export default class SpFxWebCam extends React.Component<ISpFxWebCamProps,{imageData: string, image_name:string,webcam:Webcam}> {
Replace render method with below,
public render(): React.ReactElement<ISpFxWebCamProps> {
return (
<div>
<div className={ styles.spFxWebCam }>
<div className={ styles.container }>
<div className={ styles.row }>
<div className={ styles.column }>
<span className={ styles.title }>SPFx Web/Mobile Camera Demo </span>
<p className={ styles.subTitle }>This is demo of how to open webcam and take photo from SPFx webpart.
It will open camera in mobile web browser also</p>
<a onClick={() => this.opencam()} className={ styles.button }>
<span className={ styles.label }>Open webcam</span>
</a>
<a onClick={() => this.capture()} className={ styles.button }>
<span className={ styles.label }>Take Photo</span>
</a>
<a onClick={() => this.close()} className={ styles.button }>
<span className={ styles.label }>Close webcam</span>
</a>
</div>
</div>
</div>
</div>
<div id="camContainer">
</div>
<div id="capturedPhoto">
</div>
</div>
);
}
Let us understand what are we doing here in render method.
- Added 3 buttons, open cam, take a photo and close cam and bind event handler to respective method. We will add events method in the next step.
- Added one div camContainer
where we will add Camera object.
- Added one div capturedPhoto
where we will render captured photo from camera.
private opencam () {
const element2: React.ReactElement<Webcam.WebcamProps > = React.createElement(
Webcam,
{
height:350,
width:350,
screenshotFormat:"image/jpeg",
ref:this.setRef,
}
);
ReactDom.render(element2, document.getElementById("camContainer"));
}
private setRef = (webcam) => {
this.setState({webcam:webcam})
}
This method is setting webcam reference which we just created above to the local component’s variable webcam. This will allow us to use webcam properties and methods to the scope of this class (SpFxWebCam).
private capture(){
const imageSrc = this.state.webcam.getScreenshot();
const element = React.createElement(
'img',
{
src:imageSrc
}
);
ReactDom.render(element, document.getElementById("capturedPhoto"));
}
This method will be called when user clicks on ‘Take Photo’ button. Here the first thing we are doing is using Webcam’s getScreenshot method which will take a photo and return as base64 string of the captured photo. We are creating html element img using React.createElement method and providing src as base 64 string. And again finally rendering it to container which we defined in render method.
public close(){
ReactDom.unmountComponentAtNode(document.getElementById('camContainer'));
}
This method will be called when the user clicks on ‘Close webcam’. Here we are unmounting webcam from DOM. This will remove camera element from the page.
- Take Photo and update User profile picture using Graph API.
- Store photo in SharePoint document library using JSOM, REST API.
Note – This article was originally published at this link.