java读取本地图片
Ⅰ 怎么在java代码里调用本地图片
写绝对路径或者工程目录下创建个文件,把图片放进去
Ⅱ 用java怎么读取图片
思路:使用 java.awt.Image包下的Image可以接收图片。读取则使用ImageIO对象。
代码如下:
/**
* 读取图片,首先导入以下的包
*/
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;
/**
* 用Image对象来接收图片
* 路径根据实际情况修改
*/
Image image = ImageIO.read(new File("c:\\1.png"));
System.out.println(image.getSource());
Ⅲ java怎么把本地图片以最快的速度读入inputs
首先导入各种需要的包:
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.*;
读取图片的方法如下:
Image[] array = new Image[10];
Image image = ImageIO.read(new File("d:\\source.gif"));//根据你实际情况改文件路径吧
array[0] = image;
图片读出来了。
如果你有一个Image对象,想把它写入文件可以这样做:
BufferedImage image = ImageIO.read(new File("d:\\source.gif"));
//要想保存这个对象的话你要把image声明为BufferedImage 类型
ImageIO.write(image, "png", new File("f:\\test.png"));
Ⅳ java怎么显示本地图片
在面板上搞一个和面板一样大的JLabel
然后,通过JFileChooser获得路径,利用这个图片的路径,构建一个ImageIcon
最后,根据这个ImageIcon去给JLabel对象setIcon(ImageIcon对象);
具体地:
1.panel.add(label,BorderLayout.CENTER);
2.ImageIcon icon = new ImageIcon(url);
3.label.setIcon(icon);
下面的代码你把 .JPG改成BMP试试看,O(∩_∩)O~
package com.shlq.sample;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ImagePane extends JPanel
{
JLabel jl = null;
ImageIcon img = null;
public ImagePane()
{
img = new ImageIcon( "E:\\Picture\\1.jpg ");
jl = new JLabel(img);
this.setLayout(new BorderLayout());
this.add(jl, BorderLayout.CENTER);
}
public static void main(String[] args)
{
JFrame test = new JFrame( "Image Pane ");
test.getContentPane().add(new ImagePane());
test.pack();
test.setVisible(true);
test.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
Ⅳ java如何引用本地图片
引用本地图片的话,就只能是本机访问该网站才回显示出来,即使是你局域网访问你发布的网站,也是不能够显示到图片的。
建议把图片放到webRoot文件夹下面,建一个images文件夹,images文件夹存放你的图片,如1.jpg,在引用的时候,只需要 src='/images/1.jpg'即可。 当然了,src的值可以引用网上的图片路径,这样就避免放到项目中的麻烦
Ⅵ java如何读取文件夹中的图片并在界面显示
下面给你提供一个实现,该实现采用了代理模式。这个实现包含两个文件,分别是Client.java和ImageIcoProxy.java,ImageIcoProxy.java负责了图片的延迟加载,你可以修改为不延迟即可。
Client.java的代码为:
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.Icon;
import javax.swing.JFrame;
public class Client extends JFrame {
private static int IMG_WIDTH = 510;
private static int IMG_HEIGHT = 317;
private Icon imgProxy = null;
public static void main(String[] args) {
Client app = new Client();
app.setVisible(true);
}
public Client() {
super("Virture Proxy Client");
imgProxy = new ImageIcoProxy("D:/test.jpg", IMG_WIDTH, IMG_HEIGHT);
this.setBounds(100, 100, IMG_WIDTH + 10, IMG_HEIGHT + 30);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
Insets insets = getInsets();
imgProxy.paintIcon(this, g, insets.left, insets.top);
}
}
ImageIcoProxy.java的代码为:
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
public class ImageIcoProxy implements Icon {
private ImageIcon realIcon = null;
private String imgName;
private int width;
private int height;
boolean isIconCreated = false;
public ImageIcoProxy(String imgName, int width, int height) {
this.imgName = imgName;
this.width = width;
this.height = height;
}
public int getIconHeight() {
return realIcon.getIconHeight();
}
public int getIconWidth() {
return realIcon.getIconWidth();
}
public void paintIcon(final Component c, Graphics g, int x, int y) {
if (isIconCreated) {
//已经加载了图片,直接显示
realIcon.paintIcon(c, g, x, y);
g.drawString("Just Test", x + 20, y + 370);
} else {
g.drawRect(x, y, width-1, height-1);
g.drawString("Loading photo...", x+20, y+20);
synchronized(this) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Thread.currentThread().sleep(2000);
realIcon = new ImageIcon(imgName);
isIconCreated = true;
} catch (Exception e) {
e.printStackTrace();
}
c.repaint();
}
}
);
}
}
}
}
Ⅶ java的读取本地图片没成功
把你的
image=ImageIO.read(new File("blue-ball.gif"));
改成
image=ImageIO.read(ClassLoader.getSystemResource("blue-ball.gif"));
Ⅷ java web 读取路径中的图片并显示的问题
直接用img标签显示啊,至于路径是相对路径,当然你本地上有那个图片资源填那个绝对路径也没问题。