getImageData错误与本地图像

时间:2017-05-25 14:55:29

标签: javascript jquery html css canvas

我正在使用下一个试图从图像中获取像素颜色的代码

$(document).ready(function(){
         var img = document.getElementById('my-image');
         var canvas = document.createElement('canvas');
         canvas.width = img.width;
         canvas.height = img.height;
         canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
         var pixelData = canvas.getContext('2d').getImageData(1, 1, 1, 1).data;
      });

我每次都会收到此错误:

index.html:20 Uncaught DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.
    at HTMLDocument.<anonymous> (file:///C:/Users/ariadna/Desktop/Programacion/Test/public/index.html:20:50)
    at j (http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js:2:27295)
    at Object.fireWith [as resolveWith] (http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js:2:28108)
    at Function.ready (http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js:2:29942)
    at HTMLDocument.J (http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js:2:30308)

我看到很多关于此问题的问题,所有答案都是almos =“你无法从另一个域获取图像,这是安全措施”。

好的,但为什么我的不工作,图像与.html

位于同一目录中

1 个答案:

答案 0 :(得分:0)

问题是您使用的是本地文件<!-- TODO: Find a more awesome way to get the parameters. --> <rabbit:connection-factory id="connectionFactory" username="#{ T(String).valueOf(systemEnvironment['CLOUDAMQP_URL']).split(&quot;//&quot;)[1].split(&quot;:&quot;)[0]}" password="#{ T(String).valueOf(systemEnvironment['CLOUDAMQP_URL']).split(&quot;:&quot;)[2].split(&quot;@&quot;)[0]}" host="#{ T(String).valueOf(systemEnvironment['CLOUDAMQP_URL']).split(&quot;@&quot;)[1].split(&quot;/&quot;)[0]}" virtual-host="#{ T(String).valueOf(systemEnvironment['CLOUDAMQP_URL']).split(&quot;@&quot;)[1].split(&quot;/&quot;)[1]}"/> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" /> <rabbit:admin connection-factory="connectionFactory" /> <rabbit:queue name="myQueue" /> <bean id="foo" class="Foo" /> <bean id="exHandler" class="BackgroundRejectingErrorHandler" /> <bean class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter" id="messageListenerAdapter"> <constructor-arg ref="foo" /> </bean> <bean class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer" id="listenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="queueNames" value="myQueue" /> <property name="messageListener" ref="messageListenerAdapter" /> <property name="concurrentConsumers" value="5" /> <property name="maxConcurrentConsumers" value="150" /> <property name="startConsumerMinInterval" value="10" /> <property name="stopConsumerMinInterval" value="10" /> <property name="errorHandler" ref="exHandler" /> </bean> <bean id="manageListenerContainer" class="background.ManageListenerContainer"> <property name="listenerContainer" ref="listenerContainer"></property> </bean> ...您需要设置一个域(它可以是localhost)