<template>
<div>
<video class="video" ref="videoElement" autoplay></video>
<button class="button" @click="toggleCamera">
{{ cameraActive ? "關(guān)閉攝像頭" : "打開(kāi)攝像頭" }}
</button>
<button class="button" @click="takeSnapshot">截圖</button>
<button class="button" @click="toggleRecording">
{{ recording ? "停止錄制" : "開(kāi)始錄制" }}
</button>
<button class="button" @click="downloadImage">下載圖片</button>
<button class="button" @click="downloadVideo">下載視頻</button>
<canvas ref="canvasElement"></canvas>
<img :src="snapshotUrl" v-if="snapshotUrl" alt="截圖預(yù)覽" />
<video
:src="recordedVideoUrl"
v-if="recordedVideoUrl"
autoplay
controls
></video>
</div>
</template>
<script>
export default {
data() {
return {
cameraActive: false,
recording: false,
snapshotUrl: "",
recordedVideoUrl: "",
stream: null,
videoElement: null,
canvasElement: null,
mediaRecorder: null,
chunks: [],
rotate: {
x: 0,
y: 0,
},
};
},
mounted() {
this.videoElement = this.$refs.videoElement;
this.canvasElement = this.$refs.canvasElement;
},
methods: {
toggleCamera() {
if (!this.cameraActive) {
navigator.mediaDevices
.getUserMedia({ video: true })
.then((stream) => {
this.stream = stream;
this.videoElement.srcObject = stream;
this.cameraActive = true;
})
.catch((error) => {
console.error("無(wú)法打開(kāi)攝像頭:", error);
});
} else {
this.videoElement.srcObject = null;
this.stream.getTracks().forEach((track) => {
track.stop();
});
this.cameraActive = false;
}
},
takeSnapshot() {
const context = this.canvasElement.getContext("2d");
context.drawImage(
this.videoElement,
0,
0,
this.canvasElement.width,
this.canvasElement.height
);
this.snapshotUrl = this.canvasElement.toDataURL();
},
toggleRecording() {
if (!this.recording) {
this.startRecording();
} else {
this.stopRecording();
}
},
startRecording() {
if (MediaRecorder.isTypeSupported("video/webm")) {
this.chunks = [];
this.mediaRecorder = new MediaRecorder(this.stream, {
mimeType: "video/webm",
});
this.mediaRecorder.ondataavailable = (e) => {
if (e.data && e.data.size > 0) {
this.chunks.push(e.data);
}
};
this.mediaRecorder.onstop = () => {
const recordedBlob = new Blob(this.chunks, { type: "video/webm" });
this.chunks = [];
this.recordedVideoUrl = URL.createObjectURL(recordedBlob);
};
this.mediaRecorder.start();
this.recording = true;
} else {
console.error("不支持錄制視頻");
}
},
stopRecording() {
if (this.recording) {
this.mediaRecorder.stop();
this.mediaRecorder = null;
this.recording = false;
}
},
downloadImage() {
if (this.snapshotUrl) {
const a = document.createElement("a");
a.href = this.snapshotUrl;
a.download = "snapshot.png";
a.click();
}
},
downloadVideo() {
if (this.recordedVideoUrl) {
const a = document.createElement("a");
a.href = this.recordedVideoUrl;
a.download = "recorded_video.webm";
a.click();
}
},
},
};
</script>
vue2 開(kāi)啟筆記本PC(攝像頭煌张,實(shí)現(xiàn)錄像呐赡,截圖,預(yù)覽截圖骏融,預(yù)覽錄像链嘀,下載截圖,下載錄像)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén)纤怒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人天通,你說(shuō)我怎么就攤上這事泊窘。” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵烘豹,是天一觀的道長(zhǎng)瓜贾。 經(jīng)常有香客問(wèn)我,道長(zhǎng)携悯,這世上最難降的妖魔是什么祭芦? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮憔鬼,結(jié)果婚禮上龟劲,老公的妹妹穿的比我還像新娘。我一直安慰自己轴或,他們只是感情好昌跌,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著照雁,像睡著了一般蚕愤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上饺蚊,一...
- 那天萍诱,我揣著相機(jī)與錄音,去河邊找鬼污呼。 笑死裕坊,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的曙求。 我是一名探鬼主播碍庵,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼悟狱!你這毒婦竟也來(lái)了静浴?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤挤渐,失蹤者是張志新(化名)和其女友劉穎苹享,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體浴麻,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡得问,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了软免。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宫纬。...
- 正文 年R本政府宣布蝌蹂,位于F島的核電站噩斟,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏孤个。R本人自食惡果不足惜剃允,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望齐鲤。 院中可真熱鬧斥废,春花似錦、人聲如沸佳遂。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)丑罪。三九已至荚板,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間吩屹,已是汗流浹背跪另。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像擦盾,于是被迫代替她去往敵國(guó)和親嘲驾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 各個(gè)行業(yè)的微信小程序源碼迹卢,公眾號(hào)模塊源碼辽故。帶前后端,部份帶有教程腐碱。小白也適用誊垢。 這是我交了N多會(huì)員費(fèi)從各大論壇收集...
- 功能需求: 1 手機(jī)端打開(kāi)攝像頭實(shí)時(shí)預(yù)覽喂走;2 手機(jī)端作為服務(wù)端,PC端作為客戶端連接谋作;3 連接成功后PC端可以同時(shí)...
- Content-Disposition 屬性是作為對(duì)下載文件的一個(gè)標(biāo)識(shí)字段遵蚜,在rfc2616http://www....
- 限制只能選擇圖片 限制只能選擇視頻 限制只能選擇音頻 直接打開(kāi)攝像頭拍照 直接打開(kāi)攝像頭錄像