在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:MABelanger/jslib-html5-camera-photo开源软件地址:https://github.com/MABelanger/jslib-html5-camera-photo开源编程语言:JavaScript 92.2%开源软件介绍:jslib-html5-camera-photoThe first objective of this package comes from the need to have a js library that can help me to capture picture from mobile or desktop camera through the browser with the HTML5 video and canvas elements. So instead of using the native Another js camera ? Yes! I found webcamjs and jpeg_camera but i need to switch easily from camera Features of the library.
Simple Live Demohttps://mabelanger.github.io/jslib-html5-camera-photo/ supported browsers (getUserMedia)https://caniuse.com/#search=getUserMedia ...(as April 2018) Available camera facingModes
Below is an illustration of the video facing modes in relation to the user.src : https://www.w3.org/TR/mediacapture-streams/#dom-videofacingmodeenum Getting startedYou can use the library with vanilla JavaScript, React, Jquery, Angular... Installationnpm install --save jslib-html5-camera-photo yarn add jslib-html5-camera-photo UsageConstructorimport CameraPhoto, { FACING_MODES, IMAGE_TYPES } from 'jslib-html5-camera-photo';
// get your video element with his corresponding id from the html
let videoElement = document.getElementById('videoId');
// pass the video element to the constructor.
let cameraPhoto = new CameraPhoto(videoElement); parameters of startCameracameraPhoto.startCamera(cameraDevice, resolution)
Start the default mode (facing Mode & resolution)If you do not specify any prefer resolution and facing mode, the default is used. The function return a promise. If the promises success it will give you the stream if you want to use it. If it fail, it will give you the error. // default camera and resolution
cameraPhoto.startCamera()
.then((stream)=>{/* ... */})
.catch((error)=>{/* ... */}); Start with ideal facing Mode & default resolution// environment (camera point to environment)
cameraPhoto.startCamera(FACING_MODES.ENVIRONMENT, {})
.then((stream)=>{/* ... */})
.catch((error)=>{/* ... */});
// OR user (camera point to the user)
cameraPhoto.startCamera(FACING_MODES.USER, {})
.then((stream)=>{/* ... */})
.catch((error)=>{/* ... */}); Start with a user-selected deviceId & default resolutionInstead of facing mode, you can specify the deviceId (camera) that you want to use. To know the device id you can get a list of them using see Enumerate the available cameras, so you can start the camera with the // OR specify the deviceId (use a specific camera)
const deviceId = 'the_string_of_device_id';
cameraPhoto.startCamera(deviceId, {})
.then((stream)=>{/* ... */})
.catch((error)=>{/* ... */}); Start with ideal (facing Mode & resolution)// example of ideal resolution 640 x 480
cameraPhoto.startCamera(facingMode, {width: 640, height: 480})
.then((stream)=>{/* ... */})
.catch((error)=>{/* ... */}); Start with the maximum resolutionIt will try the range of width // It will try the best to get the maximum resolution with the specified facingMode
cameraPhoto.startCameraMaxResolution(cameraDevice)
.then((stream)=>{/* ... */})
.catch((error)=>{/* ... */}); getDataUri()Function that return the Parameter (object config)
Available image types
// Use all the default value
const config = {};
let dataUri = cameraPhoto.getDataUri(config);
// OR
// Specify sizeFactor, imageType, imageCompression, isImageMirror
const config = {
sizeFactor : 1;
imageType : IMAGE_TYPES.JPG
imageCompression : .95;
isImageMirror : false;
}
let dataUri = cameraPhoto.getDataUri(config); Get the camera settingsthe function return null if no stream exist (camera not started) or an object with the camera settings attributes of (aspectRatio, frameRate, height, width). let cameraSettigs = cameraPhoto.getCameraSettings();
if (cameraSettigs) {
let {aspectRatio, frameRate, height, width} = cameraSettigs;
let settingsStr =
`aspectRatio:${aspectRatio} ` +
`frameRate: ${frameRate} ` +
`height: ${height} ` +
`width: ${width}`;
console.log(settingsStr);
} Enumerate the available cameras
cameraPhoto.enumerateCameras()
.then((cameras)=>{
cameras.forEach((camera) => {
let {kind, label, deviceId} = camera;
let cameraStr = `
kind: ${kind}
label: ${label}
deviceId: ${deviceId}
`;
console.log(cameraStr)
});
}) plugin - download photoYou can download photo of the dataUri that you took and pass it to import { downloadPhoto } from 'jslib-html5-camera-photo';
let dataUri = cameraPhoto.getDataUri(config);
downloadPhoto(dataUri, prefixFileName, number);
// The filename will be saved as the format :
`${prefixFileName}-${number}.jpg|png}` the parameters of the
Stop the cameraFunction that stop the camera. If it success, no value is returned. It can fail if they is no camera to stop because the camera has already been stopped or never started. It will give a parameter of // It stop the camera
cameraPhoto.stopCamera()
.then(()=>{/* ... */})
.catch((error)=>{/* ... */}); Full example vanilla Js & HTMLHTML<!-- needed to by the camera stream -->
<video id="videoId" autoplay="true"></video>
<!-- needed if you want to display the image when you take a photo -->
<img alt="imgId" id="imgId">
<!--buttons to trigger the actions -->
<button id="takePhotoButtonId">takePhoto</button>
<button id="stopCameraButtonId">stopCamera</button> JavaScriptimport CameraPhoto, { FACING_MODES } from 'jslib-html5-camera-photo';
// get video and image elements from the html
let videoElement = document.getElementById('videoId');
let imgElement = document.getElementById('imgId');
// get buttons elements from the html
let takePhotoButtonElement = document.getElementById('takePhotoButtonId');
let stopCameraButtonElement = document.getElementById('stopCameraButtonId');
// instantiate CameraPhoto with the videoElement
let cameraPhoto = new CameraPhoto(videoElement);
/*
* Start the camera with ideal environment facingMode
* if the environment facingMode is not available, it will fallback
* to the default camera available.
*/
cameraPhoto.startCamera(FACING_MODES.ENVIRONMENT)
.then(() => {
console.log('Camera started !');
})
.catch((error) => {
console.error('Camera not started!', error);
});
// function called by the buttons.
function takePhoto () {
const config = {};
let dataUri = cameraPhoto.getDataUri(config);
imgElement.src = dataUri;
}
function stopCamera () {
cameraPhoto.stopCamera()
.then(() => {
console.log('Camera stoped!');
})
.catch((error) => {
console.log('No camera to stop!:', error);
});
}
// bind the buttons to the right functions.
takePhotoButtonElement.onclick = takePhoto;
stopCameraButtonElement.onclick = stopCamera; Full example with ReactA project with react is build with this library react-html5-camera-photo import React from 'react';
import CameraPhoto, { FACING_MODES } from 'jslib-html5-camera-photo';
class App extends React.Component {
constructor (props, context) {
super(props, context);
this.cameraPhoto = null;
this.videoRef = React.createRef();
this.state = {
dataUri: ''
}
}
componentDidMount () {
// We need to instantiate CameraPhoto inside componentDidMount because we
// need the refs.video to get the videoElement so the component has to be
// mounted.
this.cameraPhoto = new CameraPhoto(this.videoRef.current);
}
startCamera (idealFacingMode, idealResolution) {
this.cameraPhoto.startCamera(idealFacingMode, idealResolution)
.then(() => {
console.log('camera is started !');
})
.catch((error) => {
console.error('Camera not started!', error);
});
}
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论