java - Jquery Webcam插件使用java将图像上传到服务器

标签 java jquery html jsp spring-mvc

我想知道是否有一种方法可以使用java将图像上传到服务器。我正在使用 jquery 网络摄像头插件,他们有一个脚本可以执行此操作,但是我没有使用 php,而是使用 SpringMVC (java)。

我不确定是否有办法配置 SpringMVC 项目以同时使用 php 和 java。这是我到目前为止所拥有的:

代码

var pos = 0, ctx = null, saveCB, image = [];
    var canvas = document.createElement('canvas');
    canvas.setAttribute('width', 320);
    canvas.setAttribute('height', 240);
    ctx = canvas.getContext('2d');
    image = ctx.getImageData(0, 0, 320, 240);

var saveCB = function (data) {
    var col = data.split(';');
    var img = image;
    for (var i = 0; i < 320; i++) {
        var tmp = parseInt(col[i]);
        img.data[pos + 0] = (tmp >> 16) & 0xff;
        img.data[pos + 1] = (tmp >> 8) & 0xff;
        img.data[pos + 2] = tmp & 0xff;
        img.data[pos + 3] = 0xff;
        pos += 4;
    }

    if (pos >= 4 * 320 * 240) {
        ctx.putImageData(img, 0, 0);
        foto = canvas.toDataURL("image/png");
        $("#photo").val(foto);
        pos = 0;
    }
};

$(文档).ready(函数(){

document.createElement("canvas");

$("#canvas").hide();

$("#camera").webcam({
        width: 320,
        height: 240,
        useMicrophone: false,
        mode: "callback",
        swffile: "resources/swf/jscam_canvas_only.swf",
        quality:85,

        onSave: saveCB,
        onCapture: function () {
            $("#camera").hide();
            webcam.save(//need a java function to upload to server );
            $("#canvas").show();                 

        },

        debug: function (type, string) {
            $("#status").html(type + ": " + string);
        }

}); 



$('#upload').click(function () {
    webcam.capture(); 
    return false;
});

$('#retake').click(function () {
    $("#canvas").hide();
    $("#camera").show();
    return false;
});


window.addEventListener("load", function() {

    var canvas = document.getElementById("canvas");

    if (canvas.getContext) {
        ctx = document.getElementById("canvas").getContext("2d");
        ctx.clearRect(0, 0, 320, 240);
        image = ctx.getImageData(0, 0, 320, 240);

    }

    }, false);

最佳答案

将图像绘制到 Canvas 后 使用canvas.toDataURL() - 返回图像的base64编码数据作为请求参数

然后在后端使用Base64.decodeBase64(String data)将编码后的数据解码为字节数组:

//@RequestParam data

//remove mimeType declaration in the data string first

String byteStr = data.split(",")[1];


//get the byte array of the data

byte[] bytes = Base64.decodeBase64(byteStr);

关于java - Jquery Webcam插件使用java将图像上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14508403/

相关文章:

javascript - 在 Javascript/jQuery 中创建一个不断变化的列表

javascript - AJAX 发布 onclick

php - php 中的网页的最佳执行时间是多少?

java - 多模块 gradle 构建 jar

java - Hadoop 2.7.3中的HTrace

java - 从具有日期比较的列表中删除

java - 如何按顺序确定一条线上的点(从 P1 到 P2)?

javascript - 动态添加下拉菜单并排除所选选项

javascript - 子元素填充空间以匹配另一个 div 的宽度

html - Bootstrap 3 - 使导航栏的链接随着品牌形象的大小垂直增长