图像横向绘制到屏幕(Java BufferedImage,WritableRaster)
Image is drawing sideways to screen (Java BufferedImage, WritableRaster)
我的代码绘制图像不正确。每次我尝试猜测出了什么问题并修复它时,我都会遇到 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 是上述图像 class 的实例。)
@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);
}
}
您似乎遇到了典型的 x-y 行列混淆问题。我认为在你的 for 循环中你想要:
wr.setPixels(x, 0, 1, model.image.height, model.image.image[x]);
我的代码绘制图像不正确。每次我尝试猜测出了什么问题并修复它时,我都会遇到 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 是上述图像 class 的实例。)
@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);
}
}
您似乎遇到了典型的 x-y 行列混淆问题。我认为在你的 for 循环中你想要:
wr.setPixels(x, 0, 1, model.image.height, model.image.image[x]);