导航:首页 > 文件目录 > 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读取图片文件相关的资料

热点内容
华为视屏在哪个文件夹里 浏览:172
拍画质好的视频该有什么app 浏览:845
ai教程书籍 浏览:3
凌渡carlife数据线连哪里 浏览:7
app医学里是什么意思 浏览:530
网站设置微信在线客服怎么弄 浏览:51
win10qq发语音有杂音 浏览:613
最右app缓存在哪个文件夹 浏览:157
安卓bochs教程 浏览:831
地下室外墙计算工具 浏览:805
数控铣床钻5个孔怎么编程 浏览:66
买美白产品去哪个app 浏览:543
word文件如何附文件 浏览:139
编程猫是哪个省份的 浏览:927
开始学编程从什么开始 浏览:703
数据怎么退 浏览:774
iphone6plusqq邮箱 浏览:337
小米自带文件管理找不到文件 浏览:40
win7增强工具 浏览:733
网络应该实名制吗 浏览:456

友情链接