-
Notifications
You must be signed in to change notification settings - Fork 6
/
camera.html
197 lines (187 loc) · 6.65 KB
/
camera.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!doctype html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="js/vue.js"></script>
<script src="js/v2c.js"></script>
<script src="js/LZWEncoder.js"></script>
<script src="js/NeuQuant.js"></script>
<script src="js/GIFEncoder.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/v2c.css">
<title>相机流转字符画</title>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-FZ2FSZNF5C"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-FZ2FSZNF5C');
</script>
</head>
<body style="padding-top: 0px;">
<div class="album py-5" id="app">
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>高度</label>
<input v-model="video.height" type="number" class="form-control" :disabled="running">
</div>
<div class="form-group">
<label>宽度</label>
<input v-model="video.width" type="number" class="form-control" :disabled="running">
</div>
<div class="form-group">
<label>字符集</label>
<textarea v-model="charset" type="text" class="form-control" :disabled="running"></textarea>
</div>
<div class="form-group form-check">
<input class="form-check-input" type="checkbox" v-model="gray" :disabled="running">
<label class="form-check-label">
灰度 GIF 输出
</label>
</div>
<div class="form-group form-check">
<input class="form-check-input" type="checkbox" v-model="colorful" :disabled="running">
<label class="form-check-label">
彩色 GIF 输出
</label>
</div>
<button class="btn btn-primary" @click="process"
:disabled="running || !video.height || !video.width || !video.src || charset.length === 0">开始</button>
<button class="btn btn-primary" @click="end" :disabled="!running">完成</button>
<button class="btn btn-primary" @click="downloadText" :disabled="texts.length === 0 || running">下载
HTML</button>
<button class="btn btn-primary" @click="downloadGray" :disabled="texts.length === 0 || running"
v-if="gray">下载灰度 GIF</button>
<button class="btn btn-primary" @click="downloadColorful" :disabled="texts.length === 0 || running"
v-if="colorful">下载彩色 GIF</button>
<div class="progress">
<div class="progress-bar" :style="`width: ${Math.floor(gifCur / gifCnt * 100)}%`" aria-valuemin="0"
aria-valuemax="100">{{Math.floor(gifCur / gifCnt * 100)}}%</div>
</div>
</div>
<div class="col-md-6">
<video :src="video.src" @loadedmetadata="loaded" id="video" style="max-width: 100%;"></video>
</div>
<div class="col-md-12 center">
<pre>{{text}}</pre>
</div>
<div class="col-md-12 center" v-if="gray">
<canvas id="gray">
</canvas>
</div>
<div class="col-md-12 center" v-if="colorful">
<canvas id="colorful">
</canvas>
</div>
</div>
</div>
</div>
</body>
<script>
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
var app = new Vue({
el: '#app',
data: {
file: null,
video: {
src: null,
height: null,
width: null,
duration: null,
},
running: false,
charset: '$@B%8&WM#*oahkbdpqwmzcvunxrjft/\\|()1{}[]?-_+~<>i!;:,\"^`\'. ',
text: '',
texts: [],
gray: true,
grayImages: [],
colorful: false,
colorfulImages: [],
gifCur: 0,
gifCnt: 1,
},
async mounted() {
const constraints = {
video: true,
audio: false
};
let cameraStream = await navigator.mediaDevices.getUserMedia(constraints);
let video = document.getElementById('video');
if ("srcObject" in video) {
video.srcObject = cameraStream;
} else {
video.src = window.URL.createObjectURL(cameraStream);
}
this.video.src = cameraStream;
video.onloadedmetadata = function (e) {
video.play();
};
},
methods: {
async start() { },
previewFile(event) {
this.file = event.target.files[0];
this.video.src = window.URL.createObjectURL(this.file);
},
loaded(event) {
const video = event.target;
this.video.height = Math.floor(video.videoHeight / 10);
this.video.width = Math.floor(video.videoWidth / 10);
this.video.duration = Math.floor(video.duration * 10);
},
async end() {
this.running = false;
},
async process() {
this.running = true;
const videoElem = document.getElementById('video');
this.texts = [];
this.grayImages = [];
this.colorfulImages = [];
const v2cVideo = new V2CVideo(videoElem, this.video.width, this.video.height, this.charset, document.getElementById('gray'), document.getElementById('colorful'));
for (let i = 0; i < this.video.duration && this.running; i++) {
const screenshot = v2cVideo.screenshot(this.gray, this.colorful);
this.text = screenshot.text;
this.texts.push(this.text);
if (this.gray) {
const ctx = screenshot.grayImage.getContext('2d');
this.grayImages.push(ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height));
}
if (this.colorful) {
const ctx = screenshot.colorfulImage.getContext('2d');
this.colorfulImages.push(ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height));
}
await sleep(100);
}
this.running = false;
},
downloadText() {
downloadText(this.texts);
},
downloadGray() {
const callback = (cur, cnt) => {
this.gifCur = cur;
this.gifCnt = cnt;
}
downloadGIF(this.grayImages, callback);
},
downloadColorful() {
const callback = (cur, cnt) => {
this.gifCur = cur;
this.gifCnt = cnt;
}
downloadGIF(this.colorfulImages, callback);
}
}
})
</script>
<script src="js/jquery-3.5.1.slim.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</html>