gpt4 book ai didi

java - 从磁盘读取图像时出现 ArrayOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-04 17:59:55 28 4
gpt4 key购买 nike

目前我正在尝试用 java 打印一些 pdf。使用的 API 是 Apache PDF Box 2.0 版。转换图像后,我将它们写入磁盘以节省内存。在下一步中,我再次阅读它们并将标题写在图像的头部。之后我又写了一遍。为了使用它们,我分别再次阅读了它们。当我最后一次阅读它们时,每 200-300 张图像就会出现以下异常:

java.lang.IndexOutOfBoundsException: off < 0 || len < 0 || off+len > b.length || off+len < 0!
at javax.imageio.stream.MemoryCacheImageInputStream.read(MemoryCacheImageInputStream.java:100)
at com.sun.imageio.plugins.common.SubImageInputStream.read(SubImageInputStream.java:61)
at com.sun.imageio.plugins.common.InputStreamAdapter.read(InputStreamAdapter.java:47)
at java.io.SequenceInputStream.read(SequenceInputStream.java:207)
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:238)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1104)
at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1215)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1330)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1606)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1400)
at my.code.Class.method()

我使用以下代码转换 PDF:

final HashMap<Integer, File> images = new HashMap<>();
PDDocument document = PDDocument.load(sourceFile);

PDFRenderer pdfRenderer = new PDFRenderer(document);
final ExecutorService service = Executors.newFixedThreadPool(4);
for (int page = 0; page < document.getNumberOfPages(); ++page)
{
final int finalPageNumber = page;
Runnable r = () -> {
try
{
//Java could only print with 72 dpi so I'll use it
BufferedImage image = pdfRenderer.renderImageWithDPI(finalPageNumber, 72);
File imageFile = new File(sourceFile.getAbsolutePath() + "-" + (finalPageNumber + 1) + ".png");
ImageIO.write(image, "png", imageFile);
image.flush();
images.put(finalPageNumber, imageFile);
}
catch (final IOException e)
{
e.printStackTrace();
}
};
Thread t = new Thread(r, page + "");
t.setName("" + page);
service.submit(t);
}

为了阅读,使用了以下代码:

// example url: /tmp/example.pdf-17.png
Image i = ImageIO.read(url);

如何解决这个问题


编辑

我忘记说的是我手动为图像添加了标题:

                BufferedImage bimage = new BufferedImage(   simple.getValue().getWidth(null),
simple.getValue().getHeight(null) + 50,
BufferedImage.TYPE_INT_ARGB);
Graphics bGr = bimage.createGraphics();
bGr.setColor(Color.WHITE);
bGr.fillRect(0, 0, bimage.getWidth(), bimage.getHeight());
bGr.setFont(new Font("Arial", Font.PLAIN, 15));
bGr.setColor(Color.BLACK);
bGr.drawImage(simple.getValue(), 0, 50, null);
bGr.drawString(entry.getValue(), 20, 20);
try
{
ImageIO.write(bimage, "PNG", new File(simple.getKey().toURI()));
}
catch (IOException | URISyntaxException e)
{
e.printStackTrace();
}
bGr.dispose();

这里是文件的链接(每个页面都可能出现):

Image which throws the exception

PDF file which is used (generated by Word2007)

最佳答案

我已经解决了我的问题。我没有等到图像的写作。现在我等待 ExecutorService 完成,一切正常。

我认为释放资源存在问题,因为我已经看到在 MemoryCacheImageInputStream 第 100 行抛出异常时一切正常。

关于java - 从磁盘读取图像时出现 ArrayOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36746053/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com