使用BufferedImages获取图像的每个像素的颜色 [英] Get color of each pixel of an image using BufferedImages

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

问题描述

我试图获取图像中每个像素的每一种颜色。
我的想法是:

  int [] pixels; 
BufferedImage image;

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

是不是我不能检查pixels数组包含什么,因为我得到以下错误:

  java.awt.image.DataBufferByte不能转换为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 = new File(your_file.jpg);
BufferedImage image = ImageIO.read(file);
//按位置x和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);
}
}

当然你必须为所有像素


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");
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);
  }
}

of course you have to add a for loop for all pixels

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

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