我正在尝试使用Image.IO.Write()
;保存图像 我基本上已经从这里复制了标准代码,用 lwjgl 截屏。我唯一做的就是使用现有目录作为保存路径初始化文件。
当我尝试保存图像时,FileNotFoundException
会被抛出。
glReadBuffer(GL_FRONT);
int width = Display.getDisplayMode().getWidth();
int height = Display.getDisplayMode().getHeight();
int bpp = 4; // Assuming a 32-bit display with a byte each for red, green, blue, and alpha.
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
File file = new File("res/screenshots/ss_" + Sys.getTime() + ".png"); // The file to save to.
String format = "PNG"; // Example: "PNG" or "JPG"
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
{
int i = (x + (width * y)) * bpp;
int r = buffer.get(i) & 0xFF;
int g = buffer.get(i + 1) & 0xFF;
int b = buffer.get(i + 2) & 0xFF;
image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
}
try {
ImageIO.write(image, format, file);
} catch (IOException e) { e.printStackTrace(); }
堆栈跟踪:
java.io.FileNotFoundException: res\screenshots\ss_91733792.png (The system cannot find the path specified)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(Unknown Source)
at javax.imageio.stream.FileImageOutputStream.<init>(Unknown Source)
at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(Unknown Source)
at javax.imageio.ImageIO.createImageOutputStream(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
at com.va.util.Extras.takeScreenShot(Extras.java:46)
at com.va.core.Engine.start(Engine.java:117)
at Main.main(Main.java:26)
Exception in thread "main" java.lang.NullPointerException
at javax.imageio.ImageIO.write(Unknown Source)
at com.va.util.Extras.takeScreenShot(Extras.java:46)
at com.va.core.Engine.start(Engine.java:117)
at Main.main(Main.java:26)