㈠ 如何用java讀取屏幕圖象
大概思路就是下面這樣了width=|x1-x2|,high=|y1-y2|
class ScreenCapture
{
/**
* @param args
*/
private Robot robot = null;
private Rectangle scrRect = null;
int x1,y1;
int width,high;
public ScreenCapture()
{
try
{
robot = new Robot();
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
scrRect = new Rectangle(x1, y1, width, height);
}
public BufferedImage captureScreen()
{
BufferedImage bufImg = null;
try
{
bufImg = robot.createScreenCapture(scrRect);
}
catch (Exception e)
{
System.out.println(e.toString());
}
return bufImg;
}
}
㈡ 給定一幅圖像,用java讀取每個像素的RGB三個顏色值
int rgbR;
int rgbG;
int rgbB;
int minx = 0;
int miny = 0;
try {
File file = new File("E:\\dd.png");
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();//圖片寬度
int height = image.getHeight();//圖片高度
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = image.getRGB(i, j); // 下面三行代碼將一個數字轉換為RGB數字
rgbR = (pixel & 0xff0000) >> 16;
rgbG = (pixel & 0xff00) >> 8;
rgbB = (pixel & 0xff);
System.out.println("i=" + i + ",j=" + j + ":(" + rgbR + "," + rgbG + "," + rgbB + ")");
}
}
System.out.println("圖片寬度為:"+width+",高度為:"+height);
} catch (IOException e) {
System.out.println("讀取文件出錯");
e.printStackTrace();
}
㈢ 請教如何用Java語言讀取jpg圖片,並顯示
1、獲取文件夾的路徑 2、得到文件夾中的有圖片的名稱,可以存到數組或者集合中 3、你再到jsp頁面做顯示, 4、下面是獲取路徑和文件名的代碼,前台顯示的代碼自己寫 String path = 文件夾路徑; String names = ""; try { File f = new File(path)
㈣ java 圖片文件的讀取和寫入問題
while(i != -1){
os.write(b, 0, b.length);
i=is.read(b, 0, b.length);
}
關鍵是這里,b僅僅是作為一個緩沖區,是可以反復使用的。
建議不要設置的太小至少1024是比較好的。
㈤ 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從文件中讀取圖片和寫入圖片到文件里
首先導入各種需要的包:
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讀取TIFF圖像方法
Asprise offers TIFF writer and reader library as valued add-on to our flagship procts – Asprise OCR & JTwain. Tagged Image File Format (abbreviated TIFF) is a file format for mainly storing raster images. With Asprise Java TIFF library, you can easily create, manipulate (read and write), disassemble TIFF files easily.
你可以去他們家的網站去download這個API,然後就可以比較簡單的搞定這個TIFF的讀入問題了。
㈧ 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中讀取圖片文件的函數
new File("/place.jpg") 請調試是否正確創建了代表文件的實例