贴上代码package com.xiaofeng.readimg;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;public class ReadImg {
public static void main(String[] args){
File img = new File("E:\\大学记忆\\临行\\DSC03884.JPG"); //读入文件
BufferedImage src = null;
try {
src = ImageIO.read(img);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //构造Image对象
String[] name = src.getPropertyNames();
if(name.length > 0){
for(int i =1; i<name.length; i++){
System.out.println(name[i] + ":" + src.getProperty(name[i]));
}
}
int width = src.getWidth(null); //得到源图宽
int height = src.getHeight(null); //得到源图长
System.out.println(width);
System.out.println(height);
}
}
如果不加 String[] name = src.getPropertyNames();
if(name.length > 0){
for(int i =1; i<name.length; i++){
System.out.println(name[i] + ":" + src.getProperty(name[i]));
}
}可以打印长宽,加了就出异常Exception in thread "main" java.lang.NullPointerException
at com.xiaofeng.readimg.ReadImg.main(ReadImg.java:21)
请教各位大侠这是什么原因。
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;public class ReadImg {
public static void main(String[] args){
File img = new File("E:\\大学记忆\\临行\\DSC03884.JPG"); //读入文件
BufferedImage src = null;
try {
src = ImageIO.read(img);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //构造Image对象
String[] name = src.getPropertyNames();
if(name.length > 0){
for(int i =1; i<name.length; i++){
System.out.println(name[i] + ":" + src.getProperty(name[i]));
}
}
int width = src.getWidth(null); //得到源图宽
int height = src.getHeight(null); //得到源图长
System.out.println(width);
System.out.println(height);
}
}
如果不加 String[] name = src.getPropertyNames();
if(name.length > 0){
for(int i =1; i<name.length; i++){
System.out.println(name[i] + ":" + src.getProperty(name[i]));
}
}可以打印长宽,加了就出异常Exception in thread "main" java.lang.NullPointerException
at com.xiaofeng.readimg.ReadImg.main(ReadImg.java:21)
请教各位大侠这是什么原因。
* Returns an array of names recognized by
* {@link #getProperty(String) getProperty(String)}
* or <code>null</code>, if no property names are recognized.
* @return a <code>String</code> array containing all of the property
* names that <code>getProperty(String)</code> recognizes;
* or <code>null</code> if no property names are recognized.
*/
public String[] getPropertyNames() {
return null;
}
点一下,看到源码了吗???
* Returns an array of names recognized by
* {@link #getProperty(String) getProperty(String)}
* or <code>null</code>, if no property names are recognized.
* @return a <code>String</code> array containing all of the property
* names that <code>getProperty(String)</code> recognizes;
* or <code>null</code> if no property names are recognized.
*/
public String[] getPropertyNames() {
return null;
} /**
* Returns the minimum x coordinate of this
* <code>BufferedImage</code>. This is always zero.
* @return the minimum x coordinate of this
* <code>BufferedImage</code>.
*/
public int getMinX() {
return raster.getMinX();
}
import java.io.*;
import javax.swing.*;
import com.drew.imaging.jpeg.*;
import com.drew.metadata.*;
import com.drew.metadata.exif.*;public class ExitInfo {
public static void main(String args[]) throws Exception
{
JFrame frame=new JFrame("显示图片的EXIF(Exchange Image File)信息");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel();
frame.getContentPane().add(panel);
File jpegFile=new File("E:\\photoshop\\Picture\\图片\\周恩来故居\\P1290004.JPG");
System.out.println("下面是【"+jpegFile.getAbsolutePath()+"】的Exif信息");
Metadata metadata=JpegMetadataReader.readMetadata(jpegFile);
Directory exif=metadata.getDirectory(ExifDirectory.class);
String sTitle[]={"制造商","型号","方向","X轴分辨率","Y轴分辨率","图片宽度","图片高度","日期时间","曝光时间","ISO速度等级","焦距","Exif版本"};
String sInfo[][]=new String[12][2];
for(int i=0;i<12;i++)
{
sInfo[i][0]=sTitle[i];
}
sInfo[0][1]=exif.getString(ExifDirectory.TAG_MAKE);
sInfo[1][1]=exif.getString(ExifDirectory.TAG_MODEL);
sInfo[2][1]=exif.getString(ExifDirectory.TAG_ORIENTATION);
sInfo[3][1]=exif.getString(ExifDirectory.TAG_X_RESOLUTION);
sInfo[4][1]=exif.getString(ExifDirectory.TAG_Y_RESOLUTION);
sInfo[5][1]=exif.getString(ExifDirectory.TAG_EXIF_IMAGE_WIDTH);
sInfo[6][1]=exif.getString(ExifDirectory.TAG_EXIF_IMAGE_HEIGHT);
sInfo[7][1]=exif.getString(ExifDirectory.TAG_DATETIME);
sInfo[8][1]=exif.getString(ExifDirectory.TAG_EXPOSURE_TIME);
sInfo[9][1]=exif.getString(ExifDirectory.TAG_ISO_EQUIVALENT);
sInfo[10][1]=exif.getString(ExifDirectory.TAG_FOCAL_LENGTH);
sInfo[11][1]=exif.getString(ExifDirectory.TAG_EXIF_VERSION);
//创建表格
String tableHeader[]={"Exif属性名","Exif属性值"};
JTable table=new JTable(sInfo,tableHeader);
JScrollPane pane=new JScrollPane(table);
panel.add(pane);
frame.setSize(480,260);
frame.setVisible(true);
}
}
自己写的你看看