I have captured the image from webcam using HTML5 but the result is encoded in base64. How can I decode and put a real image on file upload. Here is my code
我已经用HTML5从webcam上获取了图像,但是结果是用base64编码的。我如何解码并把一个真实的图像上传到文件上传。这是我的代码
HTML
HTML
<div class="fileUpload">
<input type="file" accept="image/*" class="CUupload" onClick="return false">
</div>
<div class="CUpopup" style="display:none">
<div id="CUmain">
<video id="video"></video>
<div class="CUvideoside">
<a id="button">Camera button</a>
<!-- target for the canvas-->
<div id="canvasHolder"></div>
<!--preview image captured from canvas-->
<img id="preview" src="" width="160" height="120">
<label>base64 image:</label>
<!--<input id="imageToForm" type="text">-->
<div class="CUdone">
<a href="javascript:void(0);">Done</a>
</div>
</div>
</div>
</div>
CSS
CSS
body{
font-family:Sans-Serif;
}
canvas{
position:absolute;
left: -9999em;
}
#button{
background-color: Red;
color: #fff;
padding: 3px 10px;
cursor:pointer;
display: inline-block;
border-radius: 5px;
}
#sidebar{
float:right;
width: 45%;
}
#main{
float:left;
width: 45%;
}
#imageToForm{
width:100%;
}
#preview{
margin: 20px 0;
}
label{
display: block;
}
video#video{
width:50%;
float:left;
height:100%;
}
.CUpopup{
position:fixed;
top:0px;
left:0px;
bottom:0px;
right:0px;
background:rgba(0,0,0,0.3);
}
#CUmain{position:absolute;
top:0px;
left:0px;
bottom:0px;
right:0px;
margin:auto;
background:#fff;
width:50%;
height:50%;
padding:10px;
border-radius:10px;
}
.CUvideoside{
width:48%;
float:left;
margin-left:2%;
}
.CUdone a{
background-color: Red;
color: #fff;
padding: 3px 10px;
cursor:pointer;
display: inline-block;
border-radius: 5px;
text-decoration:none;
}
**JS**
$(document).ready(function(){
$('.CUupload').click(function(){
$('.CUpopup').show();
});
$('.CUdone').click(function(){
if($('preview').attr('src') != ""){
$('.CUpopup').hide();
}
});
});
var video;
var dataURL;
//http://coderthoughts.blogspot.co.uk/2013/03/html5-video-fun.html - thanks :)
function setup() {
navigator.myGetMedia = (navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
navigator.myGetMedia({ video: true }, connect, error);
}
function connect(stream) {
video = document.getElementById("video");
video.src = window.URL ? window.URL.createObjectURL(stream) : stream;
video.play();
}
function error(e) { console.log(e); }
addEventListener("load", setup);
function captureImage() {
var canvas = document.createElement('canvas');
canvas.id = 'hiddenCanvas';
//add canvas to the body element
document.body.appendChild(canvas);
//add canvas to #canvasHolder
document.getElementById('canvasHolder').appendChild(canvas);
var ctx = canvas.getContext('2d');
canvas.width = video.videoWidth / 4;
canvas.height = video.videoHeight / 4;
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
//save canvas image as data url
dataURL = canvas.toDataURL();
//set preview image src to dataURL
document.getElementById('preview').src = dataURL;
// place the image value in the text box
// document.getElementById('imageToForm').value = dataURL;
$('.CUupload').attr('value',dataURL);
}
//Bind a click to a button to capture an image from the video stream
var el = document.getElementById("button");
el.addEventListener("click", captureImage, false);
I want to upload a image captured by webcam using file upload
我想上传一个由摄像头拍摄的图片,使用文件上传
1 个解决方案
#1
6
Convert the base64 string to a blob. A file using a file uploader is a blob with extra properties, so uploading a blob works the same.
将base64字符串转换为blob。使用文件上传程序的文件是一个具有额外属性的blob,所以上传一个blob是一样的。
var file = dataURItoBlob(canString, 'image/png');
function dataURItoBlob(dataURI, type) {
// convert base64 to raw binary data held in a string
var byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
var bb = new Blob([ab], { type: type });
return bb;
}
UPDATE
更新
To convert a base64 string to an image:
将base64字符串转换为图像:
var image = new Image(),
containerWidth = null,
containerHeight = null;
image.onload=function(){
containerWidth = image.width;
containerHeight = image.height;
}
image.src = base64string;
Or to save the base64 string locally as a file (this one is off memory while I locate where I implemented it):
或者将base64字符串本地保存为一个文件(在我找到实现它的位置时,这个字符串不在内存中):
function saveImage(base64string) {
var imageData = base64string.split(',')[1];
var a = $("<a>").attr("href", "data:Application/base64," + imageData )
.attr("download","image.png")
.appendTo("body");
a[0].click();
a.remove();
}
#1
6
Convert the base64 string to a blob. A file using a file uploader is a blob with extra properties, so uploading a blob works the same.
将base64字符串转换为blob。使用文件上传程序的文件是一个具有额外属性的blob,所以上传一个blob是一样的。
var file = dataURItoBlob(canString, 'image/png');
function dataURItoBlob(dataURI, type) {
// convert base64 to raw binary data held in a string
var byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
var bb = new Blob([ab], { type: type });
return bb;
}
UPDATE
更新
To convert a base64 string to an image:
将base64字符串转换为图像:
var image = new Image(),
containerWidth = null,
containerHeight = null;
image.onload=function(){
containerWidth = image.width;
containerHeight = image.height;
}
image.src = base64string;
Or to save the base64 string locally as a file (this one is off memory while I locate where I implemented it):
或者将base64字符串本地保存为一个文件(在我找到实现它的位置时,这个字符串不在内存中):
function saveImage(base64string) {
var imageData = base64string.split(',')[1];
var a = $("<a>").attr("href", "data:Application/base64," + imageData )
.attr("download","image.png")
.appendTo("body");
a[0].click();
a.remove();
}