2013-12-23 40 views
0

我是Java編程的初學者。我有算法,但我無法編碼。我想比較RGB十六進制像素值的位置2和4上的2個相似圖像,以檢查其中一個圖像是否具有與另一個圖像不同的像素值。錯誤指出程序無法找到hexa2和hexa4的符號

我的算法邏輯:

  1. if(image1.substring2 == image2.substring2) && (image1.substring4 == image2.substring4),像素是相同的。
  2. if(image1.substring2 != image2.substring2) || (image1.substring4 != image2.substring4),像素不一樣。
  3. if(image1.substring2 != image2.substring2) && (image1.substring4 != image2.substring4),像素不一樣。

我在這裏剩下的代碼。我嘗試將所有過程分開,以便稍後排除故障。

//主

public class getPixelRGB1 
    { 
private static int a; 
private static int r; 
private static int g; 
private static int b; 
private static final double bitPerColor = 4.0; 


public static void main(String[] args) throws IOException 
{ 
    FileInputStream image = null; 
    FileInputStream image2 = null; 

    getPixelData1 newPD = new getPixelData1(); 

    try { 
     BufferedImage img, img2; 

     File file = new File("img0.jpg"); 
     File file2 = new File("imgstega.jpg"); 
     image = new FileInputStream(file); 
     image2 = new FileInputStream(file2); 
     img = ImageIO.read(image); 
     img2 = ImageIO.read(image2); 

     int rowcol; 
     int width = img.getWidth(); 
     int height = img.getHeight(); 
     System.out.println("Image's Width: " + width); 
     System.out.println("Image's Height: " + height); 

     int[][] pixelData = new int[width * height][3]; 

     System.out.println("Pixel Data: " + pixelData); 

     int[] rgb; 
     int count = 0; 

     for(int i=0; i<width; i++) 
     { 
      for(int j=0; j<height; j++) 
      { 
       rgb = newPD.getPixelData(img, i, j); 

       for(int k = 0; k < rgb.length; k++) 
       { 
        pixelData[count][k] = rgb[k]; 

       } 
       count++; 
       System.out.println("\nRGB Counts: " + count); 
      } 
     } 

     int width2 = img2.getWidth(); 
     int height2 = img2.getHeight(); 
     System.out.println("Image's Width: " + width2); 
     System.out.println("Image's Height: " + height2); 

     int[][] pixelData2 = new int[width2 * height2][3]; 

     int[] rgb2; 
     int counter = 0; 

     for(int i=0; i<width2; i++) 
     { 
     for(int j=0; j<height2; j++) 
     { 
     rgb2 = newPD.getPixelData(img2, i, j); 

     for(int k = 0; k < rgb2.length; k++) 
     { 
     pixelData2[counter][k] = rgb2[k]; 

     } 
     counter++; 
     System.out.println("\nRGB2 Counts: " + counter); 
     } 
     } 

    } catch (FileNotFoundException ex) { 
     Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, null, ex); 
    } finally { 
     try { 
      image.close(); 
     } catch (IOException ex) { 
      Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

} 
} 

//第一進程 - 獲取RGB像素值

public class getPixelData1 
{ 
private static final double bitPerColor = 4.0; 

public int[] getPixelData(BufferedImage img, int w, int h) throws IOException 
{ 
    int argb = img.getRGB(w, h); 
    int rgb[] = new int[] 
    { 
     (argb >> 16) & 0xff, //red 
     (argb >> 8) & 0xff, //green 
     (argb  ) & 0xff //blue 
    }; 

    int red = rgb[0]; 
    int green = rgb[1]; //RGB Value in Decimal 
    int blue = rgb[2]; 

    System.out.println("\nRGBValue in Decimal --> " + "\nRed: " + red + " Green: " + green + " Blue: " + blue); 

    //Convert each channel RGB to Hexadecimal value 
    String rHex = Integer.toHexString((int)(red)); 
    String gHex = Integer.toHexString((int)(green)); 
    String bHex = Integer.toHexString((int)(blue)); 

    System.out.println("\nRGBValue in Hexa --> " + "\nRed Green Blue " + rHex + gHex + bHex); 

    //Check position 2 and 4 of hexa value for any changes 
    String hexa2, hexa4 = ""; 
    String rgbHexa = rHex + gHex + bHex; 

    hexa2 = rgbHexa.substring(1,2); 
    System.out.println("\nString RGB Hexa: " + rgbHexa); 
    System.out.println("\nSubstring at position 2: " + hexa2); 

    String hex = String.format("%02X%02X%02X", red, green, blue); 
    hexa4 = hex.substring(3,4); 
    System.out.println("\nSubstring at position 4: " + hexa4); 

    return rgb; 
} 
} 

//第二個過程 - 來比較兩個圖像的RGB十六進制數值

public class compareHexaRGB 
{ 
public int[] compareHexaRGB(BufferedImage img, BufferedImage img2, int w, int h) throws IOException 
{ 
    getPixelData1 newPD = new getPixelData1(); //get method from class getPixelData1 - is this correct? 

      if((img.hexa2.equals(img2.hexa2)) && (img.hexa4.equals(img2.hexa4))) 
       { 
        System.out.println("Pixel values at position 2 and 4 are the same."); 
       } 
      else if((img.hexa2 != img2.hexa2) || (img.hexa4 != img2.hexa4)) 
       { 
        System.out.println("Pixel values at position 2 and 4 are not the same."); 
       } 
      else if((img.hexa2 != img2.hexa2) && (img.hexa4 != img2.hexa4)) 
       { 
        System.out.println("Pixel values at position 2 and 4 are not the same."); 
       } 
} 
} 

錯誤指出程序無法在bufferedImage中找到hexa2和hexa4的符號。有人可以檢查我在這裏編碼是否做錯了嗎?我還是Java新手。

回答

1

imgBufferedImagejavadoc)類型。它不包含任何名爲hexa2hexa4的非專用(也不是專用)字段。

你需要做的是重構你的代碼,以確保你有權訪問compareHexaRGB()。這可能有很多方法可以完成。也許你可以擴展BufferedImage來包含你的字段,或者你可以將它們作爲輸入傳遞給方法。

考慮到我們並沒有真正擁有所有代碼(例如,我沒有看到compareHexaRGB()被調用),哪個解決方案更優雅,哪個解決方案難以確定。

更精確地對編輯的問題:通過使用img.hexa2訪問一個字段,你認爲有一個在BufferedImage稱爲hexa2字段是從你的類訪問。例如,如果某個字段被聲明爲public,則爲真。更典型的是,這些字段的範圍是private,需要由getter/setter訪問。對於BufferedImage,根本不存在這樣的字段。

瞭解訪問控制here

+0

我應該怎麼做呢?我不太瞭解這個概念。我應該創建另一種方法來獲取bufferedimage值和hexa2和hexa4值嗎?我很困惑 – user2890264

+0

@ user2890264我只是向你解釋爲什麼你不能在'BufferedImage'中訪問'hexa2'和'hexa4'。你應該怎麼做是一個完全不同的問題。您可能需要仔細考慮代碼,並在遇到特定問題時發佈另一個問題。 – Magnilex