如何使用 Java 打开 .bmp/.jpeg 图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4340710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hows to open an .bmp/.jpeg image using Java
提问by razshan
I am working on a JFrame/panel that will contain a button. When the user clicks the button, I want an image (which will be stored in the computer hard disk beforehand) to open on the front screen.
我正在处理一个包含按钮的 JFrame/面板。当用户单击按钮时,我希望在前屏幕上打开一个图像(会预先存储在计算机硬盘中)。
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//here i want a code that will somehow open the image from a given directory
}});
Any suggestions on how to go about this ? I have to tell where the image is stored and trigger a virtual 'double click' for the image to pop up on the front screen. Is that even possible using java to synchronize such computer functions?
关于如何解决这个问题的任何建议?我必须告诉图像的存储位置并触发虚拟“双击”以使图像在前屏幕上弹出。甚至可以使用java来同步这些计算机功能吗?
回答by Andreas L.
I don't know a very short way, but I would use something like this (as qick hack to get an impression):
我不知道一个很短的方法,但我会使用这样的东西(作为快速黑客获得印象):
try {
// this is a new frame, where the picture should be shown
final JFrame showPictureFrame = new JFrame("Title");
// we will put the picture into this label
JLabel pictureLabel = new JLabel();
/* The following will read the image */
// you should get your picture-path in another way. e.g. with a JFileChooser
String path = "C:\Users\Public\Pictures\Sample Pictures\Koala.jpg";
URL url = new File(path).toURI().toURL();
BufferedImage img = ImageIO.read(url);
/* until here */
// add the image as ImageIcon to the label
pictureLabel.setIcon(new ImageIcon(img));
// add the label to the frame
showPictureFrame.add(pictureLabel);
// pack everything (does many stuff. e.g. resizes the frame to fit the image)
showPictureFrame.pack();
//this is how you should open a new Frame or Dialog, but only using showPictureFrame.setVisible(true); would also work.
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
showPictureFrame.setVisible(true);
}
});
} catch (IOException ex) {
System.err.println("Some IOException accured (did you set the right path?): ");
System.err.println(ex.getMessage());
}
回答by Ravi Parmar
I think this will work ...
我认为这会奏效...
Code:
代码:
process = new ProcessBuilder("mspaint","yourFileName.jpeg").start();
process = new ProcessBuilder("mspaint","yourFileName.jpeg").start();
This will open your image file with mspaint.....
这将使用 mspaint 打开您的图像文件.....
and also use *Java Advanced Imaging (JAI)*
并使用* Java Advanced Imaging (JAI)*
回答by Neo
Try this code
试试这个代码
try
{
// the line that reads the image file
BufferedImage image;
// work with the image here ...
image = ImageIO.read(new File("C://Users//Neo//Desktop//arduino.jpg"));
jLabel1.setIcon(new ImageIcon(image));
}
catch (IOException e)
{
// log the exception
// re-throw if desired
}
回答by ap91
I'm not sure but try this...
我不确定,但试试这个......
try
{
JLabel picture=new JLabel();
ImageIcon ic=new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("C:\Users\Desktop\xyz.jpg")));
picture.setIcon(ic);
}
catch(Exception)
{
}