⑴ java 怎樣從磁碟讀取圖片文件
用JFileChoose 這個類,用來選擇文件 主要代碼如下:
JFileChooser f = new JFileChooser(); // 查找文件
f.setFileFilter(new FileNameExtensionFilter("圖片文件(*.bmp, *.gif, *.jpg, *.jpeg, *.png)", "bmp", "gif","jpg", "jpeg", "png"));
int rVal = f.showOpenDialog(null);
⑵ 您好!請問用java怎麼將截取png的圖片中間一部分,以及如何壓縮一個png圖片
getSubimage方法是進行圖片裁剪。
舉例:
public static void main(String[] args) {
try {
//從特定文件載入
BufferedImage bi = ImageIO.read(new File("c:\test.png"));
bi.getSubimage(0, 0, 10, 10);//前兩個值是坐標位置X、Y,後兩個是長和寬
} catch (IOException e) {
e.printStackTrace();
}
}
以下是進行的圖片壓縮,涉及到多個工具類。
/**
* 圖片工具類
* 壓縮圖片大小
* @author Cyw
* @version 1.0
*/
public class ZIPImage {
private File file = null;
private String outPutFilePath;
private String inPutFilePath;
private String inPutFileName;
private boolean autoBuildFileName;
private String outPutFileName;
private int outPutFileWidth = 100; // 默認輸出圖片寬
private int outPutFileHeight = 100; // 默認輸出圖片高
private static boolean isScaleZoom = true; // 是否按比例縮放
public ZIPImage() {
outPutFilePath = "";
inPutFilePath = "";
inPutFileName = "";
autoBuildFileName = true;
outPutFileName = "";
}
/**
*
* @param ipfp
* 源文件夾路徑
* @param ipfn
* 源文件名
* @param opfp
* 目標文件路徑
* @param opfn
* 目標文件名
*/
public ZIPImage(String ipfp, String ipfn, String opfp, String opfn) {
outPutFilePath = opfp;
inPutFilePath = ipfp;
inPutFileName = ipfn;
autoBuildFileName = true;
outPutFileName = opfn;
}
/**
*
* @param ipfp
* 源文件夾路徑
* @param ipfn
* 源文件名
* @param opfp
* 目標文件路徑
* @param opfn
* 目標文件名
* @param aBFN
* 是否自動生成目標文件名
*/
public ZIPImage(String ipfp, String ipfn, String opfp, String opfn,
boolean aBFN) {
outPutFilePath = opfp;
inPutFilePath = ipfp;
inPutFileName = ipfn;
autoBuildFileName = aBFN;
outPutFileName = opfn;
}
public boolean isAutoBuildFileName() {
return autoBuildFileName;
}
public void setAutoBuildFileName(boolean autoBuildFileName) {
this.autoBuildFileName = autoBuildFileName;
}
public String getInPutFilePath() {
return inPutFilePath;
}
public void setInPutFilePath(String inPutFilePath) {
this.inPutFilePath = inPutFilePath;
}
public String getOutPutFileName() {
return outPutFileName;
}
public void setOutPutFileName(String outPutFileName) {
this.outPutFileName = outPutFileName;
}
public String getOutPutFilePath() {
return outPutFilePath;
}
public void setOutPutFilePath(String outPutFilePath) {
this.outPutFilePath = outPutFilePath;
}
public int getOutPutFileHeight() {
return outPutFileHeight;
}
public void setOutPutFileHeight(int outPutFileHeight) {
this.outPutFileHeight = outPutFileHeight;
}
public int getOutPutFileWidth() {
return outPutFileWidth;
}
public void setOutPutFileWidth(int outPutFileWidth) {
this.outPutFileWidth = outPutFileWidth;
}
public boolean isScaleZoom() {
return isScaleZoom;
}
public void setScaleZoom(boolean isScaleZoom) {
this.isScaleZoom = isScaleZoom;
}
public String getInPutFileName() {
return inPutFileName;
}
public void setInPutFileName(String inPutFileName) {
this.inPutFileName = inPutFileName;
}
/**
* 壓縮圖片大小
*
* @return boolean
*/
public boolean compressImage() {
boolean flag = false;
try {
if (inPutFilePath.trim().equals("")) {
throw new NullPointerException("源文件夾路徑不存在。");
}
if (inPutFileName.trim().equals("")) {
throw new NullPointerException("圖片文件路徑不存在。");
}
if (outPutFilePath.trim().equals("")) {
throw new NullPointerException("目標文件夾路徑地址為空。");
} else {
if (!ZIPImage.mddir(outPutFilePath)) {
throw new FileNotFoundException(outPutFilePath
+ " 文件夾創建失敗!");
}
}
if (this.autoBuildFileName) { // 自動生成文件名
String tempFile[] = getFileNameAndExtName(inPutFileName);
outPutFileName = tempFile[0] + "_cyw." + tempFile[1];
compressPic();
} else {
if (outPutFileName.trim().equals("")) {
throw new NullPointerException("目標文件名為空。");
}
compressPic();
}
} catch (Exception e) {
flag = false;
e.printStackTrace();
return flag;
}
return flag;
}
// 圖片處理
private void compressPic() throws Exception {
try {
// 獲得源文件
file = new File(inPutFilePath + inPutFileName);
if (!file.exists()) {
throw new FileNotFoundException(inPutFilePath + inPutFileName
+ " 文件不存在!");
}
Image img = ImageIO.read(file);
// 判斷圖片格式是否正確
if (img.getWidth(null) == -1) {
throw new Exception("文件不可讀!");
} else {
int newWidth;
int newHeight;
// 判斷是否是等比縮放
if (ZIPImage.isScaleZoom == true) {
// 為等比縮放計算輸出的圖片寬度及高度
double rate1 = ((double) img.getWidth(null))
/ (double) outPutFileWidth + 0.1;
double rate2 = ((double) img.getHeight(null))
/ (double) outPutFileHeight + 0.1;
// 根據縮放比率大的進行縮放控制
double rate = rate1 > rate2 ? rate1 : rate2;
newWidth = (int) (((double) img.getWidth(null)) / rate);
newHeight = (int) (((double) img.getHeight(null)) / rate);
} else {
newWidth = outPutFileWidth; // 輸出的圖片寬度
newHeight = outPutFileHeight; // 輸出的圖片高度
}
BufferedImage tag = new BufferedImage((int) newWidth,
(int) newHeight, BufferedImage.TYPE_INT_RGB);
/*
* Image.SCALE_SMOOTH 的縮略演算法 生成縮略圖片的平滑度的 優先順序比速度高 生成的圖片質量比較好 但速度慢
*/
tag.getGraphics().drawImage(
img.getScaledInstance(newWidth, newHeight,
Image.SCALE_SMOOTH), 0, 0, null);
FileOutputStream out = new FileOutputStream(outPutFilePath
+ outPutFileName);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 創建文件夾目錄
*
* @param filePath
* @return
* @throws Exception
*/
@SuppressWarnings("unused")
private static boolean mddir(String filePath) throws Exception {
boolean flag = false;
File f = new File(filePath);
if (!f.exists()) {
flag = f.mkdirs();
} else {
flag = true;
}
return flag;
}
/**
* 獲得文件名和擴展名
*
* @param fullFileName
* @return
* @throws Exception
*/
private String[] getFileNameAndExtName(String fullFileName)
throws Exception {
String[] fileNames = new String[2];
if (fullFileName.indexOf(".") == -1) {
throw new Exception("源文件名不正確!");
} else {
fileNames[0] = fullFileName.substring(0, fullFileName
.lastIndexOf("."));
fileNames[1] = fullFileName
.substring(fullFileName.lastIndexOf(".") + 1);
}
return fileNames;
}
public Image getSourceImage() throws IOException{
//獲得源文件
file = new File(inPutFilePath + inPutFileName);
if (!file.exists()) {
throw new FileNotFoundException(inPutFilePath + inPutFileName
+ " 文件不存在!");
}
Image img = ImageIO.read(file);
return img;
}
/*
* 獲得圖片大小
* @path :圖片路徑
*/
public long getPicSize(String path) {
File file = new File(path);
return file.length();
}
}
//下面是測試程序
package com.sun.util.cyw;
import java.awt.Image;
import java.io.IOException;
public class ImageTest {
public static void main(String[] args) throws IOException {
ZIPImage zip=new ZIPImage("d:\","1.jpg","d:\test\","處理後的圖片.jpg",false);
zip.setOutPutFileWidth(1000);
zip.setOutPutFileHeight(1000);
Image image=zip.getSourceImage();
long size=zip.getPicSize("d:\1.jpg");
System.out.println("處理前的圖片大小 width:"+image.getWidth(null));
System.out.println("處理前的圖片大小 height:"+image.getHeight(null));
System.out.println("處理前的圖片容量:"+ size +" bit");
zip.compressImage();
}
}
⑶ java io 可以讀取什麼格式的文件
javaio可以讀取任意格式的文件,包括txt、ini、xml等文本文件,也可讀取png、MP3等各種二進制文件格式。
因為javaio讀取時是以位元組流(btyestream)的形式,將文件存儲在硬碟上的位元組一個個/一段段讀入的。讀入後的位元組流可進一步處理,比如文本的可以用顯示在某個界面上,MP3可以使用JMF(java多媒體框架)進行播放。
importjava.io.BufferedInputStream;
importjava.io.FileInputStream;
importjavazoom.jl.player.Player;
publicstaticvoidmain(String[]args)
{
try{//播放MP3文件流
Stringmp3file="神武雨霖鈴";
FileInputStreamfin=newFileInputStream(mp3file);//建立文件流
BufferedInputStreambuffer=newBufferedInputStream(fin);
player=newPlayer(buffer);
player.play();
}catch(Exceptione){
e.printStackTrace();
}
}
⑷ 用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());
⑸ 200分c/c++/java/c#讀取png圖片
沒必要那麼麻煩.只要使用GDI+庫裡面的Bitmap對象和Graphics對象就可以了。WindowsXP以上的OS都提供GDI+圖形介面了,他的功能比GDI介面更強大,使用更方便。建議你可以查查GDI+的用法。這里給你個最簡單的C#的例子:
System.Drawing.Bitmap bmp = new Bitmap("1.png");//創建Bitmap對象
System.Drawing.Color c = bmp.GetPixel(0, 0);//獲得圖像上(0,0)點的像素值
int a = c.A;//該像素的Alpha通道值
int r = c.R;//該像素的紅色通道值
int g = c.G;//該像素的綠色通道值
int b = c.B;//該像素的藍色通道
那建議你上網查一查PNG格式的標准,就知道PNG文件里的數據排列了。但PNG是壓縮過的,所以你還得有解壓演算法才行。
png的存儲格式:
關鍵數據塊中有4個標准數據塊:
文件頭數據塊IHDR(header chunk):包含有圖像基本信息,作為第一個數據塊出現並只出現一次。
調色板數據塊PLTE(palette chunk):必須放在圖像數據塊之前。
圖像數據塊IDAT(image data chunk):存儲實際圖像數據。PNG數據允許包含多個連續的圖像數據塊。
圖像結束數據IEND(image trailer chunk):放在文件尾部,表示PNG數據流結束。
在第二個數據塊中包含了調色板數據塊。可是,當我們去解析png24時,卻未找到調色板、並且我們發現png24的存儲模式是點陣顏色值加一位的阿爾法通道值構成的,這種存儲模式根本不需要調色板的存在。基於這種存儲模式,png24的位深最低是32位真彩,在我們看到的圖像過渡中會很圓潤,因為每個點都可以是不同的色彩以及不同的透明值。而這種模式也是我們最常使用、大家所理解中的png模式。至於"png"後面的「24」可見也和位深並無關系,至於為什麼叫24,我也沒有找到具體的答案。
png24源數據中無調色盤的存在,而在標准數據塊的第二塊中,卻顯示了調色板數據塊。即然存在,肯定是有意義的,可見png有另外一種存儲模式--帶色盤的png8模式。png8有點類似於GIF,包含了一個調色板,並在調色板上有一個透明顏色值,這種模式在計算機的存儲中,每個點陣存儲的是色盤索引、並且無阿爾法半透明位。所以,png8在顏色位深上,可以低於32位;也可以使用更換色盤的技術來處理一些獨特的效果;但是由於每個點陣沒有阿爾法定義,邊緣會像GIF一樣存在鋸齒現像。
好像講的有點亂,總結一下區別吧:
png8和png24的根本區別,不是顏色位的區別,而是存儲方式不同;
png8 色盤索引、調色板中一位透明值、不支持阿爾法通道的半透明,存儲格式中每個像素無透明度的數據塊定義;
png24 無調色板、支持阿爾法通道的半透明、每個點陣都有透明度的定義,最低32位真彩色;
特性
支持256色調色板技術以產生小體積文件
最高支持48位真彩色圖像以及16位灰度圖像。
支持阿爾法通道的半透明特性。
支持圖像亮度的gamma校正信息。
支持存儲附加文本信息,以保留圖像名稱、作者、版權、創作時間、注釋等信息。
使用無損壓縮
漸近顯示和流式讀寫,適合在網路傳輸中快速顯示預覽效果後再展示全貌。
使用CRC循環冗餘編碼防止文件出錯。
最新的PNG標准允許在一個文件內存儲多幅圖像。
看。有使用無損壓縮和多幅圖像。挺復雜的哦!
http://codex.wordpress.org.cn/index.php?diff=prev&oldid=88484
看下面W3C的網站介紹。你就知道有多復雜了。不用庫函數,我覺得你的想法太不現實。對與BMP這樣格式還可以,對於PNG,不行。
http://www.w3.org/TR/2003/REC-PNG-20031110/
⑹ Java圖像讀取輸出
如果用Swing有那麼麻煩嗎,draw一下不就得了.
你的圖像x.png的長40, 寬40, 那你就drawImage(a.png,0,0,20,20,null),drawImage(b.png,0,20,20,20,null),
drawImage(c.png,20,0,20,20,null),drawImage(d.png,20,20,20,20,null),
⑺ Java如何讀取文件夾中所有圖片,並顯示出來
說一下思路吧,首先遍歷文件夾,找到對應後綴的文件(png,jpg之類的),然後創建Bitmap對象,使用inputStream將文件轉成bitmap對象,之後使用imageview或者GLview顯示圖片即可。
注意對大圖進行壓縮,結束時圖片必須回收處理,bitmap.recycle()否則圖片多了內存溢出
⑻ java怎樣獲取apk文件icon圖標
java獲取apk文件icon圖標的方法步驟如下:
1、解壓apk,apk實際上是zip壓縮檔。
2、解析androidManifest.xml文件,解析application節點,獲取android:icon屬性,得到圖標資源文件名。
3、圖標資源大多數位於/drawable-hdpi目錄下。
4、嘗試讀取png或者jpg格式,如果還讀取不到,那就按照沒有圖標處理。
⑼ c/c++/java/c#讀取png圖片
給你一個java的,自己改改
import java.awt.*;
import java.awt.image.*;
import java.util.Random;
import java.io.*;
import javax.swing.*;
public class LoadImage {
/**
* @param args
*/
public static void main(String[] args) {
String myreadline = "";
//定義一個String類型的變數,用來每次讀取一行
try {
FileReader fr = new FileReader("data/imagelist.txt");//創建FileReader對象,用來讀取字元流
BufferedReader br = new BufferedReader(fr); //緩沖指定文件的輸入
FileWriter fw = new FileWriter("data/17d_result.txt");//創建FileWriter對象,用來寫入字元流
BufferedWriter bw = new BufferedWriter(fw); //將緩沖對文件的輸出
while (br.ready()) {
myreadline = br.readLine();//讀取一行
BufferedImage image = toBufferedImage(new ImageIcon("data/Image/"+myreadline).getImage());
int height = image.getHeight();
int width = image.getWidth();
int r1=0, r2=0, r3=0, r4=0, r5=0, r6=0, r7=0, r8=0, r9=0, r10=0, r11=0, r12=0, r13=0, r14=0, r15=0, r16=0, r17=0;
int g1=0, g2=0, g3=0, g4=0, g5=0, g6=0, g7=0, g8=0, g9=0, g10=0, g11=0, g12=0, g13=0, g14=0, g15=0, g16=0, g17=0;
int b1=0, b2=0, b3=0, b4=0, b5=0, b6=0, b7=0, b8=0, b9=0, b10=0, b11=0, b12=0, b13=0, b14=0, b15=0, b16=0, b17=0;
int rgb1=0, rgb2=0, rgb3=0, rgb4=0, rgb5=0, rgb6=0, rgb7=0, rgb8=0, rgb9=0, rgb10=0, rgb11=0, rgb12=0, rgb13=0, rgb14=0, rgb15=0, rgb16=0, rgb17=0;
//System.out.println("Height=" + height + ", Width=" + width);
//Random ran = new Random();
//int x = ran.nextInt(width), y = ran.nextInt(height);
for (int y=0;y<height;y++) {
for (int x=0;x<width;x++) {
Color color = new Color(image.getRGB(x, y));
if(color.getRed()<=15)
r1++;
if(color.getRed()>15 && color.getRed()<=30)
r2++;
if(color.getRed()>30 && color.getRed()<=45)
r3++;
if(color.getRed()>45 && color.getRed()<=60)
r4++;
if(color.getRed()>60 && color.getRed()<=75)
r5++;
if(color.getRed()>75 && color.getRed()<=90)
r6++;
if(color.getRed()>90 && color.getRed()<=105)
r7++;
if(color.getRed()>105 && color.getRed()<=120)
r8++;
if(color.getRed()>120 && color.getRed()<=135)
r9++;
if(color.getRed()>135 && color.getRed()<=150)
r10++;
if(color.getRed()>150 && color.getRed()<=165)
r11++;
if(color.getRed()>165 && color.getRed()<=180)
r12++;
if(color.getRed()>180 && color.getRed()<=195)
r13++;
if(color.getRed()>195 && color.getRed()<=210)
r14++;
if(color.getRed()>210 && color.getRed()<=225)
r15++;
if(color.getRed()>225 && color.getRed()<=240)
r16++;
if(color.getRed()>240 && color.getRed()<=255)
r17++;
if(color.getGreen()<=15)
g1++;
if(color.getGreen()>15 && color.getGreen()<=30)
g2++;
if(color.getGreen()>30 && color.getGreen()<=45)
g3++;
if(color.getGreen()>45 && color.getGreen()<=60)
g4++;
if(color.getGreen()>60 && color.getGreen()<=75)
g5++;
if(color.getGreen()>75 && color.getGreen()<=90)
g6++;
if(color.getGreen()>90 && color.getGreen()<=105)
g7++;
if(color.getGreen()>105 && color.getGreen()<=120)
g8++;
if(color.getGreen()>120 && color.getGreen()<=135)
g9++;
if(color.getGreen()>135 && color.getGreen()<=150)
g10++;
if(color.getGreen()>150 && color.getGreen()<=165)
g11++;
if(color.getGreen()>165 && color.getGreen()<=180)
g12++;
if(color.getGreen()>180 && color.getGreen()<=195)
g13++;
if(color.getGreen()>195 && color.getGreen()<=210)
g14++;
if(color.getGreen()>210 && color.getGreen()<=225)
g15++;
if(color.getGreen()>225 && color.getGreen()<=240)
g16++;
if(color.getGreen()>240 && color.getGreen()<=255)
g17++;
if(color.getBlue()<=15)
b1++;
if(color.getBlue()>15 && color.getBlue()<=30)
b2++;
if(color.getBlue()>30 && color.getBlue()<=45)
b3++;
if(color.getBlue()>45 && color.getBlue()<=60)
b4++;
if(color.getBlue()>60 && color.getBlue()<=75)
b5++;
if(color.getBlue()>75 && color.getBlue()<=90)
b6++;
if(color.getBlue()>90 && color.getBlue()<=105)
b7++;
if(color.getBlue()>105 && color.getBlue()<=120)
b8++;
if(color.getBlue()>120 && color.getBlue()<=135)
b9++;
if(color.getBlue()>135 && color.getBlue()<=150)
b10++;
if(color.getBlue()>150 && color.getBlue()<=165)
b11++;
if(color.getBlue()>165 && color.getBlue()<=180)
b12++;
if(color.getBlue()>180 && color.getBlue()<=195)
b13++;
if(color.getBlue()>195 && color.getBlue()<=210)
b14++;
if(color.getBlue()>210 && color.getBlue()<=225)
b15++;
if(color.getBlue()>225 && color.getBlue()<=240)
b16++;
if(color.getBlue()>240 && color.getBlue()<=255)
b17++;
}
}
rgb1 = r1 + g1 + b1;
rgb2 = r2 + g2 + b2;
rgb3 = r3 + g3 + b3;
rgb4 = r4 + g4 + b4;
rgb5 = r5 + g5 + b5;
rgb6 = r6 + g6 + b6;
rgb7 = r7 + g7 + b7;
rgb8 = r8 + g8 + b8;
rgb9 = r9 + g9 + b9;
rgb10 = r10 + g10 + b10;
rgb11 = r11 + g11 + b11;
rgb12 = r12 + g12 + b12;
rgb13 = r13 + g13 + b13;
rgb14 = r14 + g14 + b14;
rgb15 = r15 + g15 + b15;
rgb16 = r16 + g16 + b16;
rgb17 = r17 + g17 + b17;
//System.out.println("rect " + rgb1 + " " + rgb2 + " " + rgb3);
bw.write("rect " + rgb1 + " " + rgb2 + " " + rgb3 + " " + rgb4 + " " + rgb5 + " " + rgb6 + " " + rgb7 + " " + rgb8 + " " + rgb9 + " " + rgb10 + " " + rgb11 + " " + rgb12 + " " + rgb13 + " " + rgb14 + " " + rgb15 + " " + rgb16 + " " + rgb17); //寫入文件
bw.newLine();
//System.out.println(myreadline);//在屏幕上輸出
}
bw.flush(); //刷新該流的緩沖
bw.close();
br.close();
fw.close();
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// Determine if the image has transparent pixels; for this method's
// implementation, see e661 Determining If an Image Has Transparent
// Pixels
boolean hasAlpha = hasAlpha(image);
// Create a buffered image with a format that's compatible with the
// screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha) {
transparency = Transparency.BITMASK;
}
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image
.getHeight(null), transparency);
} catch (HeadlessException e) {
// The system does not have a screen
}
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha) {
type = BufferedImage.TYPE_INT_ARGB;
}
bimage = new BufferedImage(image.getWidth(null), image
.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
// This method returns true if the specified image has transparent pixels
public static boolean hasAlpha(Image image) {
// If buffered image, the color model is readily available
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage) image;
return bimage.getColorModel().hasAlpha();
}
// Use a pixel grabber to retrieve the image's color model;
// grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}
// Get the image's color model
ColorModel cm = pg.getColorModel();
return cm.hasAlpha();
}
}