Creating and Styling a ClCamera Component
mkdir ClCamera
cd ClCamera
touch index.js ClCamera.css # on Windows, run the command
# copy NUL index.js
# copy NUL ClCamera.css yarn add axios # or npm install axios // src/components/ClCamera.js
import React, { Component } from 'react';
import { Webcam } from '../../webcam';
import './ClCamera.css';
import axios from 'axios';
class ClCamera extends Component {
constructor() {
super();
this.webcam = null;
this.state = {
capturedImage: null,
captured: false,
uploading: false
}
}
componentDidMount() {
// initialize the camera
this.canvasElement = document.createElement('canvas');
this.webcam = new Webcam(
document.getElementById('webcam'),
this.canvasElement
);
this.webcam.setup().catch(() => {
alert('Error getting access to your camera');
});
}
componentDidUpdate(prevProps) {
if (!this.props.offline && (prevProps.offline === true)) {
// if its online
this.batchUploads();
}
}
render() {
const imageDisplay = this.state.capturedImage ?
<img src={this.state.capturedImage} alt="captured" width="350" />
:
<span />;
const buttons = this.state.captured ?
<div>
<button className="deleteButton" onClick={this.discardImage} > Delete Photo </button>
<button className="captureButton" onClick={this.uploadImage} > Upload Photo </button>
</div> :
<button className="captureButton" onClick={this.captureImage} > Take Picture </button>
const uploading = this.state.uploading ?
<div><p> Uploading Image, please wait ... </p></div>
:
<span />
return (
<div>
{uploading}
<video autoPlay playsInline muted id="webcam" width="100%" height="200" />
<br />
<div className="imageCanvas">
{imageDisplay}
</div>
{buttons}
</div>
)
}
[...]Last updated
Was this helpful?