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怎么把图片从数据库中调用出来
1 一半图片都是把路径存放在数据库的 到时候取出路径就可以了
2 在数据库有blob格式可以存放图片 以二进制流的方式取出来
<% String zjbm = CheckParam(request.getParameter("zjbm"),""); String zpSql = "select zp from tjjryxxx where sfzh = '"+zjbm+"'"; out.clear(); response.setContentType("image/jpeg"); response.setHeader("Content-Transfer-Encoding","base64"); Connection connection = null; PreparedStatement ps = null; ResultSet rs = null; Blob blob =null; byte[] data = null; try{ connection =getConn(); ps = connection.prepareStatement(zpSql); rs = ps.executeQuery(); while(rs.next()){ blob = (Blob)rs.getBlob("zp"); long nlen = blob.length(); int nsize = (int) nlen; data = blob.getBytes(1,nsize); OutputStream out1 = response.getOutputStream(); BufferedOutputStream bos =null; bos = new BufferedOutputStream(out1); bos.write(data,0,data.length); bos.close(); rs.close(); } }catch(Exception e){ e.printStackTrace(); } %>
❸ 怎样在java里用URL引入图片
读取图片可以有以下两种方法:x0dx0a①:ImageIO.read(new File("这里可以写目录,比如您提到的src/images/某张图片名"));x0dx0a②:new ImageIcon("目录").getImage();x0dx0a这两个方法都返回一个图片对象。可以用一个Image对象接收一下。x0dx0ax0dx0a相对路径是指您所运行的程序的包 所在的文件夹开始的路径。x0dx0a一般来说,上面两种读取方法读取时,是从项目的目录下开始找文件的。x0dx0a所以,您把图片放在src下的images包中,正确的读取方法应该是:x0dx0aImage img=ImageIO.read(new File("src/images/图片名"));或者x0dx0aImage img=new ImageIcon("src/images/图片名").getImage();x0dx0a得到这样一个Image对象后,就可以使用了。
❹ 请教如何用Java语言读取jpg图片,并显示
1、获取文件夹的路径 2、得到文件夹中的有图片的名称,可以存到数组或者集合中 3、你再到jsp页面做显示, 4、下面是获取路径和文件名的代码,前台显示的代码自己写 String path = 文件夹路径; String names = ""; try { File f = new File(path)