「java读入图面」JAVA读取图片
本篇文章给大家谈谈java读入图面,以及JAVA读取图片对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java如何读取文件夹中的图片并在界面显示
- 2、Java怎么读取图中的数据,而且不乱码?希望附带代码
- 3、java从GUI中读入图像并显示,求解答
- 4、java读入图像
- 5、安卓java中读取图片应该怎么写
- 6、JAVA 读取 数据库中的图片显示到页面
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怎么读取图中的数据,而且不乱码?希望附带代码
不知道你的文件格式,不过你可以可以尝试用io流来读取。下面代码 我试过是可以读取挺多格式文件的,你试下 拷贝过去改下文件路径就行了。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadFile
{
public static void main(String[] args)
{
//文件位置
String filepath = "D:\\test.pub";
/** 一次读取所有内容 */
FileInputStreamReadFile(filepath);
System.out.println("=====================");
/** 以行为单位读取文件,常用于读面向行的格式化文件 */
BufferedReaderReadFile(filepath);
System.out.println("=====================");
/** 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 */
ReadFileByByte(filepath);
System.out.println("\n=====================");
/** 以行为单位读取文件,常用于读面向行的格式化文件 */
InputSteamReaderReadFile(filepath);
System.out.println("\n=====================");
}
private static void InputSteamReaderReadFile(String filepath)
{
try
{
InputStreamReader sr = new InputStreamReader(new FileInputStream(new File(filepath)));
int temp = 0;
while ((temp = sr.read()) != -1)
{
System.out.print((char)temp);
}
sr.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static void ReadFileByByte(String filepath)
{
try
{
File file = new File(filepath);
FileInputStream fis = new FileInputStream(file);
int b = 0;
while ((b = fis.read()) != -1)
{
System.out.print((char)b);
}
fis.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static void BufferedReaderReadFile(String filepath)
{
try
{
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader(new File(filepath)));
String readLine = "";
while ((readLine = br.readLine()) != null)
{
sb.append(readLine + "\n");
}
br.close();
System.out.print(sb.toString());
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private static void FileInputStreamReadFile(String filepath)
{
try
{
File file = new File(filepath);
FileInputStream fis = new FileInputStream(file);
long filelength = file.length();
byte[] bb = new byte[(int)filelength];
fis.read(bb);
fis.close();
System.out.println(new String(bb));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
不知道你的文件格式,不过你可以可以尝试用io流来读取。下面代码 我试过是可以读取挺多格式文件的,你试下。
java从GUI中读入图像并显示,求解答
fileChooser1.showOpenDialog(null)
得到的路径、得到Image,在JFrame的paint事件中,绘上去
java读入图像
可以用ImageIO从文件里读,比如读一张ref.png图:
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
int[][] rgb;
try {
File png_ref = new File("ref.png");
BufferedImage image_ref = ImageIO.read(png_ref);
int columns = image_ref.getWidth();
int rows = image_ref.getHeight();
rgb = new int[rows][columns];
for (int row=0; rowrows; row++){
for (int col=0; colcolumns; col++){
rgb[row][col] = image_ref.getRGB(col, row);
}
}
}
} catch (IOException e) {
correct = false;
}
安卓java中读取图片应该怎么写
核心代码
boolean isSdCardExist = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);// 判断sdcard是否存在
if (isSdCardExist) {
String sdpath = Environment.getExternalStorageDirectory()
.getAbsolutePath();// 获取sdcard的根路径
textView1.setText("sd卡是存在的。以下是sdcard下的img25.jpg!");
String filepath = sdpath + File.separator + "img25.jpg";
File file = new File(filepath);
ImageView imageView = new ImageView(this);//创建一个imageView对象
if (file.exists()) {
Bitmap bm = BitmapFactory.decodeFile(filepath);
// 将图片显示到ImageView中
imageView.setImageBitmap(bm);
linearLayout1.addView(imageView);
}
} else {
textView1.setText("sd卡不存在!");
}
JAVA 读取 数据库中的图片显示到页面
其实在大多数的实际项目中 数据库中存放的是 图片的url地址 直接将图片放置到你的项目中的一个专门的文件夹先 在页面显示的时候直接按照 url来显示图片 这样既方便由能够缓解服务器负担增加性能 你可以尝试一下
img src="你从数据库中获得的图片的url"
想必读取数据库你应该会把
关于java读入图面和JAVA读取图片的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。