我的代码正在错误地绘制图像。每次我试着猜出什么是错的并修复它,我得到arrayOutOfBounds异常。所有阵列业务都在混淆我。我真的需要一些帮助。谢谢!
(顺便说一下,我确实需要提取像素值并存储它们,这样我就可以手动对数据进行模糊,锐化和其他类型的方法。我正在学习图像处理的工作原理。)
此代码给出了这个结果(当图像应该是正面向上时,图像是横向的):
这是原始图片:
Image.java:
package mycode;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
public class Image {
public int height;
public int width;
public int image[][];
public Image(BufferedImage openImage)
{
height = openImage.getHeight();
width = openImage.getWidth();
image = new int[openImage.getWidth()][openImage.getHeight()];
for(int x = 0; x < openImage.getWidth(); x++)
{
for(int y = 0; y < openImage.getHeight(); y++)
{
image[x][y] = 0xff & openImage.getRGB(x, y);
Debug.print(""+Integer.toHexString(image[x][y]));
}
}
}
}
refreshView(): (model.image是上面Image类的一个实例。)
@Override
public void refreshView(Graphics2D g2d)
{
//Draw the background image.
if(ControllerState.inImageMode && model.image != null)
{
Debug.print("Drawing background image");
BufferedImage bufferedImage = new BufferedImage(model.image.width, model.image.height,
BufferedImage.TYPE_BYTE_GRAY);
WritableRaster wr = bufferedImage.getRaster();
for(int x = 0; x < model.image.width; x++)
{
for(int y = 0; y < model.image.height; y++)
{
wr.setPixels(0, x, model.image.width, 1, model.image.image[x]);
}
}
g2d.drawImage(bufferedImage, null, 0, 0);
}
}
答案 0 :(得分:1)
看起来你有一个经典的x-y行列混合问题。我想在你想要的for循环中:
wr.setPixels(x, 0, 1, model.image.height, model.image.image[x]);