问题
Could anyone suggest me how to store .TIF formatted image to ImageIcon
and add this image to list model? I tried this but gives me java.lang.NullPointerException
.
public static void main(String[] args) throws Exception {
String path = "C:\\project\\aimages";
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
DefaultListModel listModel = new DefaultListModel();
System.out.println("listOfFiles.length="+listOfFiles.length);
int count = 0;
for (int i = 0; i < listOfFiles.length; i++) {
//System.out.println("check path"+listOfFiles[i]);
String name = listOfFiles[i].toString();
System.out.println("name"+name);
// load only JPEGs
if (name.endsWith("jpg") || name.endsWith("JPG")|| name.endsWith("tif") || name.endsWith("TIF")) {
if(name.endsWith("tif") || name.endsWith("TIF"))
{
BufferedImage image = ImageIO.read(listOfFiles[i]);
BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
ImageIcon ii = new ImageIcon(image);
Image img1 = ii.getImage();
Image newimg = img1.getScaledInstance(75, 75, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(img1);
listModel.add(count++, newIcon);
}
else
{
ImageIcon ii = new ImageIcon(ImageIO.read(listOfFiles[i]));
Image img1 = ii.getImage();
Image newimg = img1.getScaledInstance(75, 75, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(newimg);
listModel.add(count++, newIcon);
}
}
}
JList p2 = new JList(listModel);
}
}
here i had edited my code and this is my error msg Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.(ImageIcon.java:228) at ListImage1.main(ListImage1.java:48)
回答1:
If the TIFF is an application resource, probably better to convert it to JPG or PNG.
OTOH, I believe that JAI offers support for reading TIFF.
回答2:
Seems like .TIF
is not supported by ImageIO
. Do have a look at the formats
supported by ImageIO
by using ImageIO.getReaderFormatNames()
, when i did that I got the output as :
C:\Mine\JAVA\J2SE\classes>java TestBorder
jpg
BMP
bmp
JPG
jpeg
wbmp
png
JPEG
PNG
WBMP
GIF
gif
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
at TestBorder.createAndDisplayGUI(TestBorder.java:34)
at TestBorder.access$100(TestBorder.java:6)
at TestBorder$1.run(TestBorder.java:55)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
And this is the program I tried it upon :
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;
public class TestBorder extends JPanel
{
private static TestBorder testBorder;
public TestBorder()
{
}
private static void createAndDisplayGUI()
{
JFrame frame = new JFrame("FRAME");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
testBorder.setBackground(Color.BLUE);
java.net.URL url = testBorder.getClass().getResource("/image/MARBLES.TIF");
BufferedImage image = null;
try
{
image = ImageIO.read(url);
String[] formatNames = ImageIO.getReaderFormatNames();
for (String s: formatNames)
System.out.println(s);
}
catch(Exception e)
{
e.printStackTrace();
}
ImageIcon imageIcon = new ImageIcon(image);
JLabel label = new JLabel(imageIcon);
testBorder.add(label);
frame.add(testBorder, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public Dimension getPreferredSize()
{
return (new Dimension(300, 300));
}
public static void main(String... args)
{
Runnable runnable = new Runnable()
{
public void run()
{
testBorder = new TestBorder();
createAndDisplayGUI();
}
};
SwingUtilities.invokeLater(runnable);
}
}

Moreover have a look at what Java Docs have to say for this. Hopefully you might be able to find something useful there.
回答3:
ImageIcon's API says
public ImageIcon(byte[] imageData)
Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as
GIF
,JPEG
, or (as of 1.3)PNG
. Normally this array is created by reading an image using Class.getResourceAsStream(), but the byte array may also be statically stored in a class. If the resulting image has a "comment" property that is a string, then the string is used as the description of this icon.Parameters: imageData - an array of pixels in an image format supported by the AWT Toolkit, such as GIF, JPEG, or (as of 1.3) PNG See Also: Toolkit.createImage(java.lang.String), getDescription(), Image.getProperty(java.lang.String, java.awt.image.ImageObserver)
there nothing such as support for tiff
or raw
, contents isn't displayable
- common attribute for Icon and ImageIcon, that don't generating any error or exception,
来源:https://stackoverflow.com/questions/9634472/how-to-set-tif-image-to-imageicon-in-java