简体   繁体   English

如何从外部网址获取图像数据并返回颜色

[英]How do i get image data from external url - and return colors

Im using clusterfck.js to analyze and return colors. 我正在使用clusterfck.js分析并返回颜色。 I wan't to grap images from search-results, but it seems to be a problem with the way clusterfck does the data reading. 我不想从搜索结果中获取图像,但是clusterfck进行数据读取的方式似乎存在问题。 Here's the code from clusterfck: 这是clusterfck的代码:

$(".kmeans-button").click(function() {
   if (!colors) {
      colors = getImageColors();
   }
   var k = $(this).attr("data-k"); // $.data() returned undefined
   $("#clusters").empty().append("<div>calculating distances...</div>");
   kmeans.clusterColors(colors, k); // Threaded analyzing (worker)
})

function getImageColors() {
    var img = $("#test-image");
    var width = img.width();
    var height = img.height();
    var canvas = document.createElement('canvas');
    canvas.width = width;
    canvas.height = height;

    var context = canvas.getContext("2d");
    context.drawImage(img.get(0), 0, 0);

    var data = context.getImageData(0, 0, width, height).data;
    var colors = [];
    for(var x = 0; x < width-12; x += 3) {
    for(var y = 0; y < height-12; y += 3) { // sample image, every 3rd row and column
    var offs = x*4 + y*4*width;
    var color = [data[offs + 0], data[offs + 1], data[offs + 2]];
    colors.push(color);
    }
  }
return colors;
}

I tried to put in img.crossOrigin = "Anonymous"; 我试图放入img.crossOrigin =“ Anonymous”; with no luck. 没有运气。 I guess it needs to be loaded in af function and somehow callback colors. 我猜它需要加载到af函数和某种程度上的回调颜色。 This is the best i could come up with, but it's not working. 这是我能想到的最好的方法,但是没有用。

$(".kmeans-button").click(function() {
   if (!colors) {
        getImageColors2(function(colors2) {
        colors=colors2.slice(0);
        var k = $(this).attr("data-k-e"); // $.data() returned undefined
        $("#clusters").empty().append("<div>calculating colors...</div>");
        kmeans.clusterColors(colors, k);
    })
}
}) 


function getImageColors2(callback) {
var img2= document.getElementById("test-image");
var img = new Image();  
img.onload = function () {
  var width = img.width();
  var height = img.height();

  var canvas = document.createElement('canvas');
  canvas.width = width;
  canvas.height = height;

  var context = canvas.getContext("2d");
  context.drawImage(img.get(0), 0, 0);

  var data = context.getImageData(0, 0, width, height).data;
  var colors = [];
  for(var x = 0; x < width-12; x += 3) {
    for(var y = 0; y < height-12; y += 3) { // sample image, every 3rd row and column
     var offs = x*4 + y*4*width;
     var color = [data[offs + 0], data[offs + 1], data[offs + 2]];
     colors.push(color);
      }
    }
    callback(colors);
  }
  img.src=img2.src;
}

I assume that it can't access the data without loading into a image when on a external server. 我假设它在外部服务器上时,如果不加载到映像中就无法访问数据。 What am i missing her ? 我想念她什么? Maybe i got it all wrong ? 也许我弄错了吗?

Thanks for your comments! 感谢您的意见! I got it working now with the callback function :-) 我现在使用回调函数:-)

function getpalette(colors) {
        $("#clusters").empty().append("<div>calculating colors...</div>");
        kmeans_paint.clusterColors(colors, k);
    }

$(".kmeans-error-button").click(function() {
    k = $(this).attr("data-k-e");

    if (!colors) {   
        getImageColors(getpalette);
    }
}) 

function getImageColors(callback) {
  var img = new Image();  
  img.setAttribute('crossOrigin', 'anonymous');
  img.onload = function () {

  var width = img.width;
  var height = img.height;

  var canvas = document.createElement("canvas");
  canvas.width = width;
  canvas.height = height;
  var context = canvas.getContext("2d");
   context.drawImage(img, 0, 0);

  var data = context.getImageData(0, 0, width, height).data;
  var colors = [];
  for(var x = 0; x < width-12; x += 3) {
    for(var y = 0; y < height-12; y += 3) { // sample image, every 3rd row and column
     var offs = x*4 + y*4*width;
     var color = [data[offs + 0], data[offs + 1], data[offs + 2]];
     colors.push(color);
    }
  }
  callback(colors);
  }
img.src=document.getElementById("test-image").src;
}

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

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