Java 访问图像的像素

Java 访问图像的像素,java,swing,Java,Swing,有人能在这里告诉我如何访问图像的所有像素,以便能够构建该图像的直方图吗?您可以使用 java.awt.image.BufferedImage image = ImageIO.read( "image.gif" ); int[] samples = image.getData().getPixel( x, y, null ); // or the pixel converted to the default RGB color model: int rgb = image.getRGB( x,

有人能在这里告诉我如何访问图像的所有像素,以便能够构建该图像的直方图吗?

您可以使用

java.awt.image.BufferedImage image = ImageIO.read( "image.gif" );
int[] samples = image.getData().getPixel( x, y, null );

// or the pixel converted to the default RGB color model:
int rgb = image.getRGB( x, y )

我认为你从那里得到的样本是RGB值,但是你必须检查…

如果你可以使用它,它有选项可以生成。

下面是一些可能有用的示例代码-请测试一下(它似乎对我有用,但你永远不知道!)

这个直方图不是均衡的,也不是任何东西,只是逐字地计算每个波段中特定值的像素

我相信波段(你需要检查)是R,G,B+Alpha的顺序

import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import javax.imageio.ImageIO;

public class SimpleImageReader {


public static void main(String[] args) throws Exception {
       // File f=new File(args[0]);
        File f=new File("C:\\1.jpg");
        BufferedImage img = ImageIO.read(f);
        Raster r=img.getData();
        int levels=256;
        int bands=r.getNumBands();
        int histogram[][]=new int[bands][levels]; 

        for (int x=r.getMinX();x<r.getWidth();x++) {
            for (int y=r.getMinY();y<r.getHeight();y++) {
                for (int b=0;b<3;b++) {
                    int p=r.getSample(x, y, b);
                    histogram[b][p]++;
                }
            }
        }
      for (int b=0;b<histogram.length;b++) {
        System.out.println("Band:"+b);
        for (int i=0;i<histogram[b].length;i++) {
          System.out.println("\t"+i+"="+histogram[b][i]);
        }
       }
}
}
导入java.awt.image.buffereImage;
导入java.awt.image.graster;
导入java.io.File;
导入javax.imageio.imageio;
公共类SimpleImageReader{
公共静态void main(字符串[]args)引发异常{
//文件f=新文件(args[0]);
文件f=新文件(“C:\\1.jpg”);
BuffereImage img=图像IO.read(f);
光栅r=img.getData();
智力水平=256;
int bands=r.getNumBands();
整数柱状图[][]=新整数[波段][级别];

对于(int x=r.getMinX();xYou需要指定平台-J2ME、Android、AWT vs Swing、J2SE等都有不同的映像组件