2013-12-23 37 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