从图像中获取像素颜色 [英] Get pixel color from an image

查看:176
本文介绍了从图像中获取像素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在浏览器上有一个图像。

I have an image on a browser.

我想得到图像颜色的左上角像素(坐标为:0,0),无论是否图像是否旋转。

I want to get the top left pixel of the image color (at coordinates: 0,0), no matter whether the image is rotated or not.

我怎么能用javascript或php代码呢?

How can I do that, using javascript or php code?

推荐答案


  • 创建画布document.createElement

  • 获取2d上下文 canvas.getContext('2d')

  • 将图像绘制到画布 context.drawImage(image,x,y)

    • 确保图片来自同一个域,否则您将无法访问其像素

      • Create a canvas document.createElement
      • Get the 2d context canvas.getContext('2d')
      • Draw the image to the canvas context.drawImage(image, x, y)
        • Make sure the image is from the same domain or you won't have access to its pixels

          • 你想要的只是左上角所以 context.getImageData(0,0,1,1)

          • You want just the top left so context.getImageData(0, 0, 1, 1)

          • 数组将 r g b a 值。

          • The array will have r, g, b and a values.

          这篇关于从图像中获取像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆