-2

I want to load a tiff file which is having a huge size. I changed heap size to around 800m so image is loaded but it is not shown properly:

  1. This is my actual image : img1

  2. This is screen shot of image which i got after loading it : img2

This is my code to load the tiff file:

private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser fc = new JFileChooser(Database.basePath);

    FileNameExtensionFilter extensionFilter = new FileNameExtensionFilter("TIF Files", "tif");
    fc.setFileFilter(extensionFilter);
    int res = fc.showOpenDialog(null);
    File file = null;
    // We have an image!
    try {
        if (res == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            System.out.println(file.getAbsolutePath().toString().substring(0,
                    file.getAbsolutePath().toString().lastIndexOf("/")));
            path = file.getAbsolutePath().toString().substring(0,
                    file.getAbsolutePath().toString().lastIndexOf("/"));
            basepath = file.getAbsolutePath().toString().replace(".tif", ".xml");
            System.out.println("path:  " + basepath);
            setTarget(file);

            // System.out.println("canoic"+file.getCanonicalPath());
        } // Oops!

        else {
            JOptionPane.showMessageDialog(null, "You must select one image to be the reference.", "Aborting...",
                    JOptionPane.WARNING_MESSAGE);
        }

    } catch (Exception iOException) {
    }
public void setTarget(File reference) {
    try {
        targetImg = null;
        System.out.println("in try" + reference.getAbsolutePath());
        targetFile = reference;
        byte[] data = Files.readAllBytes(Paths.get(reference.getAbsolutePath()));
        targetImg = rescale(data);

    } catch (IOException ex) {
        System.out.println("in Catch");
        Logger.getLogger(HelloWorld.class.getName()).log(Level.SEVERE, null, ex);
    }

    panel_1.setLayout(new BorderLayout(0, 0));
    label = new JLabel(new ImageIcon(targetImg));
    JScrollPane scroll = new JScrollPane(label);
    panel_1.add(scroll);
    scroll.addMouseListener(this);
    scroll.addMouseMotionListener(this);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout(0, 0));

    setVisible(true);
}
public BufferedImage rescale(byte[] originalImage) {

    TiffDecoder decoder;
    BufferedImage decodedImage = null;
    try {
        decoder = new TiffDecoder(originalImage);
        decodedImage = decoder.read();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return decodedImage;
}`
Sanjeev
  • 9,876
  • 2
  • 22
  • 33

1 Answers1

0

You can read your image using ImageIO#read

BufferedImage image = ImageIO.read(file);

And then set this image to ImageIcon

 label = new JLabel(new ImageIcon(image));

And you will need to put jai_imageio.jar in your classpath

Community
  • 1
  • 1
Sanjeev
  • 9,876
  • 2
  • 22
  • 33