single band .tif image is distorted when loaded in java - java

When using ImageIO.read() on a .tif image the pixel intensity values are being distorted. I believe this is probably due to it being loaded as an rgb which results in vastly different pixel intensities. Is there any way around this? or any other library that allows the import of single banded .tif images?
the images below demonstrate the issue that is occurring. This is the image, The left image is the correct pixel values loaded through imageJ and check again through loading it in matlab and the right is the result of viewing the image through a method in eclipse (also checked in other ide's though).
JFileChooser chooser = new JFileChooser();
int returnValue = chooser.showOpenDialog(null);
File file = null;
if (returnValue == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(image)));
frame.pack();
frame.setVisible(true);
The intensity values are very important so any help would be appreciated!

Related

How to make Java URI class to stop using a file

I have a small Java desktop application, it reads some image files and displays them. The problem is when I want to execute something (let me say, an exiftool operation) on these files, it denies because Java is still using them.
edit: This happens only on Windows, you can't write on an animated GIF file (converted to a URL object) which is being processed (being displayed) by Java, but on Ubuntu you can edit that file Metadata, system does not think the file is being processed.
Here is that part of my code.
I read the file;
ImageInputStream iis = null;
ImageReader reader = null;
iis = ImageIO.createImageInputStream(
f);
Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis);
reader = (ImageReader) imageReaders.next();
Path pathe = f.toPath();
String mimeType = Files.probeContentType(pathe);
ImageIcon icon = null;
Here is the part I process images, resize them and make JLabel to view them.
The reason why I am not just using Java ImageIO to fill that label with an image, ImageIO can only display the first frame of an animated GIF file. Converting an image to URL this way keep the image animated (even after resized).
ImageIcon icon = null;
Integer labelWidth = this.imageLabel.getWidth();
Integer labelHeight = this.imageLabel.getHeight();
URI img;
img = f.toURI();
URL umg = img.toURL();
icon = new ImageIcon(umg);
//some calculations for setting labelWidth and labelHeight here
icon.setImage(icon.getImage().getScaledInstance(labelWidth, labelHeight,
Image.SCALE_DEFAULT));
At the end, streams are closed.
this.imageLabel.setIcon(icon);
iis.close();
reader.dispose();
And then I try to execute some exiftool commands via a process, it succesfully reads the image Metadata. But when updating the data, it says "Error renaming temporary file to C:/Users/path..." if the image is an animated GIF.
No problems occur if the image is a non animated image, JPEG, PNG or GIF, it can read and update the Metadata, I guess .
When I cancel the image displaying part of the code, I can write on the image Metadata without errors. If I read a JPEG file and display it, still no problems. If I read an animated GIF and display it (animated, does it keep the file connection open?) no modifications on this file can be done, not in my program nor on cmd.exe while the debugging session is not closed. After I quit debugging, exiftool process on cmd.exe starts working normally.
Closing ImageInputStream or ImageReader did not help.
Is there a way to make Java process (if the file is animated, I use URI, URL classes) release the file after read operation? Do these classes I mentioned have methods for releasing, closing, shutting down, kill process etc. I need to read the animated images and display them animated and make update operations on them.
thanks for the comments, here is the problem and the solution.
First of all I removed the unnecessary parts from the code. ImageInputStream and ImageReader were used to check image validation and detect image format (had to use different operations to GIF files) which I do not need anymore.
I still need to use File->URI->URL convertion to display animated GIFs. This is my old code.
URI img = f.toURI();
URL umg = img.toURL();
icon = new ImageIcon(umg);
This code kept connection to file open and blocked other processes editing the image file. (Only animated GIF files, on Windows system)
Here is the new code:
//these 2 lines are same
URI img = f.toURI();
URL umg = img.toURL();
InputStream is = umg.openStream();
byte[] byteChunk = new byte[4096];
int n;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((n = is.read(byteChunk)) > 0)
{
baos.write(byteChunk, 0, n);
}
byte[] dd= baos.toByteArray();
icon = new ImageIcon(dd);
is.close();
With this approach URI(image) is read via stream and ImageIcon is created from image file's byte array not directly from URL, and after that operation InputStream is closed, so the block on that file is released. (If "is.close()" line is not executed, file is still being processed and blocked for other write operations)
Have you tried to load the image with Toolkit.createImage method?
Give this a go:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main {
private static class DrawPanel extends JPanel {
private Image i = null;
public void loadImage(final File f) {
//Most important point: load image via Toolkit.
//I think it must support GIF, JPEG and PNG.
i = getToolkit().createImage(f.getAbsolutePath());
repaint();
}
#Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
if (i != null) {
//g.drawImage(i, 0, 0, this); //Draw full scale image.
g.drawImage(i, 0, 0, getWidth(), getHeight(), this); //Draw scaled/streched image.
//Supplying 'this' in place of ImageObserver argument to the drawImage method is also very important!
}
}
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
final DrawPanel imagePanel = new DrawPanel();
imagePanel.setPreferredSize(new Dimension(500, 350));
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setMultiSelectionEnabled(true);
final JButton load = new JButton("Load");
load.addActionListener(e -> {
if (fileChooser.showOpenDialog(imagePanel) == JFileChooser.APPROVE_OPTION)
imagePanel.loadImage(fileChooser.getSelectedFile());
});
final JPanel contents = new JPanel(new BorderLayout());
contents.add(imagePanel, BorderLayout.CENTER);
contents.add(load, BorderLayout.PAGE_END);
final JFrame frame = new JFrame("Images");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(contents);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}
There are several createImage methods to choose from.
I think they support animated GIFs, and non-animated JPGs and PNGs. I tested only for a GIF, but I have also worked with this in the past for JPGs and PNGs.
I think the animated image is completely loaded into memory (all frames). So you shouldn't have problem modifying the image after reading it.
Get the Toolkit related to the Component which will display the Image. I think also the Toolkit.getDefaultToolkit should do the job under certain circumstances. Then use the drawImage method of the Graphics object to draw the image. There are several drawImage methods to choose from. You can you use for example the one that scales the image on the fly for you (by supplying new width and height along with the draw location). Important: make sure you supply the drawImage with the component which renders it as the ImageObserver (note Component implements ImageObserver).

How to display a jpeg2000 image on a Jframe?

I have a jpeg2000 image, img.jp2 on a file, and on a DataInputStream object imgobj in my project, and want to show that image on a JFrame.
The old version jai_imageio-1.1.jar recommended here and the jj2000 library are both included.
I have tried:
j2kImageReader.setInput(new FileImageInputStream(new File(fileName)));
ImageReadParam imageReadParam =
j2kImageReader.getDefaultReadParam();
imageReadParam.setSourceRegion(new Rectangle(0, 0, 300, 300));
IIOImage image = j2kImageReader.readAll(0, imageReadParam);
// This type of images is difficult to handle,
// I just don't know what to do with IIOImage,
// ImageIcon doesn't accept that type in its constructor.
And this:
Image img = ImageIO.read(new File(fileName));
ImageIcon imgIcon = new ImageIcon(img);
JLabel label = new JLabel(imgIcon);
panel1.add(label);
panel1.repaint();
//Error: Can't read input file!. The panel is still empty
The option included in JMRTD is using two decoders, and no one of them accepts .jp2:
NistDecoder dec=new NistDecoder();
WsqDecoder wdec=new WsqDecoder();
//using the last one, I tried: bitmp= wdec.decode(myDataInputStream);
//but with Error, Invalid pointer : 0!.
So the question is: what is the proper use of jj2000 or jai_imageio to read a jpeg2000 image from a file or a DataInputStream, and if it is possible, to show it on a simple panel on a JFrame?
Thank you for help.
EDIT
AS requested in comments, this is what I did:
//1
FaceImageInfo imgfn = fsinf.getFaceImageInfos().get(0);
BufferedImage bf=ImageIO.read(imgfn.getImageInputStream());
ImageIcon iconImg = new ImageIcon();
iconImg= new ImageIcon(bf);// if this fails try write it to a stream and read it back see //2
JLabel(iconImg, JLabel.CENTER);
//2
ByteArrayOutputStream baos=null;
try{
baos=new ByteArrayOutputStream();
ImageIO.write(bf, "jpg", baos);
}
finally{
try{
baos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
try {
ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
bf2=ImageIO.read(bais) ;
iconImg= new ImageIcon(bf2);
JLabel(iconImg, JLabel.CENTER);
} catch (Exception e) {
e.printStackTrace();
}
Assuming the code otherwise reads the image like you want it, you can easily get a BufferedImage from the ImageReader like this:
try (ImageInputStream input = ImageIO.createImageInputStream(new File(fileName))) {
j2kImageReader.setInput(input));
// Not sure why/if you want to read only the upper left, but I'll leave it as is
ImageReadParam imageReadParam = j2kImageReader.getDefaultReadParam();
imageReadParam.setSourceRegion(new Rectangle(0, 0, 300, 300));
// Use read instead of readAll
BufferedImage image = j2kImageReader.read(0, imageReadParam);
// You can now create an icon and add to a component
Icon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
// Etc...
}

set size of image after retrieving from json

I want to set the size of image get through json service. ???
Here is my code
public void getImage(){
try {
URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=good%20idea%20good%20idea%20water%20jug%20water%20jug%20thirsty%20crow%20thirsty%20crow%20long%20time%20long%20time%20jug%20jug");
URLConnection connection = url.openConnection();
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
builder.append(line);
}
JSONObject json = new JSONObject(builder.toString());
String imageUrl = json.getJSONObject("responseData").getJSONArray("results").getJSONObject(0).getString("url");
BufferedImage image = ImageIO.read(new URL(imageUrl));
ImageIcon img = new ImageIcon(image);
JLabel label = new JLabel("", img, JLabel.CENTER);
jPanel1.add( label, BorderLayout.CENTER );
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Failure", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
I am showing the image in a jPanel.I want to set the size of image so it must be smaller than panel size. How could I achieve this ????
If you don't care about the quality of the result, you could use Image#getScaledInstance, but you should have a read of The Perils of Image.getScaledInstance() of first
Image scaled = image.getScaledInstance(jPanel1.getWidth(), jPanel1.getHeight(), Image.SCALE_SMOOTH);
But this won't maintain the aspect ratio of the image, to do that, you'd have to decide which dimension is more important
int width = jPanel1.getWidth();
int height = jPanel1.getHeight();
if (width < height) {
height = -1;
} else {
width = -1;
}
Image scaled = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
Have a look at Java: maintaining aspect ratio of JPanel background image for more details
Now, if you do care about the quality of the resulting image, then you need to use a different scaling approach, you could use something like a divide and conqure approach, as demonstrated in Quality of Image after resize very low -- Java or use a 3rd party library, like ImgSclr
Now, you next problem is, the size of a component is generally calculated based on the needs of it's children. In this case, that means that jPanel1 may want to use the size of the JLabel to determine how large it should be. Equally, the JLabel will use the size of the image to determine how large it should be. Which puts you in a catch 22 problem.
You could use a JScrollPane which will allow the image to occupy a large area then is available or you will need a dynamic panel which can resize the image automatically based on the available space, but you would still need to make a determination of what the "default" or "preferred" size should be
try this:
resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);

Saving png image with Java using JFileChooser

I am trying to save an image that is formed with a BufferedImage. I get the BufferedImage by doing
(BufferedImage) fg;
fg is an image of my jPanel's graphics. I am successful at saving the image by hardcoding the path in directly as follows:
ImageIO.write((BufferedImage)fg,"png",new File("C:\\Users\\Geiger\\Documents\\test.png"));
But when I attempt to add the JFileChooser to the mix the image that is saved comes up being blank with nothing but the jPanel's background color.
My code for my attempt at utilizing the JFileChooser is as follows:
JFileChooser jfc = new JFileChooser();
int retVal = jfc.showSaveDialog(null);
if(retVal==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();
String test = f.getAbsolutePath();
ImageIO.write((BufferedImage)fg,"png",new File(test));
}
EDIT:To clarify on the issue a little bit more:
The issue is not that a file doesn't appear its that the graphics don't appear on the image when using the JFileChooser object.
I update my image when the JFrame has a mouse presses event:
fg = jPanel2.createImage(jPanel2.getWidth(), jPanel2.getHeight());
try to put this line of code I think that's what you need:
ImageIO.write(buffer, "png", fileDialog.getSelectedFile());
Hope that helps
Graphics2D graphics2D = image.createGraphics();
scribblePane.paint(graphics2D);
Use these two lines of code to add graphics.
That's works for me
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("*.png", "png"));
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
ImageIO.write((BufferedImage) image, "png", new File(file.getAbsolutePath()));
} catch (IOException ex) {
System.out.println("Failed to save image!");
}
} else {
System.out.println("No file choosen!");
}
}

Opencv java - Load image to GUI

I'm developing an application using Java Opencv-2.4.4 and swing GUI. Problem is that I'm unable to find any solution, that shows efficient way how to print processed image (saved in Mat object) to java swing GUI. For this moment I'm using this clumsy solution:
javax.swing.JLabel outputImage;
outputImage.setIcon(new javax.swing.ImageIcon("/home/username/Output.png"));
private void sliderStateChanged(javax.swing.event.ChangeEvent evt) {
.
.
Mat canny; // Here is saved what I want to plot
String filename = "/home/username/Output.png";
Highgui.imwrite(filename, canny); // write to disk
outputImage.setIcon(new ImageIcon(ImageIO.read(new File(filename)))); //update Icon
.
.
}
When user changes some values, inputs etc ., in GUI I have to overwrite Output.png on disk and update jLabel with new image from disk.
Is there any more elegant / efficient solution to this ? Is it posible to plot or convert Mat object directly to Canvas or Image or anything that is printable as image in swing ?
Yes there is more elegant way to do it. You can concert Mat to BufferedImage type and then just Load it with swing. The code to convert it to Buffered image is:
Mat image_tmp = your image
MatOfByte matOfByte = new MatOfByte();
Highgui.imencode(".jpg", image_tmp, matOfByte);
byte[] byteArray = matOfByte.toArray();
BufferedImage bufImage = null;
try {
InputStream in = new ByteArrayInputStream(byteArray);
bufImage = ImageIO.read(in);
} catch (Exception e) {
e.printStackTrace();
}
And then you just can paint it in your GUI object:
g.drawImage(bufImage , 0, 0, null);
where g is of type Graphics
Hope this helps.
jpeg encoding is interesting, but there are a couple problems:
it is not a lossless format, you will lose image data when compressing
it takes quite a while (around 6 to 10 times longer than the suggested one below)
public Image toBufferedImage(Mat m){
int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return image;
}
This is a readymade solution for Imshow() equivalent in Java OpenCV Its simple to use. API will look like:
Imshow im = new Imshow("Title");
im.showImage(matimage);
Visit here https://github.com/master-atul/ImShow-Java-OpenCV
This is a better solution as you don't store the image into disk and read again. Hence it reduces the overhead of reading from a disk and thus is faster.
Using #andriy's anwser. I came up with this solution. I used JFrame instead of Graphics. Hope this helps.
public void imshow(Mat src){
BufferedImage bufImage = null;
try {
MatOfByte matOfByte = new MatOfByte();
Highgui.imencode(".jpg", src, matOfByte);
byte[] byteArray = matOfByte.toArray();
InputStream in = new ByteArrayInputStream(byteArray);
bufImage = ImageIO.read(in);
JFrame frame = new JFrame("Image");
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(bufImage)));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Exception e) {
e.printStackTrace();
}
}

Categories

Resources