[英]Add TIF image to Jlist or Frame in java
i am trying to load a .TIF formatted image to add into Jlist and display it into frame. 我正在尝试加载.TIF格式的图像以添加到Jlist并将其显示在框架中。 Here is My code what i tried, here though the image is loaded my frame still looks empty and doesn't showing anything.
这是我尝试的代码,尽管加载了图片,但我的框架仍然看起来空着,没有任何显示。 please correct my code or suggest me that what should be the change i have to make display my .TIF image.
请更正我的代码或建议我,要显示我的.TIF图像,应该进行哪些更改。
import javax.media.jai.PlanarImage;
import com.sun.media.jai.codec.ByteArraySeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.SeekableStream;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.awt.Image;
import java.awt.image.RenderedImage;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
public class ImageViewer {
static Image load(byte[] data) throws Exception {
Image image = null;
SeekableStream stream = new ByteArraySeekableStream(data);
String[] names = ImageCodec.getDecoderNames(stream);
ImageDecoder dec =
ImageCodec.createImageDecoder(names[0], stream, null);
RenderedImage im = dec.decodeAsRenderedImage();
image = PlanarImage.wrapRenderedImage(im).getAsBufferedImage();
return image;
}
public static void main(String[] args) throws Exception {
String path;
DefaultListModel listModel = new DefaultListModel();
JFrame frame = new JFrame("Split Pain");
frame.setSize(700, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
path ="C:/Documents and Settings/Administrator/My Documents/My Pictures/tiff Images/CCITT_1.TIF";
FileInputStream in = new FileInputStream(path);
FileChannel channel = in.getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
Image image = load(buffer.array());
Image imageScaled =image.getScaledInstance(500, -1, Image.SCALE_SMOOTH);
listModel.addElement(new ImageIcon(imageScaled));
final JList list = new JList(listModel);
frame.add(list);
}
}
PlanarImage image = JAI.create("fileload", "C:\\JAVA\\mahes_24_m\\09-11-1988\\mdr16.tiff"); PlanarImage图片= JAI.create(“文件加载”,“ C:\\ JAVA \\ mahes_24_m \\ 09-11-1988 \\ mdr16.tiff”); using this syntax u can load tiff image.
使用此语法,您可以加载tiff图像。 BufferedImage bi = image.getAsBufferedImage();
BufferedImage bi = image.getAsBufferedImage(); add this bi to ListModel and give this ListModel to JList, and add JList to JFrame.
将此bi添加到ListModel并将此ListModel添加到JList,并将JList添加到JFrame。 but first u have to download JAI API to use PlanerImage Class.
但是首先您必须下载JAI API才能使用PlanerImage类。
You need to supply your own ListCellRenderer
implementation to the List: 您需要向列表提供自己的
ListCellRenderer
实现:
class ImageIconCellRenderer extends JLabel implements ListCellRenderer
{
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){
ImageIcon icon = (ImageIcon)value;
setIcon(icon);
return this;
}
}
and then in the main method: 然后在主要方法中:
list.setCellRenderer(new ImageIconCellRenderer());
But I am not sure tiff images are supported by standard Java. 但是我不确定标准Java是否支持tiff图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.