拍摄设备的型号便是EXIF信息中的其中一个。下面我们给出一段代码将这个图片的所有的EXIF信息全部打印出来。
package com.liusoft.dlog4j.test; import java.io.File; import java.util.Iterator; import com.drew.imaging.jpeg.JpegMetadataReader; import com.drew.metadata.Directory; import com.drew.metadata.Metadata; import com.drew.metadata.Tag; import com.drew.metadata.exif.ExifDirectory; /** * 测试用于读取图片的EXIF信息 * @author Winter Lau */ public class ExifTester { public static void main(String[] args) throws Exception { File jpegFile = new File("D:\\我的文档\\我的相册\\DSCF1749.JPG"); Metadata metadata = JpegMetadataReader.readMetadata(jpegFile); Directory exif = metadata.getDirectory(ExifDirectory.class); Iterator tags = exif.getTagIterator(); while (tags.hasNext()) { Tag tag = (Tag)tags.next(); System.out.println(tag); } } }把metadata-extractor-2.3.0.jar文件加入到类路径中编译并执行上面这段代码后可得到下面的运行结果:
[Exif] Make - FUJIFILM [Exif] Model - FinePix A205S [Exif] Orientation - Top, left side (Horizontal / normal) [Exif] X Resolution - 72 dots per inch [Exif] Y Resolution - 72 dots per inch [Exif] Resolution Unit - Inch [Exif] Software - Digital Camera FinePix A205S Ver1.00 [Exif] Date/Time - 2005:05:13 22:18:49 [Exif] YCbCr Positioning - Datum point [Exif] Copyright - [Exif] Exposure Time - 1/60 sec [Exif] F-Number - F3 [Exif] Exposure Program - Program normal [Exif] ISO Speed Ratings - 320 [Exif] Exif Version - 2.20 [Exif] Date/Time Original - 2005:05:13 22:18:49 [Exif] Date/Time Digitized - 2005:05:13 22:18:49 [Exif] Components Configuration - YCbCr [Exif] Compressed Bits Per Pixel - 3 bits/pixel [Exif] Shutter Speed Value - 1/63 sec [Exif] Aperture Value - F3 [Exif] Brightness Value - -61/100 [Exif] Exposure Bias Value - 0 EV [Exif] Max Aperture Value - F3 [Exif] Metering Mode - Multi-segment [Exif] Light Source - Unknown [Exif] Flash - Flash fired, auto [Exif] Focal Length - 5.5 mm [Exif] FlashPix Version - 1.00 [Exif] Color Space - sRGB [Exif] Exif Image Width - 1280 pixels [Exif] Exif Image Height - 960 pixels [Exif] Focal Plane X Resolution - 1/2415 cm [Exif] Focal Plane Y Resolution - 1/2415 cm [Exif] Focal Plane Resolution Unit - cm [Exif] Sensing Method - One-chip color area sensor [Exif] File Source - Digital Still Camera (DSC) [Exif] Scene Type - Directly photographed image [Exif] Custom Rendered - Normal process [Exif] Exposure Mode - Auto exposure [Exif] White Balance - Auto white balance [Exif] Scene Capture Type - Standard [Exif] Sharpness - None [Exif] Subject Distance Range - Unknown [Exif] Compression - JPEG (old-style) [Exif] Thumbnail Offset - 1252 bytes [Exif] Thumbnail Length - 7647 bytes [Exif] Thumbnail Data - [7647 bytes of thumbnail data]从这个执行的结果我们可以看出该照片是在2005年05月13日 22时18分49秒拍摄的,拍摄用的相机型号是富士的FinePix A205S,曝光时间是1/60秒,光圈值F3,焦距5.5毫米,ISO值为320等等。
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)。我们在拍照的时候经常会根据场景的不同来选择相机的方向,例如拍摄一颗高树,我们会把相机竖着拍摄,使景物刚好适合整个取景框,但是这样得到的图片如果用普通的图片浏览器看便是倒着的,需要调整角度才能得到一个正常的图像,有如下面一张照片。
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); } }从这个方法我们可以清楚看到各个返回值的意思,如此我们便可以根据实际的返回值来对图像进行旋转或者是镜像处理了。在这个例子中我们需要将图片顺时针旋转270度,或者是逆时针旋转90度方可得到正常的图片。
String path = "D:\\TEST.JPG"; File img = new File(path); 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:\\test2.jpg"); try{ ImageIO.write(new_img, "JPG", out); }finally{ out.close(); }旋转后的照片如下: