⑴ 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();
}
}