使用 Android 相机 API,快照照片的方向始终未定义
Using Android Camera API, snapped photo's orientation is always Undefined
我用相机api拍出来的照片总是旋转90度,我想旋转一下。
所以首先我想知道图片的方向,这一点我卡住了。
我总是在两种方式中都得到 UNDEFINDED orientation。
代码如下:
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//Bitmap Options for lowering quality the bitmap to save memory
Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
options.inPreferredConfig = Bitmap.Config.RGB_565;
//Make the bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
//Making the path, the root will be fine for tests
String path = Environment.getExternalStorageDirectory().toString();
//output stream
OutputStream outputStream = null;
//Making the file as a jpg
File file = new File(path, "tmp_pic" + ".jpg"); // the File to save to
try {
//Writing the file
outputStream = new FileOutputStream(file);
outputStream.flush();
outputStream.close(); // do not forget to close the stream
//Getting the orientation in both possible ways
int ori = getOrientationFromExif(file.getPath());
int ori2 = getOrientationFromUri(Uri.fromFile(file));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
if (bitmap == null) {
Toast.makeText(getApplicationContext(), "Error: Cant make photo.", Toast.LENGTH_SHORT).show();
}
else {
PhotoTapView.photoViews.get(index).setPhotoImage(bitmap);
finish();
}
cameraObject.release();
}
定位功能:
//Getting orientation from file URI
private int getOrientationFromUri(Uri imageUri) {
String[] orientationColumn = { MediaStore.Images.Media.ORIENTATION };
Cursor cur = getContentResolver().query(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Log.i("Orientation from Uri", orientation + "");
return orientation;
}
//Getting orientation from ExifInterface
private static int getOrientationFromExif(String imagePath) {
int orientation = -1;
try {
ExifInterface exif = new ExifInterface(imagePath);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.i("Orientation from Exif: ", exifOrientation + "");
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
orientation = 270;
Log.i("Orientation from Exif", "270");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;
Log.i("Orientation from Exif", "180");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Log.i("Orientation from Exif", "90");
orientation = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
orientation = 0;
Log.i("Orientation from Exif", "0 - Normal");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
orientation = -1;
Log.e("Orientation from Exif", "UNDEFINED");
}
}
catch (IOException e) {}
return orientation;
}
日志输出:
01-14 19:46:09.468: E/Orientation from Exif(12411): UNDEFINED
01-14 19:46:09.468: I/Orientation from Uri(12411): -1
可能是什么问题?
在此代码中
outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream); // Add this line
outputStream.flush();
outputStream.close(); // do not forget to close the stream
您忘记实际将字节写入输出流。所以,文件长度为0,自然不包含任何EXIF信息。
另外注意调用decodeByteArray
然后再保存出来,这样也会丢失EXIF信息!您需要从原始图像字节中解析出 EXIF。
自 2011 年初以来,我一直是 decoding/observing Android JPEG 图片,因为我发布了一个图片查看应用程序。在 'early' 天里,图像以传感器的原始方向编码,设备的实际方向被写入 EXIF 元数据。大约从 2 年前开始,我注意到方向不再写入 EXIF 数据,取而代之的是,相机应用程序在编码 JPEG 文件之前旋转图像像素。我的猜测是,这是因为某些照片查看器 (* cough * Windows * cough *) 在显示 JPEG 文件时忽略了 EXIF 方向,而不是等待 Microsoft 修复它并指责 Android 做了某事错了,他们决定使用速度更快的 CPU/memory 并只旋转图像。
结果是,在不知道 EXIF 方向的情况下,只能确定照片是横向还是纵向拍摄的。这条信息只是一个假设,因为大多数相机传感器的宽度大于高度。
我用相机api拍出来的照片总是旋转90度,我想旋转一下。
所以首先我想知道图片的方向,这一点我卡住了。 我总是在两种方式中都得到 UNDEFINDED orientation。
代码如下:
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//Bitmap Options for lowering quality the bitmap to save memory
Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
options.inPreferredConfig = Bitmap.Config.RGB_565;
//Make the bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
//Making the path, the root will be fine for tests
String path = Environment.getExternalStorageDirectory().toString();
//output stream
OutputStream outputStream = null;
//Making the file as a jpg
File file = new File(path, "tmp_pic" + ".jpg"); // the File to save to
try {
//Writing the file
outputStream = new FileOutputStream(file);
outputStream.flush();
outputStream.close(); // do not forget to close the stream
//Getting the orientation in both possible ways
int ori = getOrientationFromExif(file.getPath());
int ori2 = getOrientationFromUri(Uri.fromFile(file));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
if (bitmap == null) {
Toast.makeText(getApplicationContext(), "Error: Cant make photo.", Toast.LENGTH_SHORT).show();
}
else {
PhotoTapView.photoViews.get(index).setPhotoImage(bitmap);
finish();
}
cameraObject.release();
}
定位功能:
//Getting orientation from file URI
private int getOrientationFromUri(Uri imageUri) {
String[] orientationColumn = { MediaStore.Images.Media.ORIENTATION };
Cursor cur = getContentResolver().query(imageUri, orientationColumn, null, null, null);
int orientation = -1;
if (cur != null && cur.moveToFirst()) {
orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
}
Log.i("Orientation from Uri", orientation + "");
return orientation;
}
//Getting orientation from ExifInterface
private static int getOrientationFromExif(String imagePath) {
int orientation = -1;
try {
ExifInterface exif = new ExifInterface(imagePath);
int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.i("Orientation from Exif: ", exifOrientation + "");
switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
orientation = 270;
Log.i("Orientation from Exif", "270");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;
Log.i("Orientation from Exif", "180");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Log.i("Orientation from Exif", "90");
orientation = 90;
break;
case ExifInterface.ORIENTATION_NORMAL:
orientation = 0;
Log.i("Orientation from Exif", "0 - Normal");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
orientation = -1;
Log.e("Orientation from Exif", "UNDEFINED");
}
}
catch (IOException e) {}
return orientation;
}
日志输出:
01-14 19:46:09.468: E/Orientation from Exif(12411): UNDEFINED
01-14 19:46:09.468: I/Orientation from Uri(12411): -1
可能是什么问题?
在此代码中
outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream); // Add this line
outputStream.flush();
outputStream.close(); // do not forget to close the stream
您忘记实际将字节写入输出流。所以,文件长度为0,自然不包含任何EXIF信息。
另外注意调用decodeByteArray
然后再保存出来,这样也会丢失EXIF信息!您需要从原始图像字节中解析出 EXIF。
自 2011 年初以来,我一直是 decoding/observing Android JPEG 图片,因为我发布了一个图片查看应用程序。在 'early' 天里,图像以传感器的原始方向编码,设备的实际方向被写入 EXIF 元数据。大约从 2 年前开始,我注意到方向不再写入 EXIF 数据,取而代之的是,相机应用程序在编码 JPEG 文件之前旋转图像像素。我的猜测是,这是因为某些照片查看器 (* cough * Windows * cough *) 在显示 JPEG 文件时忽略了 EXIF 方向,而不是等待 Microsoft 修复它并指责 Android 做了某事错了,他们决定使用速度更快的 CPU/memory 并只旋转图像。
结果是,在不知道 EXIF 方向的情况下,只能确定照片是横向还是纵向拍摄的。这条信息只是一个假设,因为大多数相机传感器的宽度大于高度。