遍历2d数组

1tuwyuhd  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(265)

嗨,我想每12分钟重复一次 image 像素如果像素是黑色的,我想在我的数组上加1,否则加0。

  1. int width = image.getWidth();
  2. int height = image.getHeight();
  3. int[][] pixels = new int[height / 12][width / 12];
  4. int counterX = 1;
  5. int counterY = 1;
  6. for (int i = 0; i < width / 12; i++) {
  7. for (int j = 0; j < height / 12; j++) {
  8. if (counterX == 1 && counterY == 1) {
  9. int rgb = image.getRGB(counterX, counterY);
  10. String s = Integer.toHexString(rgb);
  11. if (s.equals("ff000000")) {
  12. pixels[i][j] = 1;
  13. } else {
  14. pixels[i][j] = 0;
  15. }
  16. counterX += 12;
  17. } else {
  18. int rgb = image.getRGB(counterX, counterY);
  19. String s = Integer.toHexString(rgb);
  20. if (s.equals("ff000000")) {
  21. pixels[i][j] = 1;
  22. } else {
  23. pixels[i][j] = 0;
  24. }
  25. counterX += 12;
  26. if (counterX > width) {
  27. counterX = 1;
  28. counterY += 12;
  29. if (counterY > height) {
  30. counterY = height;
  31. }
  32. }
  33. }
  34. }
  35. }
  36. return pixels;

我的索引41超出了长度41的界限,它发生在这一行 pixels[i][j] = 0; x有点问题。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题