0

I got this error.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: rgb2 cannot be resolved to a variable

its always the rgb2 array that caused the error. How to solve this problem?

    BufferedImage img1 = ImageIO.read(file1);
    BufferedImage img2 = ImageIO.read(file2);

    int w = img1.getWidth();
    int h = img1.getHeight();

    long diff = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        int rgb1[] = img1.getRGB(x, y, w, h, rgb1, 0, w);
        int rgb2[]= img2.getRGB(x, y, w, h, rgb2, 0, w);
        int index = y * w + x;
        int r1 = (rgb1[index] >> 16) & 0xff;
        int g1 = (rgb1[index] >>  8) & 0xff;
        int b1 = (rgb1[index]      ) & 0xff;
        int r2 = (rgb2[index] >> 16) & 0xff;
        int g2 = (rgb2[index]>>  8) & 0xff;
        int b2 = (rgb2[index]     ) & 0xff;
        r2 += Math.abs(r2 - r1);
        g2 += Math.abs(g2 - g1);
        b2 += Math.abs(b2 - b1);

        rgb2[index] = (((r2 & 0xff) << 16) + ((g2 & 0xff) << 8) +(b2 & 0xff));
        rgb2[index] = (rgb2[index]*17);
      }
    }

    int i = 0;
    for (int y = 0; y < h; y++) {
      int red = (y * 255) / (h - 1);
      for (int x = 0; x < w; x++) {
        int green = (x * 255) / (w - 1);
        int blue = 128;
        rgb2[i++] = (red << 16) | (green << 8) | blue;//the problem is at this line
      }
    }
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    image.setRGB(0, 0, w, h, rgb2, 0, w);
    Graphics g = image.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    File imageFile = new File("saved.jpeg");
    ImageIO.write(image, "jpg", imageFile);

}

I got this error after declare outside the loop. Exception in thread "main" java.lang.Error: Unresolved compilation problems: The local variable rgb1 may not have been initialized

    int w = img1.getWidth();
    int h = img1.getHeight();
    int scale = w * h * 3;
    int rgb1[] = img1.getRGB(0, 0, w, h, rgb1, 0, w);
    int rgb2[] = img2.getRGB(0, 0, w, h, rgb2, 0, w);

2 Answers 2

2

Your problem is because rgb2[] is declared inside a for loop immediately before, in this line:

int rgb2[]= img2.getRGB(x, y, w, h, rgb2, 0, w);

Then the for loop ends, so rgb2[] falls out of scope and is released from memory, and no longer defined. If you want rgb2[] to be accessible from outside the loop, you have to say int rgb2[]; before the loop is called, so that the variable is in the same scope as the line where it needs to be called. Remember, scope is inherited downwards -- anything accessible immediately before the loop is accessible inside it -- but not the other way around.

Sign up to request clarification or add additional context in comments.

Comments

0

You declared your rgb2 variable inside your first for loop, which is not visible to your second for loop, where the problem occurs. To fix it, simply declare rgb2 array before your for loops.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.