image - 如何创建任意大小的黑白图像
问题描述
我想创建一个仅由黑白像素组成的任意大图像。我目前正在使用BufferedImage
. 但是,这达到了 65500 像素的硬性限制。
Exception in thread "main" javax.imageio.IIOException: Maximum supported image dimension is 65500 pixels
at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1007)
at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:371)
at java.desktop/javax.imageio.ImageWriter.write(ImageWriter.java:613)
at java.desktop/javax.imageio.ImageIO.doWrite(ImageIO.java:1628)
at java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1554)
如何创建任意大小的图像?
加分项:由于图像只有黑白,因此有效的数据格式会很棒。
解决方案
您只遇到了特定图像编写器的限制。当我尝试使用更适合真正黑白图像的 PNG 时,可以写入和读取更大的图像。
我使用以下代码进行测试:
public static void main(String[] args) throws IOException {
int[][] sizes = {{4000, 4000 }, {40_000, 40_000 }, {70_000, 4000 }, {700_000, 400 }};
for(int[] size: sizes) {
int width = size[0], height = size[1];
System.out.println("trying " + width + " x " + height);
BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_BYTE_BINARY);
Graphics2D gfx = bi.createGraphics();
gfx.setColor(Color.WHITE);
gfx.fillRect(0, 0, width, height);
gfx.setColor(Color.BLACK);
gfx.drawOval(20, 20, width - 40 , height - 40);
int off = Math.min(width, height) >>> 3;
gfx.drawOval(off, off, width - off - off, height - off - off);
gfx.dispose();
test(bi, "PNG");
test(bi, "GIF");
test(bi, "JPEG");
}
}
static void test(BufferedImage bi, String format) throws IOException {
Path f = Files.createTempFile("test", "-out."+format);
try {
try { ImageIO.write(bi, format, f.toFile()); }
catch(Throwable t) {
System.err.println(format+": "+t);
return;
}
BufferedImage read;
try { read = ImageIO.read(f.toFile()); }
catch(Throwable t) {
System.err.println(format+": written, re-reading: "+t);
return;
}
if(bi.getWidth() != read.getWidth() || bi.getHeight() != read.getHeight()) {
System.err.println(format+": size changed to "
+read.getWidth() + " x " + read.getHeight());
return;
}
if(format.equalsIgnoreCase("jpeg")) {
// turn back to black & white, rounding away JPEG artifacts
BufferedImage bw = new BufferedImage(
read.getWidth(), read.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
Graphics2D gfx = bw.createGraphics();
gfx.drawImage(read, 0, 0, null);
gfx.dispose();
read = bw;
}
for(int x = 0, w = bi.getWidth(); x < w; x++) {
for(int y = 0, h = bi.getHeight(); y < h; y++) {
if(bi.getRGB(x, y) != read.getRGB(x, y)) {
System.err.println(format+": content changed");
return;
}
}
}
System.out.println(format + ": sucessfully written and restored");
}
finally {
Files.deleteIfExists(f);
}
}
结果:
trying 4000 x 4000
PNG: sucessfully written and restored
GIF: sucessfully written and restored
JPEG: sucessfully written and restored
trying 40000 x 40000
PNG: sucessfully written and restored
GIF: sucessfully written and restored
JPEG: written, re-reading: java.lang.IllegalArgumentException: Invalid scanline stride
trying 70000 x 4000
PNG: sucessfully written and restored
GIF: size changed to 4464 x 4000
JPEG: javax.imageio.IIOException: Maximum supported image dimension is 65500 pixels
trying 700000 x 400
PNG: sucessfully written and restored
GIF: size changed to 44640 x 400
JPEG: javax.imageio.IIOException: Maximum supported image dimension is 65500 pixels
我想,我无法创建BufferedImage
宽度 × 高度超出int
值范围的对象。这种限制似乎在实现的几个地方被硬编码。
推荐阅读
- javascript - 是否可以在 Select 标签上的 Javascript onChange 函数中使用枚举值而不是索引?
- javascript - SAP Cloud SDK for Javascript:预交付的 OData 服务 VS 自行生成的客户端之间的区别
- mongodb - MongoDB - 自动完成 - 获取所有以 X 开头的单词
- c++ - 最长递增和递减子序列(自上而下,带记忆)
- php - 如何省略关联数组以使用 PHP 中的项目?
- javascript - 每次发布后如何强制完全刷新(重新下载缓存文件)
- r - 在 geom_boxplot 中对齐标签
- javascript - 在 puppeteer page.evaluate 中找不到文本
- qt - 当我更新 QGraphicsPixmapItem 的 QPixmap 时,图像从场景中消失
- c# - 如何在 Unity GUI 中构建三个水平触摸的按钮