java - 遍历二维数组
问题描述
嗨,我想遍历每 12 个image
像素。如果像素是黑色的,我想把 1 放在我的数组上,否则 0。
int width = image.getWidth();
int height = image.getHeight();
int[][] pixels = new int[height / 12][width / 12];
int counterX = 1;
int counterY = 1;
for (int i = 0; i < width / 12; i++) {
for (int j = 0; j < height / 12; j++) {
if (counterX == 1 && counterY == 1) {
int rgb = image.getRGB(counterX, counterY);
String s = Integer.toHexString(rgb);
if (s.equals("ff000000")) {
pixels[i][j] = 1;
} else {
pixels[i][j] = 0;
}
counterX += 12;
} else {
int rgb = image.getRGB(counterX, counterY);
String s = Integer.toHexString(rgb);
if (s.equals("ff000000")) {
pixels[i][j] = 1;
} else {
pixels[i][j] = 0;
}
counterX += 12;
if (counterX > width) {
counterX = 1;
counterY += 12;
if (counterY > height) {
counterY = height;
}
}
}
}
}
return pixels;
我的索引 41 超出了长度 41 的范围,它发生在这一行pixels[i][j] = 0;
X 有问题。
解决方案
推荐阅读
- android - 如何修复 Hidapi 未初始化:在资源路径 (.) 中找不到本机库 (com/sun/jna/android-arm/libjnidispatch.so)
- html - 附加弹性项目而不影响居中的兄弟姐妹
- excel - Excel公式将匹配两个条件的数字相加,一行一行地水平记录
- swift - 使用 REST API 将 pdf 文件上传到 Sharepoint
- twilio - Twilio WhatsApp HTTP 检索失败,但无法在本地重现错误
- c# - 尝试在 Xamarin 中连接到 MongoDB 客户端时出现系统聚合异常
- informatica - 在 Powercenter 中将“á”等字符替换为“a”
- android - 当您点击 chrome 上的链接时,有没有办法禁用“在 [App Name] 中打开”建议?
- typescript - 打字稿中的 AWS DynamoDB:尝试插入项目时没有响应或错误
- php - 如何使用 Oracle 和 Zend 获取插入的 id