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:
This is my actual image :
This is screen shot of image which i got after loading it :
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;
}`