Example usage for org.apache.poi.hwpf.model PicturesTable getAllPictures

List of usage examples for org.apache.poi.hwpf.model PicturesTable getAllPictures

Introduction

In this page you can find the example usage for org.apache.poi.hwpf.model PicturesTable getAllPictures.

Prototype

public List<Picture> getAllPictures() 

Source Link

Document

Not all documents have all the images concatenated in the data stream although MS claims so.

Usage

From source file:org.sleuthkit.autopsy.imageExtractor.ImageExtractor.java

private List<ExtractedImage> extractImagesFromDoc(AbstractFile af) {
    // TODO check for BBArtifact ENCRYPTION_DETECTED? Might be detected elsewhere...?
    List<ExtractedImage> listOfExtractedImages = new ArrayList<ExtractedImage>();
    String parentFileName = getUniqueName(af);
    HWPFDocument docA = null;/*from   w ww  . j ava2s .  co  m*/
    try {
        docA = new HWPFDocument(new ReadContentInputStream(af));
    } catch (IOException ex) {
        logger.log(Level.WARNING,
                "HWPFDocument container could not be instantiated while reading " + af.getName(), ex);
        return null;
    }
    PicturesTable pictureTable = docA.getPicturesTable();
    List<org.apache.poi.hwpf.usermodel.Picture> listOfAllPictures = pictureTable.getAllPictures();
    String outputFolderPath;
    if (listOfAllPictures.isEmpty()) {
        return null;
    } else {
        outputFolderPath = getOutputFolderPath(parentFileName);
    }
    if (outputFolderPath == null) {
        logger.log(Level.WARNING, "Could not get path for image extraction from AbstractFile: {0}",
                af.getName());
        return null;
    }
    for (org.apache.poi.hwpf.usermodel.Picture picture : listOfAllPictures) {
        FileOutputStream fos = null;
        String fileName = picture.suggestFullFileName();
        try {
            fos = new FileOutputStream(outputFolderPath + File.separator + fileName);
        } catch (FileNotFoundException ex) {
            logger.log(Level.WARNING, "Invalid path provided for image extraction", ex);
            continue;
        }
        try {
            fos.write(picture.getContent());
            fos.close();
        } catch (IOException ex) {
            logger.log(Level.WARNING, "Could not write to the provided location", ex);
            continue;
        }
        // TODO Extract more info from the Picture viz ctime, crtime, atime, mtime
        String fileRelativePath = File.separator + moduleDirRelative + File.separator + parentFileName
                + File.separator + fileName;
        long size = picture.getSize();
        ExtractedImage extractedimage = new ExtractedImage(fileName, fileRelativePath, size, af);
        listOfExtractedImages.add(extractedimage);
    }

    return listOfExtractedImages;
}

From source file:org.sleuthkit.autopsy.modules.embeddedfileextractor.ImageExtractor.java

License:Open Source License

/**
 * Extract images from doc format files.
 *
 * @param af the file from which images are to be extracted.
 *
 * @return list of extracted images. Returns null in case no images were
 *         extracted.//from ww  w . j  a va 2  s  .  c  o  m
 */
private List<ExtractedImage> extractImagesFromDoc(AbstractFile af) {
    List<ExtractedImage> listOfExtractedImages;
    HWPFDocument doc = null;
    try {
        doc = new HWPFDocument(new ReadContentInputStream(af));
    } catch (Throwable ex) {
        // instantiating POI containers throw RuntimeExceptions
        logger.log(Level.WARNING,
                NbBundle.getMessage(this.getClass(),
                        "EmbeddedFileExtractorIngestModule.ImageExtractor.docContainer.init.err", af.getName()),
                ex); //NON-NLS
        return null;
    }

    PicturesTable pictureTable = null;
    List<org.apache.poi.hwpf.usermodel.Picture> listOfAllPictures = null;
    try {
        pictureTable = doc.getPicturesTable();
        listOfAllPictures = pictureTable.getAllPictures();
    } catch (Exception ex) {
        // log internal Java and Apache errors as WARNING
        logger.log(Level.WARNING, NbBundle.getMessage(this.getClass(),
                "EmbeddedFileExtractorIngestModule.ImageExtractor.processing.err", af.getName()), ex); //NON-NLS
        return null;
    }

    String outputFolderPath;
    if (listOfAllPictures.isEmpty()) {
        return null;
    } else {
        outputFolderPath = getOutputFolderPath(this.parentFileName);
    }
    if (outputFolderPath == null) {
        return null;
    }
    listOfExtractedImages = new ArrayList<>();
    byte[] data = null;
    for (org.apache.poi.hwpf.usermodel.Picture picture : listOfAllPictures) {
        String fileName = picture.suggestFullFileName();
        try {
            data = picture.getContent();
        } catch (Exception ex) {
            // log internal Java and Apache errors as WARNING
            logger.log(Level.WARNING,
                    NbBundle.getMessage(this.getClass(),
                            "EmbeddedFileExtractorIngestModule.ImageExtractor.processing.err", af.getName()),
                    ex); //NON-NLS
            return null;
        }
        writeExtractedImage(Paths.get(outputFolderPath, fileName).toString(), data);
        // TODO Extract more info from the Picture viz ctime, crtime, atime, mtime
        listOfExtractedImages
                .add(new ExtractedImage(fileName, getFileRelativePath(fileName), picture.getSize(), af));
    }

    return listOfExtractedImages;
}