从图像中读取元数据/EXIFbyte[]
(对...有用Camera.takePicture() http://developer.android.com/reference/android/hardware/Camera.html#takePicture(android.hardware.Camera.ShutterCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback,%20android.hardware.Camera.PictureCallback)) using 版本2.9.1 of the Java中的元数据提取库 http://www.drewnoakes.com/code/exif/ by 德鲁·诺克斯 https://stackoverflow.com/users/24874/drew-noakes:
try
{
// Extract metadata.
Metadata metadata = ImageMetadataReader.readMetadata(new BufferedInputStream(new ByteArrayInputStream(imageData)), imageData.length);
// Log each directory.
for(Directory directory : metadata.getDirectories())
{
Log.d("LOG", "Directory: " + directory.getName());
// Log all errors.
for(String error : directory.getErrors())
{
Log.d("LOG", "> error: " + error);
}
// Log all tags.
for(Tag tag : directory.getTags())
{
Log.d("LOG", "> tag: " + tag.getTagName() + " = " + tag.getDescription());
}
}
}
catch(Exception e)
{
// TODO: handle exception
}
读取 EXIF方向图像的方向(不是缩略图的方向):
try
{
// Get the EXIF orientation.
final ExifIFD0Directory exifIFD0Directory = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
if(exifIFD0Directory.containsTag(ExifIFD0Directory.TAG_ORIENTATION))
{
final int exifOrientation = exifIFD0Directory.getInt(ExifIFD0Directory.TAG_ORIENTATION);
/* Work on exifOrientation */
}
else
{
/* Not found */
}
}
catch(Exception e)
{
// TODO: handle exception
}
方向是从 1 到 8。参见here http://sylvana.net/jpegcrop/exif_orientation.html, here http://www.impulseadventure.com/photo/exif-orientation.html, here http://developer.android.com/reference/android/media/ExifInterface.html or here http://beradrian.wordpress.com/2008/11/14/rotate-exif-images/.
要根据位图的 EXIF 方向转换位图:
try
{
final Matrix bitmapMatrix = new Matrix();
switch(exifOrientation)
{
case 1: break; // top left
case 2: bitmapMatrix.postScale(-1, 1); break; // top right
case 3: bitmapMatrix.postRotate(180); break; // bottom right
case 4: bitmapMatrix.postRotate(180); bitmapMatrix.postScale(-1, 1); break; // bottom left
case 5: bitmapMatrix.postRotate(90); bitmapMatrix.postScale(-1, 1); break; // left top
case 6: bitmapMatrix.postRotate(90); break; // right top
case 7: bitmapMatrix.postRotate(270); bitmapMatrix.postScale(-1, 1); break; // right bottom
case 8: bitmapMatrix.postRotate(270); break; // left bottom
default: break; // Unknown
}
// Create new bitmap.
final Bitmap transformedBitmap = Bitmap.createBitmap(imageBitmap, 0, 0, imageBitmap.getWidth(), imageBitmap.getHeight(), bitmapMatrix, false);
}
catch(Exception e)
{
// TODO: handle exception
}