使用 BufferedImages 获取图像每个像素的颜色 [英] Get color of each pixel of an image using BufferedImages
本文介绍了使用 BufferedImages 获取图像每个像素的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取图像每个像素的每种颜色.我的想法如下:
I am trying to get every single color of every single pixel of an image. My idea was following:
int[] pixels;
BufferedImage image;
image = ImageIO.read(this.getClass.getResources("image.png");
int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
是吗?我什至无法检查像素"数组包含的内容,因为我收到以下错误:
Is that right? I can't even check what the "pixels" array contains, because i get following error:
java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt
我只想接收数组中每个像素的颜色,我该如何实现?
I just would like to receive the color of every pixel in an array, how do i achieve that?
推荐答案
import java.io.*;
import java.awt.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class GetPixelColor {
public static void main(String args[]) throws IOException {
File file = new File("your_file.jpg");
BufferedImage image = ImageIO.read(file);
// Getting pixel color by position x and y
int clr = image.getRGB(x, y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
System.out.println("Red Color value = " + red);
System.out.println("Green Color value = " + green);
System.out.println("Blue Color value = " + blue);
}
}
当然你必须为所有像素添加一个for循环
of course you have to add a for loop for all pixels
这篇关于使用 BufferedImages 获取图像每个像素的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文