gpt4 book ai didi

java - 使用 BufferedImages 获取图像每个像素的颜色

转载 作者:太空狗 更新时间:2023-10-29 23:01:21 25 4
gpt4 key购买 nike

我正在尝试获取图像每个像素的每种颜色。我的想法如下:

int[] pixels;
BufferedImage image;

image = ImageIO.read(this.getClass.getResources("image.png");
int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();

是吗?我什至无法检查“像素”数组包含的内容,因为出现以下错误:

java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt

我只想接收数组中每个像素的颜色,我该如何实现?

最佳答案

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循环

关于java - 使用 BufferedImages 获取图像每个像素的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22391353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com