有了 PNG 图像的 URL(或该 url 中的数据,以 String
形式),如何使用 Java 查找一组坐标处的 RGB(或类似)值?
提前致谢!
最佳答案
这个例子应该有你需要的一切:
引用线程的相关部分:
File inputFile = new File("image.png");
BufferedImage bufferedImage = ImageIO.read(inputFile);
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight(null);
//Get Pixels
int [] rgbs = new int[w*h];
bufferedImage.getRGB(0, 0, w, h, rgbs, 0, w); //Get all pixels
然后要获取特定像素,请参阅文档:
即:
int pixel = rgbs[offset + (y-startY)*scansize + (x-startX)];
如果您只想要一个像素,可以使用getRGB(x, y)
:
即:
int pixel = bufferedImage.getRGB(x, y);
关于java - 在 Java 中查找 PNG 图像中坐标处的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9388772/