java为什么我的BuffereImage在绘制到画布时会有所不同?
原创的
https://drive.google.com/file/d/1B3xxfWkGsMs2_MQ_bUQ8_ALYI0DL-LIo/view?usp=sharing
保存到文件时
https://drive.google.com/file/d/1z5euXupeHmiFebch4A39fVqGukoUiK0p/view?usp=sharing
当打印到画布上时
https://drive.google.com/file/d/1VouD-ygf0pPXFFx9Knr4pv44FHMtoqcV/view?usp=sharing
BufferedImage temp = bImg.getSubimage(100, 100, (int)imgWidth - 100, (int)imgHeight - 100);
try{
ImageIO.write(temp, "png", new File("test.png"));
}catch(Exception e){
e.printStackTrace();
}
gc.drawImage(SwingFXUtils.toFXImage(temp, null), 100, 100);
出于某种原因,如果我将图像打印到画布上,与将同一图像保存到文件中不同。当我将其保存到文件时,它会正确地计算子图像,但当我将其打印到画布时,它会忽略我给出的x和y坐标,并使用(0,0)作为(x,y)以给定的宽度和高度获取子图像
# 1 楼答案
从documentation of the getSubimage method开始:
子图像只是进入原始图像的“窗口”;它们使用相同的像素数据
该SwingFXUtils.toFXImage documentation声明:
虽然只复制源图像维度中的像素肯定是有意义的,但上面的文字并不能完全清楚地表明它不会复制整个像素数据缓冲区,因此忽略了子图像的边界。我会认为这是一个错误,但我可以看到哪里可能有一个论点,它不是。
同时,您可以通过自己提取子图像来解决此问题: