2012-08-13 59 views
0

我有Image_1.jpg.I在Image_1.jpg中獲得二維數組中的圖像像素值。我已經設置了第一個8 * 8塊二維數組的值,如下所示。如何獲得二維圖像數組的價值和設置值在Java中

16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 
16 , 16 , 16 , 16 , 16 , 16 , 16 , 16 , 

and construct image as Img.jpg。再次採取這個Img.jpg圖像閱讀這在二維陣列和讀取第一個8 * 8 blocks.I得到以下值。

-16776690 , -16776690 , -16776432 , -16776176 , -16775922 , -16775927 , -16776190 , -16645120 , 
-16776944 , -16776688 , -16776432 , -16776176 , -16775922 , -16775925 , -16776188 , -16645376 , 
-16777198 , -16776940 , -16776940 , -16776684 , -16776430 , -16776434 , -16776697 , -16580096 , 
-16777196 , -16777196 , -16776939 , -16776683 , -16776428 , -16776432 , -16776695 , -16580350 , 
-16646124 , -16711660 , -16777195 , -16776937 , -16776683 , -16776686 , -16711413 , -16449532 , 
-16646126 , -16711662 , -16776940 , -16776940 , -16776940 , -16776944 , -16711413 , -16449532 , 
-16449523 , -16580594 , -16711664 , -16776944 , -16776944 , -16776947 , -16645881 , -16384000 , 
-16449529 , -16449527 , -16646133 , -16776693 , -16776695 , -16776442 , -16645632 , -16383744 , 

我希望這些值爲16.我爲此寫了下面的代碼,我該怎麼辦?

/* 
    * To change this template, choose Tools | Templates 
    * and open the template in the editor. 
    */ 
    package testing; 

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

    /** 
    * 
    * @author pratibha 
    */ 
    public class ConstructImage{ 
     int[][] PixelArray; 
     public ConstructImage(){ 
      try{ 

      BufferedImage bufferimage=ImageIO.read(new File("C:\\photo\\Modified\\image_1.jpg")); 
      int height=bufferimage.getHeight(); 
      int width=bufferimage.getWidth(); 
      PixelArray=new int[width][height]; 
      for(int i=0;i<width;i++){ 
       for(int j=0;j<height;j++){ 
        PixelArray[i][j]=bufferimage.getRGB(i, j); 
       } 
      } 
      ///////create Image from this PixelArray 

      for(int i=0;i<8;i++){ 
       for(int j=0;j<8;j++){ 
        PixelArray[i][j]=16; 
       } 
      } 
      for(int i=0;i<8;i++){ 
       for(int j=0;j<8;j++){ 
        System.out.print(PixelArray[i][j]+" , "); 
       } 
       System.out.println(""); 
      } 






      BufferedImage bufferImage2=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); 
       for(int x=0;x<width;x++){ 
        for(int y=0;y<height;y++){ 
       bufferImage2.setRGB(x, y, PixelArray[x][y]); 

       } 
      } 

      File outputfile = new File("C:\\photo\\Modified\\img.jpg"); 
      ImageIO.write(bufferImage2, "jpg", outputfile); 
      /////////////////////////////////////////////////////////////////// 
        BufferedImage bufferimage1=ImageIO.read(new File("C:\\photo\\Modified\\img.jpg")); 
      int height1=0; 
      height1=bufferimage1.getHeight(); 
      int width1=0; 
      width1=bufferimage1.getWidth(); 
      int[][] PixelArray1=new int[width1][height1]; 
      for(int i=0;i<width1;i++){ 
       for(int j=0;j<height1;j++){ 
        PixelArray1[i][j]=bufferimage1.getRGB(i, j); 
       } 
      } 
      ///////create Image from this PixelArray 

      for(int i=0;i<8;i++){ 
       for(int j=0;j<8;j++){ 
        System.out.print(PixelArray1[i][j]+" , "); 
       } 
       System.out.println(); 
      } 
      System.out.println(Integer.toBinaryString(222)); 
      System.out.println(Integer.signum(93226268));; 
      } 
      catch(Exception ee){ 
       ee.printStackTrace(); 
      } 
     } 

     public static void main(String args[]){ 
      ConstructImage c=new ConstructImage(); 
     } 
    } 
+0

請幫我.. – Sumit 2012-08-13 11:41:36

+0

你的問題還不清楚。你想複製圖像嗎?你想提取一個子圖像?你想轉換圖像嗎?你想打印到控制檯嗎? – Matt 2012-08-13 11:43:11

+0

其實我想先設置數值8 * 8的圖像塊爲Strevilography – Sumit 2012-08-13 11:44:56

回答

1

我不知道,你要直接與int設置Image值...我猜你真正想要做的是將其設置爲「灰度值16」。你實際做的是將像素設置爲「紅色0,綠色0,藍色16」。

首先,嘗試創建一個Color(16,16,16),然後在其上調用getRGB()

順便說一句,JPG是一個lossy compression,所以你不能指望保存/加載時的值完全匹配 - 你可能想用PNG代替。順便說一下,除非您試圖滿足第三方庫的API要求,否則我會建議您直接訪問BufferedImage類,而不是嘗試創建陣列副本。未壓縮的圖像可以很快得到確實,並且由於不必要地增加了內存使用量而導致內存不足。