简体   繁体   English

我想上传在Java文件的FileReader中已调整大小的图像(相机等)

[英]I want to upload an image(camera etc…) that has been resized in FileReader of Javascript

I was able to get images from the camera or file explorer. 我能够从相机或文件浏览器中获取图像。

I was loading to the canvas while resizing the image. 我在调整图像大小时正在加载到画布。

I want to upload this image that has been resized, but the security problem occurs. 我要上载已调整大小的图像,但是会出现安全问题。

reader.onload = function (event)
{
    image.onload = function()
    {
        // resize ...
        // canvas handling ...

        var context = canvas.getContext("2d");

        context.drawImage(image, x, y, size, size, 0, 0, resize, resize);

        var uri  = canvas.toDataURL();
        var data = uri.replace(/^data:image\/(png|jpg);base64,/, "");

        $(input_file_selector).val(data);  // << security error
    }

    //...
};

file load or camera capture -> paste to canvas and resize -> resized image file upload ... 文件加载或相机捕获->粘贴到画布并调整大小->调整大小的图像文件上传...

This process is impossible? 这个过程不可能吗?

Sure, you can use the resizing version of context.drawImage as you've done--your code should resize the image assuming your drawImage parameters are valid). 当然,您可以像完成操作一样使用context.drawImage的调整大小版本-您的代码应该在假定drawImage参数有效的情况下调整图像的大小)。

But...The security error are likely a result of attempting to draw a cross-domain image onto canvas. 但是...该安全错误可能是试图在画布上绘制跨域图像的结果。

Cross-domain draws will taint the canvas and prevent canvas.toDataURL from occurring. 跨域绘制会canvas.toDataURL画布,并阻止canvas.toDataURL发生。 You can upload a cross-domain image if it's headers allow anonymous access and you also specify in javascript that the image allows anonymous access. 如果标题允许匿名访问,则可以上传跨域图像,并且您还可以在javascript中指定该图像允许匿名访问。 You can read about enabling cross-domain transfers here: http://enable-cors.org/ 您可以在此处阅读有关启用跨域传输的信息: http : //enable-cors.org/

Here is an example using FileReader to let the user drag an image into a dropzone. 这是一个使用FileReader的示例,允许用户将图像拖到放置区中。 The image will be scaled 2X and added as a new image on the webpage. 该图像将缩放2倍,并作为新图像添加到网页上。

 // dropzone event handlers var dropzone; dropzone = document.getElementById("dropzone"); dropzone.addEventListener("dragenter", dragenter, false); dropzone.addEventListener("dragover", dragover, false); dropzone.addEventListener("drop", drop, false); // function dragenter(e) { e.stopPropagation(); e.preventDefault(); } // function dragover(e) { e.stopPropagation(); e.preventDefault(); } // function drop(e) { e.stopPropagation(); e.preventDefault(); var dt = e.dataTransfer; var files = dt.files; handleFiles(files); } // function handleFiles(files) { for (var i = 0; i < files.length; i++) { // get the next file that the user selected var file = files[i]; var imageType = /image.*/; // don't try to process non-images if (!file.type.match(imageType)) { continue; } // a seed img element for the FileReader var img = document.createElement("img"); img.classList.add("obj"); img.file = file; // get an image file from the user // this uses drag/drop, but you could substitute file-browsing var reader=new FileReader(); reader.onload=(function(aImg){ return function(e) { aImg.onload=function(){ // draw the aImg onto the canvas var canvas=document.createElement("canvas"); var ctx=canvas.getContext("2d"); canvas.width=aImg.width*2; canvas.height=aImg.height*2; ctx.drawImage(aImg,0,0,aImg.width,aImg.height,0,0,canvas.width,canvas.height); // make the jpeg image var newImg=new Image(); newImg.onload=function(){ newImg.id="newest"; document.body.appendChild(newImg); } newImg.src=canvas.toDataURL('image/jpeg'); } // e.target.result is a dataURL for the image aImg.src = e.target.result; }; })(img); reader.readAsDataURL(file); } // end for } // end handleFiles 
 body{ background-color: ivory; } canvas{border:1px solid red;} #dropzone{border:1px solid blue; width:300px;height:300px;} 
 <h4>Drag an image from desktop to blue dropzone.</h4> <div id="dropzone"></div> <div id="preview"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM