错误表明程序找不到hexa2和hexa4的符号

时间:2013-12-23 06:31:30

标签: java substring

我是Java编程的初学者。我有算法,但我很难编码。我想比较RGB十六进制像素值的第2和第4位的2个相似图像,以检查其中一个图像的像素值是否与另一个不同。

我的算法逻辑:

  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),像素不一样。
  4. 我这里有其余的代码。我尝试将所有过程分开,以便以后进行故障排除。

    // MAIN

    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);
            }
        }
    
    }  
    }
    

    // 1ST PROCESS - 获取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;
    }
    }
    

    // 2nd Process - 比较两个图像的RGB Hex值

    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 个答案:

答案 0 :(得分:1)

img的类型为BufferedImagejavadoc)。它不包含任何名为hexa2hexa4的非私有(也称为私有)字段。

您需要做的是重构您的代码,以确保您可以在compareHexaRGB()中访问它们。可能有很多方法可以做到这一点。也许您可以扩展BufferedImage以包含您的字段,或者您可以将它们作为输入传递给方法。

考虑到我们并没有真正拥有所有代码(例如,我根本没有看到compareHexaRGB()被调用),哪个更优雅的解决方案很难确定。

更准确地说一下编译问题:通过使用img.hexa2访问字段,您可以假设hexa2中有一个名为BufferedImage的字段,可以从您的班级访问。例如,如果某个字段声明为public,则为true。更典型的是,字段为private范围,需要由getter / setter访问。对于BufferedImage,根本不存在此类字段。

了解访问控制here