Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ApachePOI从文档中获取图像_Java_Image_Apache Poi - Fatal编程技术网

Java 使用ApachePOI从文档中获取图像

Java 使用ApachePOI从文档中获取图像,java,image,apache-poi,Java,Image,Apache Poi,我正在使用ApachePOI从docx读取图像 这是我的密码: enter code here public Image ReadImg(int imageid) throws IOException { XWPFDocument doc = new XWPFDocument(new FileInputStream("import.docx")); BufferedImage jpg = null; List<XWPFPictureData> pic = d

我正在使用ApachePOI从docx读取图像

这是我的密码:

enter code here

public Image ReadImg(int imageid) throws IOException {
    XWPFDocument doc = new XWPFDocument(new FileInputStream("import.docx"));
    BufferedImage jpg = null;
    List<XWPFPictureData> pic = doc.getAllPictures();
    XWPFPictureData pict = pic.get(imageid);
    String extract = pict.suggestFileExtension();
    byte[] data = pict.getData();
    //try to read image data using javax.imageio.* (JDK 1.4+)
    jpg = ImageIO.read(new ByteArrayInputStream(data));
    return jpg;
}
在此处输入代码
公共映像ReadImg(int-imageid)引发IOException{
XWPFDocument doc=新的XWPFDocument(新文件输入流(“import.docx”);
BuffereImage jpg=null;
List pic=doc.getAllPictures();
xwpfpicturedatapict=pic.get(imageid);
String extract=pict.suggestFileExtension();
字节[]数据=pict.getData();
//尝试使用javax.imageio.*(JDK1.4+)读取图像数据
jpg=ImageIO.read(新的ByteArrayInputStream(数据));
返回jpg;
}
它可以正确地读取图像,但不能按顺序读取

例如,如果文档包含

图像1.jpeg 图像2.jpeg 图像3.jpeg 图像4.jpeg 图像5.jpeg

上面写着

图4 图3 图1 图5 图2

你能帮我解决这个问题吗

我想按顺序阅读图片

谢谢, Sithik

公共静态图像(XWPFDocument docx){
试一试{
List piclist=docx.getAllPictures();
//遍历列表并将每个图像写入文件
迭代器迭代器=piclist.Iterator();
int i=0;
while(iterator.hasNext()){
xwpfpicturedatapic=iterator.next();
byte[]bytepic=pic.getData();
BuffereImage imag=ImageIO.read(新的ByteArrayInputStream(bytepic));
write(imag,“jpg”,新文件(“D:/imagefromword/”+pic.getFileName());
i++;
}
}捕获(例外e){
系统退出(-1);
}
}

是什么让你认为它以无序的方式读取图像?List pic=doc.getAllPictures();如果我传递imageid=0,那么它将返回image4.jpeg…理想情况下,它应该返回image1.jpeg…或者请建议我如何实现这一点?doc.getAllPictures()从文档中检索图像的依据是什么?一种解决方案是对生成的列表进行排序。我相信这个方法使用了一个ArrayList,所以应该保持插入顺序。当文档中有100个图像时,您能提供一些示例如何进行排序吗?如果你给出代码示例会很有帮助吗?你能用一两句话解释一下你是如何解决这个问题的吗?这将使您的答案对未来的读者更有用,并帮助OP了解更多内容,而不仅仅是复制代码。
public static void extractImages(XWPFDocument docx) {
    try {

        List<XWPFPictureData> piclist = docx.getAllPictures();
        // traverse through the list and write each image to a file
        Iterator<XWPFPictureData> iterator = piclist.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            XWPFPictureData pic = iterator.next();
            byte[] bytepic = pic.getData();
            BufferedImage imag = ImageIO.read(new ByteArrayInputStream(bytepic));
            ImageIO.write(imag, "jpg", new File("D:/imagefromword/" + pic.getFileName()));
            i++;
        }

    } catch (Exception e) {
        System.exit(-1);
    }

}