Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
Get ImageData from imgurl using xmlhttprequest

There are similar questions, but the answers all use canvas. It'll be slow and I think it's unnatural.

I searched and it seemed that I can use XMLHttpRequest. But I tried all examples and failed (although I got some data,they failed to form ImageData).

function getdata(){
    var xhr=new XMLHttpRequest()
    xhr.responseType="blob" // or something else?
    xhr.onload=function(){
        temp=xhr.response
    }
    xhr.open("GET","favicon.ico",true)
    xhr.send()
}
// what to do next?

EDIT: I shall repeat myself again : no canvas

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

<!DOCTYPE html>
<meta charset="utf-8"/>
<html>
    <head>
        <title>Load Image</title>
    </head>
    <body>
        <img id="my_image"/>
        <canvas id="my_canvas"></canvas>
        <script type="text/javascript">
            loadImage();
            var blob;
            var blobUrl;
            async function loadImage() {

                var url = "http://.../image.png";
                blob = await makeRequest("GET", url);
                console.log("blob: ", blob);
                console.log("blob size: ", blob.size);
                console.log("blob type: ", blob.type);
                console.log("createObjectURL: ", URL.createObjectURL(blob));
                blobUrl = URL.createObjectURL(blob);
                console.log("blobUrl: ", blobUrl);
                getImageData(blob);
                document.querySelector('#my_image').src = URL.createObjectURL(blob);
            }

            function makeRequest(method, url) {
                return new Promise(function (resolve, reject) {
                    let xhr = new XMLHttpRequest();
                    xhr.open(method, url,true);
                    xhr.responseType="blob"
                    xhr.onload = function () {
                        if (this.status >= 200 && this.status < 300) {
                            resolve(xhr.response);
                        } else {
                            reject({
                                status: this.status,
                                statusText: xhr.statusText
                            });
                        }
                    };
                    xhr.onerror = function () {
                        reject({
                            status: this.status,
                            statusText: xhr.statusText
                        });
                    };
                    xhr.send();
                });
            }

            var imageData;
            getImageData = async function(blob){

                const bitmap = await createImageBitmap(blob);
                const [width, height] = [bitmap.width, bitmap.height];

                var newCanvas = document.getElementById('my_canvas');
                var ctx = newCanvas.getContext('2d');

                //ctx.drawImage(bitmap, width, height);
                console.log("bitmap: ", bitmap);
                
                //ImageData Object
                imageData = ctx.createImageData(width, height);
                console.log("imageData: ", imageData);
            }
        </script>
    </body>
</html>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda