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)