當前位置:首頁 » 編程語言 » java圖片讀取

java圖片讀取

發布時間: 2025-05-24 09:07:43

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)

熱點內容
軟體u盤加密 發布:2025-05-24 13:47:13 瀏覽:830
ftp的進程是哪個 發布:2025-05-24 13:38:47 瀏覽:161
ftp伺服器破解 發布:2025-05-24 13:20:52 瀏覽:665
如何自建郵件伺服器 發布:2025-05-24 13:16:26 瀏覽:959
魔獸電腦配置低怎麼不卡 發布:2025-05-24 13:13:19 瀏覽:483
phpimagick安裝 發布:2025-05-24 13:11:41 瀏覽:837
rhel6ftp 發布:2025-05-24 13:09:15 瀏覽:668
二次存儲器 發布:2025-05-24 13:02:23 瀏覽:368
萬能鑰匙的wifi密碼在哪裡 發布:2025-05-24 12:42:00 瀏覽:226
htcandroidphone 發布:2025-05-24 12:28:48 瀏覽:717