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

java讀取本地圖片

發布時間: 2022-05-16 00:03:11

Ⅰ 怎麼在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標簽顯示啊,至於路徑是相對路徑,當然你本地上有那個圖片資源填那個絕對路徑也沒問題。

熱點內容
php56mssql 發布:2024-03-29 23:49:10 瀏覽:611
dns伺服器地址改為 發布:2024-03-29 23:42:04 瀏覽:97
trimsql 發布:2024-03-29 23:29:13 瀏覽:691
編程自學書 發布:2024-03-29 23:29:08 瀏覽:377
qq密碼在qq哪裡能看見 發布:2024-03-29 23:29:06 瀏覽:214
楚雄琪豐酒店wifi密碼多少 發布:2024-03-29 23:20:10 瀏覽:509
自招編程題 發布:2024-03-29 23:19:31 瀏覽:192
蘋果端的什麼游戲與安卓數據互通 發布:2024-03-29 23:18:23 瀏覽:695
androidwear表盤 發布:2024-03-29 23:09:46 瀏覽:833
19萬的紅旗有哪些配置 發布:2024-03-29 23:09:44 瀏覽:985