2016-02-02 104 views
2

我正在用Java读取简单的jpg图像并打印出像素数据的2d阵列。从JPG图像中读取像素数据

如果我有黑色的整个图像我得到了我期望: 这是10×20的黑色图像

enter image description here

而结果:

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 

但是,如果我画一个白色行在图像的第一行我得到1的地方我不指望: 其他图像:

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 //Why?? 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 

这是我的代码:

byte[][]pixels; 
BufferedImage image; 

public ImageProcessor(File f) throws IOException{ 
    image = ImageIO.read(f); 
    //Bitmap bMap = BitmapFactory.decodeFile(f.getAbsolutePath()); // Si es jpg 
    pixels = new byte[image.getWidth()][]; 

    for (int x = 0; x < image.getWidth(); x++) { 
     pixels[x] = new byte[image.getHeight()]; 

     for (int y = 0; y < image.getHeight(); y++) { 
      pixels[x][y] = (byte) (image.getRGB(x, y)); 
     } 
    }  

} 

public void printPixelMatrix(){ 
    for (int i = 0; i < image.getHeight(); i++) { 
     for (int j = 0; j < image.getWidth(); j++) { 
      System.out.print(" "+pixels[j][i] + " "); 
     } 
     System.out.print("\n"); 
    }   

} 

回答

1

我不使用Java,所以我打算在一般计算机图形学知识......

1)首先,你可能需要设置一个您的缓冲图像(即:4个字节,其中对于R + G + B + Alpha的每个组件,您有1个字节)。像BufferedImage.TYPE_INT_ARGB

2)我不明白你为什么试图把image.getRGB(x, y)的结果放到一个字节中。从Java文档看来,getRGB(x, y)返回的数组并非单个数字,即使如此,该数字将所有ARGB合并为一个,但一个字节只能保存一个组件的最大值(最大为255,但颜色int可能如4278254335分布在4个字节)。

建议的解决方案:而不是字节(和步幅?),只是扫描像素,并获得像素值的int。然后打印这些值。 Black = 0以及White = 4278254335。我认为我在下面得到了十六进制格式代码,所以应该显示为:Black = FF000000,然后White = FFFFFFFF

我认为你的最终代码应该像下面显示的那样工作。请修正任何错误(我把意见,所以你看到我想做的事情)。这应该给你在预期的位置的黑色或黑色与白线预期的颜色:

//# load your old one as usual 
image = ImageIO.read(f); 

//# Create a new bufferdImage with type (will draw old into this) //also consider TYPE_INT_RGB 
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); 

//# Then draw original into new type... 
Graphics2D g = newImage.createGraphics(); 
g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); 
g.dispose(); //needed or not??? 

//# Doing the For-loop 
int imgW = image.getWidth(); 
int imgH = image.getHeight(); 
int pixels[][] = new int[imgW][imgH]; //create a 2D array 

//# Fill Array : for each [x] pos we read down /column 
//# so that we fill [y] with those pixel values 
for (int x = 0; x < imgW; x++) 
{ 
    //On each X pos we scan all Y pixels in that column 
    for (int y = 0; y < imgH; y++) 
    { 
     int col = image.getRGB(x, y); 
     pixels[x][y] = col; 

     //printPixelARGB(col); //if you need this format instead of printPixelMatrix 
    } 
} 

//# To Print output 
public void printPixelMatrix() 
{ 
    for (int i = 0; i < image.getHeight(); i++) 
    { 
     for (int j = 0; j < image.getWidth(); j++) 
     { 
      //System.out.print(" " + pixels[j][i] + " "); 
      int c = pixels[j][i]; //get integer that was stored in the array 
      String HexVal = Integer.toHexString(c) //giveshex value like AARRGGBB 
      System.out.print(" " + HexVal + " "); 
     } 

     System.out.print("\n"); 
    }   

} 

//# Another helper function to print pixel values 
//# print out example blue : argb: 255, 0, 0, 255 
public void printPixelARGB(int pixel) 
{ 
    int alpha = (pixel >> 24) & 0xff; 
    int red = (pixel >> 16) & 0xff; 
    int green = (pixel >> 8) & 0xff; 
    int blue = (pixel) & 0xff; 
    System.out.println("ARGB : " + alpha + ", " + red + ", " + green + ", " + blue); 
} 
1

Java没有无符号的类型,所以你的“白色”的像素,这是最大值(0xff),被解释为负1

据推测,你的正面1是压缩神器。

+0

有没有什么办法,我可以删除positve 1的从图像中获得确切的像素数据?或者是与我的图像编辑器有关的压缩东西? –

+1

@PabloEstrada JPEG是基于丢失的压缩算法,你可以尝试使用不同的格式,比如png – MadProgrammer

1

我怀疑你是从JPEG量化获得一个神器。 JPEG压缩方块像素,而不是单个像素。

看看是否有办法改变你的量化表。如果你让他们都成为一个价值观,这应该消失。一些编码器为此使用“质量”设置。使用最高质量的设置。