Dear All,

To be brief, have drag and drop leading into a pdf to jpg converter. It was working, now stopped.

My thoughts are either;
1) something stuck in computer memory I can not erase (it is calling a pdf with more that allotted pages)
2) I may have a counting error, trying to print some page not there.

I tried throwing in pdf with successively larger page numbers and same error.

Please let me know your thoughts on where to narrow down the error.

The next few lines of where the error starts is below.

I use BlueJ as IDE. Have MtContainer as main, calling FileDrop and MLabel.

Thank you.
**

Nov 07, 2014 5:41:19 PM org.apache.pdfbox.util.PDFImageWriter writeImage
INFO: Writing: photos\/out24.jpg
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
    at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
    at java.awt.image.BufferedImage.<init>(BufferedImage.java:331)
    at MyContainer.buildDisplayer(MyContainer.java:152)
    at MyContainer$1.filesDropped(MyContainer.java:74)
    at FileDrop$1.drop(FileDrop.java:325)
    at java.awt.dnd.DropTarget.drop(DropTarget.java:450)

**

Recommended Answers

All 4 Replies

The code that generates that error would be more useful. Can you post that up?
But it seems likely your code is trying to access something that doesn't exist as that would explain why height and width aren't real values;

Here is the main program. The issue may be around lines 95-106.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.LookupOp;
import java.awt.image.Raster;
import java.awt.image.ShortLookupTable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFImageWriter;

public class MyContainer {
    private JFrame mainFrame;
    private JFrame concDisplay;
    private JFrame threshFrame;
    private JFrame concThresh;
    private ArrayList<String> images;
    private ArrayList<String> threshImgs;
    private JLabel mText;
    private JPanel dragDrop;
    private int sampleWidth = 600;
    private int sampleHeight = 600;
    private int minGrayThresh = 0;
    private int maxGrayThresh = 160;
    private String outPath = "photos\\";

    public void loadImages(String dir) {
        File imgDir = new File(dir);
        File[] imgs = imgDir.listFiles();
        for (int j = 0; j < imgs.length; j++) {
            if (imgs[j].getName().split("_")[0].compareTo("threshold") != 0)
                images.add(imgs[j].getAbsolutePath());
        }
        for (int j = 0; j < images.size(); j++) {
            for (int i = j + 1; i < images.size(); i++) {
                if (images.get(i).compareTo(images.get(j)) < 0) {
                    String temp = images.get(j);
                    images.set(j, images.get(i));
                    images.set(i, temp);
                }
            }
        }
    }

    public void dragNdrop() {
        dragDrop = new JPanel();
        dragDrop.add(mText);
        new FileDrop(dragDrop, new FileDrop.Listener() {
                public void filesDropped(java.io.File[] files) {
                    if (files.length == 1) {
                        pdftoImage(files[0].getAbsolutePath());
                        loadImages(outPath);
                    }
                    buildDisplayer(0);
                } // end filesDropped
            }); // end FileDrop.Listener
    }

    public String directoryChooser() {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("choosertitle");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        String directory = null;
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            directory = chooser.getSelectedFile().getAbsolutePath();
        }
        return directory;
    }

    public void updateImageZoomWindow() {
    }

    public void pdftoImage(String pdfPath) {
        try {
            PDDocument document = PDDocument.load(pdfPath);
            PDFImageWriter piw = new PDFImageWriter();
            piw.writeImage(document, "jpg", "", 1, Integer.MAX_VALUE, outPath
                + "/out");
            document.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void displaySingleImage(String imagePath, int type) {
        String title = "";
        JFrame concDisplay;
        if (type == 0) {
            title = "Single Page Display";
            concDisplay = this.concDisplay;
        } else {
            title = "single Thresholded Display";
            concDisplay = this.concThresh;
        }
        if (concDisplay == null)
            concDisplay = new JFrame(title);
        concDisplay.setVisible(false);
        ImageIcon imgIc = new ImageIcon(imagePath);
        JLabel image = new JLabel(imgIc);
        JScrollPane jsp = new JScrollPane(image);
        concDisplay.add(jsp);
        concDisplay.setSize(imgIc.getIconWidth(), imgIc.getIconHeight());
        concDisplay.setVisible(true);
    }

    public void buildDisplayer(final int type) {
        JFrame main;
        ArrayList<String> images;
        if (type == 0) {
            images = this.images;
            main = mainFrame;
            main.remove(dragDrop);
            dragDrop.setVisible(false);
            main.setVisible(false);
        } else {
            images = this.threshImgs;
            main = threshFrame;
            main = new JFrame("Threshold Displayer");
            main.setLayout(new GridLayout(1, images.size()));
            main.setBackground(new Color(255, 255, 255));
            // mainFrame.setLayout(new FlowLayout());
            main.setSize(800, 600);

        }
        JPanel container = new JPanel();
        for (int i = 0; i < images.size(); i++) {
            ImageIcon myIcon = new ImageIcon(images.get(i));
            Image img = myIcon.getImage();
            BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics g = bi.createGraphics();
            g.drawImage(img, 0, 0, sampleWidth, sampleHeight, null);
            final MLabel j = new MLabel(new ImageIcon(bi), images.get(i));
            j.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseReleased(MouseEvent arg0) {
                    }

                    @Override
                    public void mousePressed(MouseEvent arg0) {
                    }

                    @Override
                    public void mouseExited(MouseEvent arg0) {
                    }

                    @Override
                    public void mouseEntered(MouseEvent arg0) {
                    }

                    @Override
                    public void mouseClicked(MouseEvent arg0) {
                        displaySingleImage(j.imageDir, type);
                    }
                });
            container.add(j);
        }
        JScrollPane jsp = new JScrollPane(container);
        main.add(jsp);
        main.setVisible(true);
        if (type == 0) {
            try {
                thresholdingPhase();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void prepareGUI() {
        mainFrame = new JFrame("File Browser");
        mainFrame.setLayout(new GridLayout(1, images.size()));
        // mainFrame.setLayout(new FlowLayout());
        mainFrame.setSize(800, 600);
        mText = new JLabel("Drag single pdf file into here", JLabel.CENTER);
        mText.setSize(600, 400);
        dragNdrop();
        mainFrame.add(dragDrop);
        mainFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent windowEvent) {
                    System.exit(0);
                }
            });

        mainFrame.setVisible(true);
    }

    public void start() {
        File dir = new File(outPath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        images = new ArrayList<>();
        threshImgs = new ArrayList<String>();
        prepareGUI();
    }

    public BufferedImage thresholdImage(BufferedImage img) throws IOException {
        int black = 0;
        int white = 0;
        BufferedImage thresh = new BufferedImage(img.getWidth(),
                img.getHeight(), img.getType());
        white = (white | 0x00fffffff);
        for (int y = 0; y < img.getHeight(); y++) {
            for (int x = 0; x < img.getWidth(); x++) {
                int clr = img.getRGB(x, y);
                int red = (clr & 0x00ff0000) >> 16;
                int green = (clr & 0x0000ff00) >> 8;
                int blue = clr & 0x000000ff;
                if (!((red >= minGrayThresh && green >= minGrayThresh && blue >= minGrayThresh) && (red <= maxGrayThresh
                        && green <= maxGrayThresh && blue < maxGrayThresh))) {
                    thresh.setRGB(x, y, black);
                } else {
                    thresh.setRGB(x, y, white);
                }
            }
        }
        return thresh;
    }

    public void testShowImage(BufferedImage img) {
        JFrame frame = new JFrame("Test Show");
        JLabel lblimage = new JLabel(new ImageIcon(img));
        frame.getContentPane().add(lblimage, BorderLayout.CENTER);
        frame.setSize(300, 400);
        frame.setVisible(true);

    }

    public void thresholdingPhase() throws IOException {
        // ArrayList<BufferedImage> thresholded = new ArrayList<>();
        for (int i = 0; i < images.size(); i++) {
            BufferedImage origin = null;
            try {
                origin = ImageIO.read(new File(images.get(i)));
            } catch (Exception e) {
                // TODO: handle exception
            }
            BufferedImage th = thresholdImage(origin);
            // thresholded.add(th);
            threshImgs.add(outPath + "threshold_" + i + ".png");
            File outputfile = new File(outPath + "threshold_" + i + ".png");
            ImageIO.write(th, "png", outputfile);
        }
        buildDisplayer(1);
    }

    public static void main(String[] args) throws IOException {
        MyContainer m = new MyContainer();
        m.start();
    }
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.LookupOp;
import java.awt.image.Raster;
import java.awt.image.ShortLookupTable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFImageWriter;

public class MyContainer {
    private JFrame mainFrame;
    private JFrame concDisplay;
    private JFrame threshFrame;
    private JFrame concThresh;
    private ArrayList<String> images;
    private ArrayList<String> threshImgs;
    private JLabel mText;
    private JPanel dragDrop;
    private int sampleWidth = 600;
    private int sampleHeight = 600;
    private int minGrayThresh = 0;
    private int maxGrayThresh = 160;
    private String outPath = "photos\\";

    public void loadImages(String dir) {
        File imgDir = new File(dir);
        File[] imgs = imgDir.listFiles();
        for (int j = 0; j < imgs.length; j++) {
            if (imgs[j].getName().split("_")[0].compareTo("threshold") != 0)
                images.add(imgs[j].getAbsolutePath());
        }
        for (int j = 0; j < images.size(); j++) {
            for (int i = j + 1; i < images.size(); i++) {
                if (images.get(i).compareTo(images.get(j)) < 0) {
                    String temp = images.get(j);
                    images.set(j, images.get(i));
                    images.set(i, temp);
                }
            }
        }
    }

    public void dragNdrop() {
        dragDrop = new JPanel();
        dragDrop.add(mText);
        new FileDrop(dragDrop, new FileDrop.Listener() {
                public void filesDropped(java.io.File[] files) {
                    if (files.length == 1) {
                        pdftoImage(files[0].getAbsolutePath());
                        loadImages(outPath);
                    }
                    buildDisplayer(0);
                } // end filesDropped
            }); // end FileDrop.Listener
    }

    public String directoryChooser() {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("choosertitle");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        String directory = null;
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            directory = chooser.getSelectedFile().getAbsolutePath();
        }
        return directory;
    }

    public void updateImageZoomWindow() {
    }

    public void pdftoImage(String pdfPath) {
        try {
            PDDocument document = PDDocument.load(pdfPath);
            PDFImageWriter piw = new PDFImageWriter();
            piw.writeImage(document, "jpg", "", 1, Integer.MAX_VALUE, outPath
                + "/out");
            document.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void displaySingleImage(String imagePath, int type) {
        String title = "";
        JFrame concDisplay;
        if (type == 0) {
            title = "Single Page Display";
            concDisplay = this.concDisplay;
        } else {
            title = "single Thresholded Display";
            concDisplay = this.concThresh;
        }
        if (concDisplay == null)
            concDisplay = new JFrame(title);
        concDisplay.setVisible(false);
        ImageIcon imgIc = new ImageIcon(imagePath);
        JLabel image = new JLabel(imgIc);
        JScrollPane jsp = new JScrollPane(image);
        concDisplay.add(jsp);
        concDisplay.setSize(imgIc.getIconWidth(), imgIc.getIconHeight());
        concDisplay.setVisible(true);
    }

    public void buildDisplayer(final int type) {
        JFrame main;
        ArrayList<String> images;
        if (type == 0) {
            images = this.images;
            main = mainFrame;
            main.remove(dragDrop);
            dragDrop.setVisible(false);
            main.setVisible(false);
        } else {
            images = this.threshImgs;
            main = threshFrame;
            main = new JFrame("Threshold Displayer");
            main.setLayout(new GridLayout(1, images.size()));
            main.setBackground(new Color(255, 255, 255));
            // mainFrame.setLayout(new FlowLayout());
            main.setSize(800, 600);

        }
        JPanel container = new JPanel();
        for (int i = 0; i < images.size(); i++) {
            ImageIcon myIcon = new ImageIcon(images.get(i));
            Image img = myIcon.getImage();
            BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics g = bi.createGraphics();
            g.drawImage(img, 0, 0, sampleWidth, sampleHeight, null);
            final MLabel j = new MLabel(new ImageIcon(bi), images.get(i));
            j.addMouseListener(new MouseListener() {
                    @Override
                    public void mouseReleased(MouseEvent arg0) {
                    }

                    @Override
                    public void mousePressed(MouseEvent arg0) {
                    }

                    @Override
                    public void mouseExited(MouseEvent arg0) {
                    }

                    @Override
                    public void mouseEntered(MouseEvent arg0) {
                    }

                    @Override
                    public void mouseClicked(MouseEvent arg0) {
                        displaySingleImage(j.imageDir, type);
                    }
                });
            container.add(j);
        }
        JScrollPane jsp = new JScrollPane(container);
        main.add(jsp);
        main.setVisible(true);
        if (type == 0) {
            try {
                thresholdingPhase();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public void prepareGUI() {
        mainFrame = new JFrame("File Browser");
        mainFrame.setLayout(new GridLayout(1, images.size()));
        // mainFrame.setLayout(new FlowLayout());
        mainFrame.setSize(800, 600);
        mText = new JLabel("Drag single pdf file into here", JLabel.CENTER);
        mText.setSize(600, 400);
        dragNdrop();
        mainFrame.add(dragDrop);
        mainFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent windowEvent) {
                    System.exit(0);
                }
            });

        mainFrame.setVisible(true);
    }

    public void start() {
        File dir = new File(outPath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        images = new ArrayList<>();
        threshImgs = new ArrayList<String>();
        prepareGUI();
    }

    public BufferedImage thresholdImage(BufferedImage img) throws IOException {
        int black = 0;
        int white = 0;
        BufferedImage thresh = new BufferedImage(img.getWidth(),
                img.getHeight(), img.getType());
        white = (white | 0x00fffffff);
        for (int y = 0; y < img.getHeight(); y++) {
            for (int x = 0; x < img.getWidth(); x++) {
                int clr = img.getRGB(x, y);
                int red = (clr & 0x00ff0000) >> 16;
                int green = (clr & 0x0000ff00) >> 8;
                int blue = clr & 0x000000ff;
                if (!((red >= minGrayThresh && green >= minGrayThresh && blue >= minGrayThresh) && (red <= maxGrayThresh
                        && green <= maxGrayThresh && blue < maxGrayThresh))) {
                    thresh.setRGB(x, y, black);
                } else {
                    thresh.setRGB(x, y, white);
                }
            }
        }
        return thresh;
    }

    public void testShowImage(BufferedImage img) {
        JFrame frame = new JFrame("Test Show");
        JLabel lblimage = new JLabel(new ImageIcon(img));
        frame.getContentPane().add(lblimage, BorderLayout.CENTER);
        frame.setSize(300, 400);
        frame.setVisible(true);

    }

    public void thresholdingPhase() throws IOException {
        // ArrayList<BufferedImage> thresholded = new ArrayList<>();
        for (int i = 0; i < images.size(); i++) {
            BufferedImage origin = null;
            try {
                origin = ImageIO.read(new File(images.get(i)));
            } catch (Exception e) {
                // TODO: handle exception
            }
            BufferedImage th = thresholdImage(origin);
            // thresholded.add(th);
            threshImgs.add(outPath + "threshold_" + i + ".png");
            File outputfile = new File(outPath + "threshold_" + i + ".png");
            ImageIO.write(th, "png", outputfile);
        }
        buildDisplayer(1);
    }

    public static void main(String[] args) throws IOException {
        MyContainer m = new MyContainer();
        m.start();
    }
}

It's line 152 (new BugfferedImage) that's going wrong - implying tha there's a problem with the ImageIcon/Image that you use to get the width & height.
Try printing the ImageIcon and Image just before 152 so yu can see what's going on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.