Git Product home page Git Product logo

vue-use-camera's Introduction

Vue Use Camera

A Vue composable for handling Camera API also utilities for taking photos and videos.

Installation

npm install @alzera/vue-use-camera

Usage

<script setup lang="ts">
import { useCamera, useTakePhoto, useTakeVideo } from '@alzera/vue-use-camera'
const controller = useCamera({
  autoPause: true,
})
const photoController = useTakePhoto(controller)
const videoController = useTakeVideo(controller)

const resultPhoto = ref<string>()
const resultVideo = ref<Blob>()

const selectedChange = (e: Event) => {
  if (!e.target) return;
  const target = e.target as HTMLInputElement
  controller.device.selected = controller.device.list.find((d) => d.deviceId === target.value)
}

const createUrl = (blob: Blob) => {
  return URL.createObjectURL(blob)
}

const takePhoto = async () => {
  resultPhoto.value = await photoController.takeAsDataURL()
  if(resultPhoto.value) resultVideo.value = undefined
}

const takeVideo = async () => {
  resultVideo.value = await videoController.stop()
  if(resultVideo.value) resultPhoto.value = undefined
}
</script>

<template>
  <div id="camera-preview">
    <video :ref="controller.video" preload="none" playsInline muted style="max-width: 500px;"></video><br/>
    <select :value="controller.device.selected?.deviceId" @change="selectedChange">
      <option v-for="(d, i) in controller.device.list" :key="i" :value="d.deviceId">{{ d.label }}</option>
    </select>
    <button @click="takePhoto">Take Photo</button>
    <button v-if="videoController.state === 'inactive'" @click="videoController.start">Start Video</button>
    <button v-if="videoController.state === 'recording'" @click="videoController.pause">Pause Video</button>
    <button v-if="videoController.state === 'paused'" @click="videoController.resume">Resume Video</button>
    <button v-if="videoController.state === 'recording'" @click="takeVideo">Stop Video</button><br/>
    <img v-if="resultPhoto" :src="resultPhoto" />
    <video v-if="resultVideo" :src="createUrl(resultVideo)" controls />
  </div>
</template>

API

useCamera

useCamera({
  onError?: (error: any) => void;
  useLastDeviceId?: boolean;
  autoStart?: boolean;
  autoPause?: boolean;
  constraints?: MediaStreamConstraints;
}): CameraController

useTakePhoto

useTakePhoto(controller: CameraController, mime?: string): UseTakePhoto

useTakeVideo

useTakeVideo(controller: CameraController, mime?: string): UseTakeVideo

CameraController

readonly video: Ref<HTMLVideoElement | undefined>;
readonly camera: {
  readonly capabilities: MediaTrackCapabilities | undefined;
  readonly state: CameraState;
  stream: MediaStream | null;
  torch: boolean;
};
readonly device: {
  list: MediaDeviceInfo[];
  selected: MediaDeviceInfo | undefined;
  lastSelected: string | undefined;
};

UseTakePhoto

takeAsBlob(): Promise<Blob | undefined>;
takeAsDataURL(): Promise<string | undefined>;

UseTakeVideo

readonly state: RecordingState;
start(): void;
pause(): void;
resume(): void;
stop(): Promise<Blob | undefined>;

License

MIT

vue-use-camera's People

Contributors

alzera avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.