我试图在没有标准方法的情况下旋转图像,制作颜色数组并对其进行操作,但是当我调用旋转时,我得到黑点(看图片)
这是我的代码,colScaled是我想要转换为数组的图片:
public void arrays() {
colScaled = zoom2();
int j = 0;
int i = 0;
angel = Integer.parseInt(this.mn.jTextField1.getText());
float degree = (float) Math.toRadians(angel);
float cos = (float) Math.cos(degree);
float sin = (float) Math.sin(degree);
int W = Math.round(colScaled[0].length * Math.abs(sin) + colScaled.length * Math.abs(cos));
int H = Math.round(colScaled[0].length * Math.abs(cos) + colScaled.length * Math.abs(sin));
int x;
int y;
int xn = (int) W / 2;
int yn = (int) H / 2;
int hw = (int) colScaled.length / 2;
int hh = (int) colScaled[0].length / 2;
BufferedImage image = new BufferedImage(W + 1, H + 1, im.getType());
for (i = 0; i < colScaled.length; i++) {
for (j = 0; j < colScaled[0].length; j++) {
x = Math.round((i - hw) * cos - (j - hh) * sin + xn);
y = Math.round((i - hw) * sin + (j - hh) * cos + yn);
image.setRGB(x, y, colScaled[i][j]);
}
}
ImageIcon ico = new ImageIcon(image);
this.mn.jLabel1.setIcon(ico);
}
答案 0 :(得分:0)
请注意代码中的此块: -
for (i = 0; i < colScaled.length; i++) {
for (j = 0; j < colScaled[0].length; j++) {
x = Math.round((i - hw) * cos - (j - hh) * sin + xn);
y = Math.round((i - hw) * sin + (j - hh) * cos + yn);
image.setRGB(x, y, colScaled[i][j]);
}
}
x
和y
是来源图像(colScaled
)中的像素坐标。
此代码的目标是填充目标图像(image
)中的所有像素。
在循环中,无法保证目标图像中的所有像素都会被填充,即使它位于矩形区域中也是如此。
上图描绘了问题 看到? 目标图像中的红色像素可能无法写入。
正确的解决方法是迭代目标图像中的像素,然后在源图像中找到相应的像素。
编辑:发帖后,我刚看到Spektre的评论。
我同意,这似乎是一个重复的问题。单词&#34;像素阵列&#34;让我成为不是的东西。