gpt4 book ai didi

javascript - 将base64图像转换为jpeg

转载 作者:行者123 更新时间:2023-12-04 14:10:29 25 4
gpt4 key购买 nike

努力将使用网络摄像头捕获的 base64 图像转换为 jpeg 以供上传。

以下捕获/显示照片有效(注意我使用的是 webcam.min.js(返回 base64)而不是 webcam.js(返回 jpeg 但依赖于 Flash)-

function take_snapshot() {
Webcam.snap( function(data_uri) {
// display results in page
document.getElementById('upload_results').innerHTML =
'<img id="imageprev" src="'+data_uri+'"/>';
} );
}

我已经尝试了以下方法,它可能会将 base 64image 转换为 blob,也可能不会 -

function saveSnap(){
var base64image = document.getElementById("imageprev").src;
alert(base64image)


// convert base64 to raw binary data held in a string
var byteString = atob(base64image.split(',')[1]);
// separate out the mime component
var mimeString = base64image.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
var dw = new DataView(ab);
for(var i = 0; i < byteString.length; i++) {
dw.setUint8(i, byteString.charCodeAt(i));
alert("arrived here");

// write the ArrayBuffer to a blob, and you're done
return new Blob([ab], {type: mimeString});
}

除了停止 jsp 之外,这什么都不做

let image = new Image();
image.src = base64image;
document.body.appendChild(image);

我如何获取/查看/提取实际的 jpeg 文件,以便我可以上传它(它必须是类似 number.jpeg 的东西)

JDK6/Javascript(请不要使用 php)

任何想法表示赞赏。

问候拉尔夫

最佳答案

创建一个图像对象并将base64作为其来源。

let image = new Image();
image.src = 'data:image/png;base64,iVBORw0K...';
document.body.appendChild(image);

var aFilePartss = [image];
var oMyBlob = new Blob(aFileParts, {type : 'image/png'});
// window.open(URL.createObjectURL(oMyBlob));

var fd = new FormData();
fd.append('data', oMyBlob);
$.ajax({
type: 'POST',
url: '/upload.php',
data: fd,
}).done(function(data) {
console.log(data);
});

关于javascript - 将base64图像转换为jpeg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64891555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com