Android中读取图片EXIF元数据之metadata-extractor的使用(1)

System.out.println("曝光时间: " + directory.getString(ExifSubIFDDirectory.TAG_EXPOSURE_TIME)+ “秒” );

System.out.println("ISO速度: " + directory.getString(ExifSubIFDDirectory.TAG_ISO_EQUIVALENT) );

System.out.println("焦距: " + directory.getString(ExifSubIFDDirectory.TAG_FOCAL_LENGTH) + “毫米” );

System.out.println("拍照时间: " + directory.getString(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL) );

System.out.println("宽: " + directory.getString(ExifSubIFDDirectory.TAG_EXIF_IMAGE_WIDTH) );

System.out.println("高: " + directory.getString(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT) );

}

if(“ExifIFD0Directory”.equalsIgnoreCase( directory.getClass().getSimpleName() )){

System.out.println("照相机制造商: " + directory.getString(ExifIFD0Directory.TAG_MAKE) );

System.out.println("照相机型号: " + directory.getString(ExifIFD0Directory.TAG_MODEL) );

System.out.println("水平分辨率: " + directory.getString(ExifIFD0Directory.TAG_X_RESOLUTION) );

System.out.println("垂直分辨率: " + directory.getString(ExifIFD0Directory.TAG_Y_RESOLUTION) );

}

}

}

示例3):

File mFilePath=“C://XXX.jpg”;

Metadata metadata = com.drew.imaging.jpeg.JpegMetadataReader.readMetadata(mFilePath);

JpegDirectory jd = (JpegDirectory)metadata.getDirectory(JpegDirectory.class);

System.out.println(“------------” + jd.getImageHeight()); //图片的高

System.out.println(“------------” + jd.getImageWidth()); //图片的宽

//由于只是读取图片的头信息,所以无论多大的图片都能读取,而且速度很快.

从执行的中可以看到照片的详细拍摄时间,拍摄用的相机型号,曝光时间,光圈值,焦距,ISO值 等等。

你也可以直接指定读取其中任意参数的值,ExifDirectory 类中定义了很多以 TAG_ 开头的整数常量,这些常量代表特定的一个参数值,例如要读取相机的型号,可以用下面代码来获取。

Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);

Directory exif = metadata.getDirectory(ExifDirectory.class);

String model = exif.getString(ExifDirectory.TAG_MODEL);

上述提到的是如何获取照片的EXIF信息,其中包含一个很重要的信息就是——拍摄方向。例如所用的图片拍摄方向是:Orientation - Top, left side (Horizontal / normal)。我们在拍照的时候经常会根据场景的不同来选择相机的方向,例如拍摄一颗高树,我们会把相机竖着拍摄,使景物刚好适合整个取景框,但是这样得到的图片如果用普通的图片浏览器看便是倒着的,需要调整角度才能得到一个正常的图像。

通过读取图片的EXIF信息,可以得到关于拍摄方向的这样一个结果:Orientation - Left side, bottom (Rotate 270 CW)

而直接读取 ExitDirectory.TAG_ORIENTATION 标签的值是8。

来看下这个项目是如何来定义这些返回值的,打开源码包中的ExifDescriptor类的getOrientationDescription(),该方法代码如下:

public String getOrientationDescription() throws MetadataException{

if (!_directory.containsTag(ExifDirectory.TAG_ORIENTATION)) return null;

int orientation = _directory.getInt(ExifDirectory.TAG_ORIENTATION);

switch (orientation) {

case 1: return “Top, left side (Horizontal / normal)”;

case 2: return “Top, right side (Mirror horizontal)”;

case 3: return “Bottom, right side (Rotate 180)”;

case 4: return “Bottom, left side (Mirror vertical)”;

case 5: return “Left side, top (Mirror horizontal and rotate 270 CW)”;

case 6: return “Right side, top (Rotate 90 CW)”;

case 7: return “Right side, bottom (Mirror horizontal and rotate 90 CW)”;

case 8: return “Left side, bottom (Rotate 270 CW)”;

default:

return String.valueOf(orientation);

}

}

从这个方法可以清楚看到各个返回值的意思,如此我们便可以根据实际的返回值来对图像进行旋转或者是镜像处理了。

下面给出代码用以旋转图片,其他的关于图片的镜像等处理读者可以依此类推:

String mPath = “D:\XXX.JPG”;

File img = new File(mPath);

BufferedImage old_img = (BufferedImage)ImageIO.read(img);

int w = old_img.getWidth();

int h = old_img.getHeight();

BufferedImage new_img = new BufferedImage(h,w,BufferedImage.TYPE_INT_BGR);

Graphics2D g2d =new_img.createGraphics();

AffineTransform origXform = g2d.getTransform();

AffineTransform newXform = (AffineTransform)(origXform.clone());

// center of rotation is center of the panel

double xRot = w/2.0;

newXform.rotate(Math.toRadians(270.0), xRot, xRot); //旋转270度

g2d.setTransform(newXform);

// draw image centered in panel

g2d.drawImage(old_img, 0, 0, null);

// Reset to Original

g2d.setTransform(origXform);

//写到新的文件

FileOutputStream out = new FileOutputStream(“D:\XXX2.jpg”);

try{

ImageIO.write(new_img, “JPG”, out);

}finally{

out.close();

}

注:利用上面的代码旋转照片后,原有照片包含的EXIF信息就不存在了。关于该问题需要在照片旋转之前先把EXIF信息读出,然后再在旋转后写入新的照片中,可以使用 MediaUtil 包来写EXIF信息到图片文件中,关于这个包的使用可参考最后的链接。

照片的镜面翻转可以直接利用Graphic2D 的 drawImage 方法来实现:

public abstract boolean drawImage(Image img,

int dx1,int dy1,

int dx2,int dy2,

int sx1,int sy1,

int sx2,int sy2,

ImageObserver observer);

三、补充说明

======

解释部分参数的实际含义:

Make 生产者 指产品生产厂家

Model 型号 指设备型号

Orientation  方向 有的相机支持,有的不支持

X Resolution/Y Resolution X/Y方向分辨率 本栏目已有专门条目解释此问题

ResolutionUnit  分辨率单位 一般为PPI

Software  软件 显示固件Firmware版本

DateTime  日期和时间

YCbCrPositioning  色相定位

ExifOffsetExif  信息位置,定义Exif在信息在文件中的写入,有些软件不显示。

ExposureTime 曝光时间 即快门速度

FNumber  光圈系数

ISO speed ratings  感光度

ExifVersionExif  版本

DateTimeOriginal  创建时间

DateTimeDigitized  数字化时间

ComponentsConfiguration  图像构造(多指色彩组合方案)

CompressedBitsPerPixel(BPP)  压缩时每像素色彩位 指压缩程度

ExposureBiasValue  曝光补偿。

MaxApertureValue  最大光圈

MeteringMode  测光方式, 平均式测光、中央重点测光、点测光等。

Lightsource  光源 指白平衡设置

Flash  是否使用闪光灯。

FocalLength  焦距,一般显示镜头物理焦距,有些软件可以定义一个系数,显示相当于35mm相机的焦距 MakerNote(User Comment)作者标记、说明、记录

FlashPixVersionFlashPix  版本 (个别机型支持)

ColorSpace 色域、色彩空间

ExifImageWidth(Pixel X Dimension)  图像宽度 指横向像素数

ExifImageLength(Pixel Y Dimension)  图像高度 指纵向像素数

四、参考资料及资源下载

===========

项目的最新版本及其源码下载:

最后

由于文章篇幅原因,我只把面试题列了出来,详细的答案,我整理成了一份PDF文档,这份文档还包括了还有 高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 ,帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值