Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中将图像(jpg)转换为Base64格式时出现问题_Javascript_Image Processing_Base64_Jpeg - Fatal编程技术网

在Javascript中将图像(jpg)转换为Base64格式时出现问题

在Javascript中将图像(jpg)转换为Base64格式时出现问题,javascript,image-processing,base64,jpeg,Javascript,Image Processing,Base64,Jpeg,我需要从本地硬盘读取图像文件,并将其转换为base64格式。转换后,我需要将base64字符串传递给Web服务,并从Web服务获取结果 我有相同的代码: <html> <script> // create the object //test = new Base64(); x = new XMLHttpRequest(); // set the function that should be called when // the ready state chang

我需要从本地硬盘读取图像文件,并将其转换为base64格式。转换后,我需要将base64字符串传递给Web服务,并从Web服务获取结果

我有相同的代码:

<html>

<script>
// create the object

//test = new Base64();

x = new XMLHttpRequest();

// set the function that should be called when
// the ready state changes (the callback)
x.onreadystatechange = handleDoc;
// establish the method and URL
//x.open('GET', "Latest Eticket.jpg", true);
x.overrideMimeType("text/plain; charset=x-user-defined");
x.open('GET', 'file:///C:\\vishwa\\Node_JS\\Jquery_ajax\\JS_with_Ajax\\base64_encode\\Latest Eticket.jpg', true);
//x.open('GET', 'C:\vishwa\Node_JS\Jquery_ajax\JS_with_Ajax\base64_encode\Latest Eticket.jpg', true);
//x.open('GET', 'http://www.google.co.in/imgres?imgurl=http://1.bp.blogspot.com/_PveGYAt2T10/TDRH1kJsXAI/AAAAAAAAAgI/a45pNA5hxrA/s1600/hi-pig.jpg&imgrefurl=http://weiwei95.blogspot.com/2010_07_01_archive.html&usg=__k761BPCJk7FGxAgy8UHUiCCO1dA=&h=474&w=600&sz=51&hl=en&start=1&sig2=K2Z27chXr6EPO-lHJVY43g&zoom=1&tbnid=NSx-UedGEdm84M:&tbnh=107&tbnw=135&ei=uezUTbaDFMSBgAet_7iVDA&prev=/search%3Fq%3Dhi%26hl%3Den%26biw%3D1024%26bih%3D548%26gbv%3D2%26tbm%3Disch&itbs=1', true);
//x.open('GET', 'http://localhost:80//Latest Eticket.jpg', true);
// initiate the HTTP transaction
x.send(null);


//
function handleDoc() {
//window.alert("READY STATE IS " +
//x.readyState);
if(x.readyState == 1)
{
    window.alert("Handling x 1 State: The response is started");
}
if(x.readyState == 4)
{
    var encodeImagescanned_image = '';
    alert("Am i coming here 4-2"+x.responseText);
                    //var base64 = new Base64();
                    encodeImagescanned_image = encode(x.responseText);
                    alert("Am i coming here 4-3"+encodeImagescanned_image);
    window.alert("Handling x 4 State: The response is ended");

    alert("Constructing Soap body");
    var xmlp = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:dsf=\"http://DSFService/\">" +
                    "<soapenv:Header/>" +
                    "<soapenv:Body>" +
                    "<dsf:FieldExtraction>" +
                    "<dsf:inImage>" +
                    this.encodeImagescanned_image +
                    "</dsf:inImage>" +
                    "</dsf:FieldExtraction>" +
                    "</soapenv:Body>" +
                    "</soapenv:Envelope>";
                    var xmlps = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:dsf=\"http://DSFService/\">" +
                    "<soapenv:Header/>" +
                    "<soapenv:Body>" +
                    "<dsf:FieldExtraction>" +
                    "<dsf:inImage>" +
                    "</dsf:inImage>" +
                    "</dsf:FieldExtraction>" +
                    "</soapenv:Body>" +
                    "</soapenv:Envelope>";

                    var dsfUrl = "http://hpldsf-tst.asiapacific.cpqcorp.net:8090/DSFServiceSite/DSFService.svc/basic";
                    request = new XMLHttpRequest();
                        // We're going to be POSTing to this URL and want a synchronous response
                    request.open("POST", dsfUrl, true);
                    request.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
                    request.setRequestHeader("Content-length", xmlp.length);
                    request.setRequestHeader("Accept-Encoding", "gzip,deflate");
                    request.timeout = 300000;
                    // This header is a required part of the SOAP protocol
                    request.setRequestHeader("SOAPAction", '\"http://DSFService/DSFService/FieldExtraction\"');
                    // Now send an XML-formatted SOAP request to the server
                    request.send(xmlp);
                    request.onreadystatechange = handlesoap;


}
}

function handlesoap()
{
    alert("Inside handlesoap function");
    if(request.readyState == 4)
    {
        var xmls = request.responseText;
        var xmlDoc = (new DOMParser()).parseFromString(xmls, "text/xml");
        var doc = xmlDoc.documentElement;
        var nvalue = xmlDoc.getElementsByTagName('FieldExtractionResponse')[0];
        alert("nvalu ="+nvalue);
        if ((nvalue !== null) && (nvalue !== undefined)) 
        {

            ////var docidval = nodes.getAttribute("id");
            ///Mojo.Log.info("docidval:" + docidval);
            dsfxml = xmls; //nodes.nodeValue;
            var pattern = /\cM/;

                                // Break the string into pieces separated by the pattern above and
                                // and store the pieces in an array called nameList
                                var nameList = [];
                                nameList = dsfxml.split(pattern);
                                var clean = '';
                                for (i = 0; i < nameList.length; i++) {

                                    clean = clean + nameList[i].replace(pattern, "").trim();
                                }
        }
        else
        {
            alert("nvalue is eitheer null or undefined");
        }
    }
}



    function encode(input) {
    alert("I am in encode function");
    var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;

    while (i < input.length) {
        chr1 = input.charCodeAt(i++) & 0xff; //alert('chr1 : ' + chr1.toString(16));
        chr2 = input.charCodeAt(i++) & 0xff; //alert('chr2 : ' + chr2.toString(16));
        chr3 = input.charCodeAt(i++) & 0xff; //alert('chr3 : ' + chr3.toString(16));

        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;

        if (isNaN(chr2)) {
        enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
        enc4 = 64;
        }

        output = output +
        _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
        _keyStr.charAt(enc3) + _keyStr.charAt(enc4);

    }

    return output;
}

</script>
</html>

//创建对象
//test=新的Base64();
x=新的XMLHttpRequest();
//设置应在以下情况下调用的函数:
//就绪状态更改(回调)
x、 onreadystatechange=handleDoc;
//建立方法和URL
//x、 打开('GET',“Latest Eticket.jpg”,true);
x、 overrideMimeType(“text/plain;charset=x-user-defined”);
x、 打开('获取','file:///C:\\vishwa\\Node\u JS\\Jquery\u ajax\\JS\u with\u ajax\\base64\u encode\\Latest Eticket.jpg',true);
//x、 打开('GET','C:\vishwa\Node\JS\Jquery\u ajax\JS\u with\u ajax\base64\u encode\Latest Eticket.jpg',true);
//x、 打开(‘得到’,’http://www.google.co.in/imgres?imgurl=http://1.bp.blogspot.com/_PveGYAt2T10/TDRH1kJsXAI/AAAAAAAAAgI/a45pNA5hxrA/s1600/hi-pig.jpg&imgrefurl=http://weiwei95.blogspot.com/2010_07_01_archive.html&usg=__k761BPCJk7FGxAgy8UHUiCCO1dA=&h=474&w=600&sz=51&hl=en&start=1&sig2=K2Z27chXr6EPO-lHJVY43g&zoom=1&tbnid=NSx-UedGEdm84M:&tbnh=107&tbnw=135&ei=uezUTbaDFMSBgAet_7iVDA&prev=/search%3Fq%3Dhi%26hl%3Den%26biw%3D1024%26bih%3D548%26gbv%3D2%26tbm%3Disch&itbs=1',对);
//x、 打开('获取','http://localhost:80//Latest Eticket.jpg',正确);
//启动HTTP事务
x、 发送(空);
//
函数handleDoc(){
//window.alert(“就绪状态为”+
//x、 再结晶);
如果(x.readyState==1)
{
window.alert(“处理x1状态:响应已启动”);
}
如果(x.readyState==4)
{
var encodeImagescanned_image='';
警报(“我来这里吗4-2”+x.responseText);
//var base64=新的base64();
encodeImagescanned_image=encode(x.responseText);
警报(“我来这里吗4-3”+编码图像扫描图像);
window.alert(“处理x4状态:响应结束”);
警报(“构建Soap主体”);
var xmlp=“”+
"" +
"" +
"" +
"" +
此.encodeImagescanned\u图像+
"" +
"" +
"" +
"";
var xmlps=“”+
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
var dsfUrl=”http://hpldsf-tst.asiapacific.cpqcorp.net:8090/DSFServiceSite/DSFService.svc/basic";
请求=新的XMLHttpRequest();
//我们将发布到此URL,并希望得到同步响应
请求。打开(“POST”,dsfUrl,true);
setRequestHeader(“内容类型”,“text/xml;charset=UTF-8”);
setRequestHeader(“内容长度”,xmlp.length);
setRequestHeader(“接受编码”、“gzip、deflate”);
request.timeout=300000;
//此标头是SOAP协议的必需部分
setRequestHeader(“SOAPAction”,“\”http://DSFService/DSFService/FieldExtraction\"');
//现在向服务器发送一个XML格式的SOAP请求
请求发送(xmlp);
request.onreadystatechange=handlesoap;
}
}
函数handlesoap()
{
警报(“内部把手滑动功能”);
if(request.readyState==4)
{
var xmls=request.responseText;
var xmlDoc=(new DOMParser()).parseFromString(xmls,“text/xml”);
var doc=xmlDoc.documentElement;
var nvalue=xmlDoc.getElementsByTagName('FieldExtractionResponse')[0];
警报(“nvalu=“+nvalue”);
if((nvalue!==null)和&(nvalue!==undefined))
{
////var docidval=nodes.getAttribute(“id”);
///Mojo.Log.info(“docidval:+docidval”);
dsfxml=xmls;//nodes.nodeValue;
变量模式=/\cM/;
//把绳子分成几段,按上面和下面的图案分开
//并将这些片段存储在名为nameList的数组中
变量名称列表=[];
nameList=dsfxml.split(模式);
var-clean='';
对于(i=0;i>2;
enc2=((chr1&3)>4);
enc3=((chr2&15)>6);
enc4=chr3&63;
if(isNaN(chr2)){
enc3=enc4=64;
}否则如果(isNaN(chr3)){
enc4=64;
}
输出=输出+
_按键字符(附件1)+按键字符(附件2)+
_按键字符(enc3)+按键字符(enc4);
}
返回输出;
}

问题是没有正确地将图像转换为base64格式。您能帮我知道哪里出了问题吗…

这是基于浏览器的吗?如果是这样,您就错了。如果您需要从本地硬盘复制,包含您的