導航:首頁 > 文件目錄 > java讀取圖片文件

java讀取圖片文件

發布時間:2025-02-10 08:51:26

java 怎麼一次性讀取多張圖片

實現思路:一次性讀取選中的統一路徑下的多張圖片,實現整體復制。
//復制文件
package com.cdd.util;
import java.io.*;
public class FileUtil {
private static void (File[] files, File d) {
if (!d.exists()) //如果指定目錄不存在
d.mkdir(); //創建目錄
for (int i = 0; i < files.length; i++) { //循環遍歷要復制的文件夾
if (files[i].isFile()) { //如果文件夾中是文件
try {
FileInputStream fis = new FileInputStream(files[i]); //創建FileInputStream對象
FileOutputStream out = new FileOutputStream(new File(d
.getPath()
+ File.separator + files[i].getName())); //復制後文件的保存路徑
int count = fis.available();
byte[] data = new byte[count];
while ((str = bre.readLine())!= null) //讀取文件通過readline方法可以有效的避免亂碼
out.write(str ); //將讀取的信息寫入文件中
}
out.close(); //關閉流
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (files[i].isDirectory()) { //如果文件夾中是一個路徑
File des = new File(d.getPath() + File.separator
+ files[i].getName()); //在復制後路徑中創建子文件夾
des.mkdir();
(files[i].listFiles(), des); //再次調用本方法
}
}
System.out.println("文件夾復製成功");
}

⑵ 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讀取圖片文件相關的資料

熱點內容
fx3u哪個編程軟體 瀏覽:841
u盤的文件夾在手機哪裡 瀏覽:881
引用文件內容的格式及範文 瀏覽:881
編程圖元屬性怎麼修改 瀏覽:828
word文檔的大括弧怎麼輸入 瀏覽:862
關於旅遊收入的數據從哪裡找 瀏覽:989
gson解析json集合問題 瀏覽:680
安卓ipsec標識符填什麼 瀏覽:215
在哪裡解壓文件第一分卷 瀏覽:63
奧維使用教程 瀏覽:324
編程程序怎麼轉到plc上 瀏覽:807
文件名沖突但是找不到 瀏覽:261
上海瑞金醫院app下載 瀏覽:998
qq群里的機器人買武器 瀏覽:428
捕魚達人歷史版本 瀏覽:73
mp4視頻文件解密軟體 瀏覽:62
多軸編程哪個軟體最方便 瀏覽:27
老平板哪個是顯示屏數據線插座 瀏覽:849
5sing上傳音頻文件格式 瀏覽:171
win10輸入文件滑鼠右鍵異常 瀏覽:634

友情鏈接