My app has to take pictures and save them as PDF, but those pictures should not be saved. So first thing, here is my code to take pictures from a web using a webcam.
Here are my sources. Sumber 1 dan Sumber 2
HTML
<style>
.videoElement {
width: 400px;
height: 275px;
background-color: #666;
transform: rotateY(180deg);
-webkit-transform:rotateY(180deg); /* Safari and Chrome */
-moz-transform:rotateY(180deg); /* Firefox */
}
</style>
<video autoplay="true" id="video-webcam" class="videoElement" style="text-align:center">
Izinkan untuk Mengakses Webcam untuk Demo
</video>
<img id="snapshot" style="width: 400px;height: 275px;">
<button onclick="takeSnapshot()"> Take Picture </button>
And here is the JS code:
var video = document.querySelector("#video-webcam");
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({ video: true }, handleVideo, videoError);
}
function handleVideo(stream) {
video.srcObject = stream;
//console.log(stream);
}
function videoError(e) {
// do something
alert("Izinkan menggunakan webcam untuk demo!")
}
function takeSnapshot() {
var img = document.getElementById('snapshot');
var context;
var width = video.offsetWidth
, height = video.offsetHeight;
canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
context = canvas.getContext('2d');
context.drawImage(video, 0, 0, width, height);
img.src = canvas.toDataURL('image/png');
img.style="transform: scaleX(-1);width: 400px;height: 275px";
}
What I learnt from that code are:
- The display taken from webcam has to be mirrored, as well as the picture taken. I did it with the CSS above.
- The images, that are taken using canva, are saved in Base64 code. This is super cool, furthermore the HTML tag is able to show an image with source of Base64 code. Super cool.... this feature helps me a lot, because I don't have to save the picture in server. What I did was I took this Base64 code and put them in HTML tag, which the HTML code is later converted to PDF file
Thats all, folks
Tidak ada komentar:
Posting Komentar