webcam.mdx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ---
  2. sidebar_position: 1
  3. slug: /webcam
  4. ---
  5. import Tabs from '@theme/Tabs';
  6. import TabItem from '@theme/TabItem';
  7. import UppyCdnExample from '/src/components/UppyCdnExample';
  8. # Webcam
  9. The `@uppy/webcam` plugin lets you take photos and record videos with a built-in
  10. camera on desktop and mobile devices.
  11. :::tip
  12. [Try out the live example](/examples) or take it for a spin in
  13. [StackBlitz](https://stackblitz.com/edit/vitejs-vite-zaqyaf?file=main.js).
  14. :::
  15. ## When should I use it?
  16. When you want your users to able to use their camera. This plugin is published
  17. separately but made specifically for the [Dashboard](/docs/dashboard). You can
  18. technically use it without it, but it’s not officially supported.
  19. ## Install
  20. <Tabs>
  21. <TabItem value="npm" label="NPM" default>
  22. ```shell
  23. npm install @uppy/webcam
  24. ```
  25. </TabItem>
  26. <TabItem value="yarn" label="Yarn">
  27. ```shell
  28. yarn add @uppy/webcam
  29. ```
  30. </TabItem>
  31. <TabItem value="cdn" label="CDN">
  32. <UppyCdnExample>
  33. {`
  34. import { Uppy, Dashboard, Webcam } from "{{UPPY_JS_URL}}"
  35. const uppy = new Uppy()
  36. uppy.use(Dashboard, { inline: true, target: 'body' })
  37. uppy.use(Webcam)
  38. `}
  39. </UppyCdnExample>
  40. </TabItem>
  41. </Tabs>
  42. ## Use
  43. :::note
  44. To use the Webcam plugin in Chrome,
  45. [your site must be served over https](https://developers.google.com/web/updates/2015/10/chrome-47-webrtc#public_service_announcements).
  46. This restriction does not apply on `localhost`, so you don’t have to jump
  47. through many hoops during development.
  48. :::
  49. ```js {3,7,11} showLineNumbers
  50. import Uppy from '@uppy/core';
  51. import Dashboard from '@uppy/dashboard';
  52. import Webcam from '@uppy/webcam';
  53. import '@uppy/core/dist/style.min.css';
  54. import '@uppy/dashboard/dist/style.min.css';
  55. import '@uppy/webcam/dist/style.min.css';
  56. new Uppy().use(Dashboard, { inline: true, target: 'body' }).use(Webcam);
  57. ```
  58. ## API
  59. ### Options
  60. #### `id`
  61. A unique identifier for this plugin (`string`, default: `'Webcam'`).
  62. #### `target`
  63. DOM element, CSS selector, or plugin to place the drag and drop area into
  64. (`string`, `Element`, `Function`, or `UIPlugin`, default:
  65. [`Dashboard`](/docs/dashboard)).
  66. #### `countdown`
  67. When taking a picture: the amount of seconds to wait before actually taking a
  68. snapshot (`boolean`, default: `false`).
  69. If set to `false` or 0, the snapshot is taken right away. This also shows a
  70. `Smile!` message through the [Informer](/docs/informer) before the picture is
  71. taken.
  72. #### `onBeforeSnapshot`
  73. A hook function to call before a snapshot is taken (`Function`, default:
  74. `Promise.resolve`).
  75. The Webcam plugin will wait for the returned Promise to resolve before taking
  76. the snapshot. This can be used to carry out variations on the `countdown` option
  77. for example.
  78. #### `modes`
  79. The types of recording modes to allow (`Array`, default: `[]`).
  80. - `video-audio` - Record a video file, capturing both audio and video.
  81. - `video-only` - Record a video file with the webcam, but don’t record audio.
  82. - `audio-only` - Record an audio file with the user’s microphone.
  83. - `picture` - Take a picture with the webcam.
  84. By default, all modes are allowed, and the Webcam plugin will show controls for
  85. recording video as well as taking pictures.
  86. #### `mirror`
  87. Configures whether to mirror preview image from the camera (`boolean`, default:
  88. `true`).
  89. This option is useful when taking a selfie with a front camera: when you wave
  90. your right hand, you will see your hand on the right on the preview screen, like
  91. in the mirror. But when you actually take a picture, it will not be mirrored.
  92. This is how smartphone selfie cameras behave.
  93. #### `videoConstraints`
  94. Configure the [`MediaTrackConstraints`][] (`Object`, default: `{}`).
  95. You can specify acceptable ranges for the resolution of the video stream using
  96. the [`aspectRatio`][], [`width`][], and [`height`][] properties. Each property
  97. takes an object with `{ min, ideal, max }` properties. For example, use
  98. `width: { min: 720, max: 1920, ideal: 1920 }` to allow any width between 720 and
  99. 1920 pixels wide, while preferring the highest resolution.
  100. Devices sometimes have several cameras, front and back, for example.
  101. [`facingMode`][] lets you specify which should be used:
  102. - `user`: The video source is facing toward the user; this includes, for
  103. example, the front-facing camera on a smartphone.
  104. - `environment`: The video source is facing away from the user, thereby viewing
  105. their environment. This is the back camera on a smartphone.
  106. - `left`: The video source is facing toward the user but to their left, such as
  107. a camera aimed toward the user but over their left shoulder.
  108. - `right`: The video source is facing toward the user but to their right, such
  109. as a camera aimed toward the user but over their right shoulder.
  110. For a full list of available properties, check out MDN documentation for
  111. [`MediaTrackConstraints`][].
  112. [`mediatrackconstraints`]:
  113. https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints#Properties_of_video_tracks
  114. [`aspectratio`]:
  115. https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints/aspectRatio
  116. [`width`]:
  117. https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints/width
  118. [`height`]:
  119. https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints/height
  120. [`facingmode`]:
  121. https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints/facingMode
  122. #### `showVideoSourceDropdown`
  123. Configures whether to show a dropdown which enables to choose the video device
  124. to use (`boolean`, default: `false`).
  125. This option will have priority over `facingMode` if enabled.
  126. #### `showRecordingLength`
  127. Configures whether to show the length of the recording while the recording is in
  128. progress (`boolean`, default: `false`).
  129. #### `preferredVideoMimeType`
  130. Set the preferred mime type for video recordings, for example `'video/webm'`
  131. (`string`, default: `null`).
  132. If the browser supports the given mime type, the video will be recorded in this
  133. format. If the browser does not support it, it will use the browser default. If
  134. no preferred video mime type is given, the Webcam plugin will prefer types
  135. listed in the [`allowedFileTypes` restriction](/docs/uppy/#restrictions), if
  136. any.
  137. #### `preferredImageMimeType`
  138. Set the preferred mime type for images, for example `'image/png'` (`string`,
  139. default: `image/jpeg`).
  140. If the browser supports rendering the given mime type, the image will be stored
  141. in this format. Else `image/jpeg` is used by default. If no preferred image mime
  142. type is given, the Webcam plugin will prefer types listed in the
  143. [`allowedFileTypes` restriction](/docs/uppy/#restrictions), if any.
  144. #### `mobileNativeCamera`
  145. Replaces Uppy’s custom camera UI on mobile and tablet with the native device
  146. camera (`Function: boolean` or `boolean`, default: `isMobile()`).
  147. This will show the “Take Picture” and / or “Record Video” buttons, which ones
  148. show depends on the [`modes`](#modes) option.
  149. You can set a boolean to forcefully enable / disable this feature, or a function
  150. which returns a boolean. By default we use the
  151. [`is-mobile`](https://github.com/juliangruber/is-mobile) package.
  152. #### `locale`
  153. ```js
  154. export default {
  155. strings: {
  156. pluginNameCamera: 'Camera',
  157. noCameraTitle: 'Camera Not Available',
  158. noCameraDescription:
  159. 'In order to take pictures or record video, please connect a camera device',
  160. recordingStoppedMaxSize:
  161. 'Recording stopped because the file size is about to exceed the limit',
  162. submitRecordedFile: 'Submit recorded file',
  163. discardRecordedFile: 'Discard recorded file',
  164. // Shown before a picture is taken when the `countdown` option is set.
  165. smile: 'Smile!',
  166. // Used as the label for the button that takes a picture.
  167. // This is not visibly rendered but is picked up by screen readers.
  168. takePicture: 'Take a picture',
  169. // Used as the label for the button that starts a video recording.
  170. // This is not visibly rendered but is picked up by screen readers.
  171. startRecording: 'Begin video recording',
  172. // Used as the label for the button that stops a video recording.
  173. // This is not visibly rendered but is picked up by screen readers.
  174. stopRecording: 'Stop video recording',
  175. // Used as the label for the recording length counter. See the showRecordingLength option.
  176. // This is not visibly rendered but is picked up by screen readers.
  177. recordingLength: 'Recording length %{recording_length}',
  178. // Title on the “allow access” screen
  179. allowAccessTitle: 'Please allow access to your camera',
  180. // Description on the “allow access” screen
  181. allowAccessDescription:
  182. 'In order to take pictures or record video with your camera, please allow camera access for this site.',
  183. },
  184. };
  185. ```