For some reason, my camera app saves all photos rotated 90 degrees (pictures only look right when taken with camera on landscape mode) I believe onPictureTaken should rotate photos automatically but I read there is a problem with Samsung devices (I haven't been able to test it on another brand so I don't know if it's the case). This is my code:
由于某种原因,我的相机应用程序保存所有旋转90度的照片(图片只在横向模式下用相机拍摄时看起来正确)我相信照片应该自动旋转照片但我读到三星设备有问题(我还没有能够在另一个品牌测试它,所以我不知道是否是这种情况)。这是我的代码:
public void onPictureTaken(byte[] data, Camera camera) {
// Generate file name
FileOutputStream outStream = null;
outStream = new FileOutputStream(filePath);
outStream.write(data);
outStream.close();
I was thinking it could be fixed by checking the orientation and rotating the byte array but there must be a more straightforward way to do it since handling byte arrays is a pain. How can I make sure photos are saved matching the orientation they were taken?
我认为可以通过检查方向和旋转字节数组来修复它,但是必须有一种更直接的方法来处理它,因为处理字节数组很麻烦。如何确保照片的保存方式与拍摄方向相符?
2 个解决方案
#1
0
Try something like this:
尝试这样的事情:
int orientation = Exif.getOrientation(data);
Log.d("#", "onPictureTaken().orientation = " + orientation);
if(orientation != 0) {
Bitmap bmpSrc = BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap bmpRotated = CameraUtil.rotate(bmpSrc, orientation);
bmpSrc.recycle();
try {
FileOutputStream localFileOutputStream = new FileOutputStream(filePath);
bmpRotated.compress(Bitmap.CompressFormat.JPEG, 90,localFileOutputStream);
localFileOutputStream.flush();
localFileOutputStream.close();
bmpRotated.recycle();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
} else {
try {
FileOutputStream localFileOutputStream = new FileOutputStream(filePath);
localFileOutputStream.write(data);
localFileOutputStream.flush();
localFileOutputStream.close();
} catch (IOException localIOException)
{
Log.e("#",localIOException.getMessage());
}
}
#2
0
There are different ways to get your image, as data, as stream, as file, also it is different for camera and gallery and other apps. For each of them you have another way to access the orientation tag. Once you have the orientation, you can rotate your image.
有不同的方式来获取您的图像,如数据,流,文件,以及相机和图库和其他应用程序的不同。对于他们每个人,您有另一种方法来访问方向标记。获得方向后,您可以旋转图像。
You - or for that matter everyone - should really get a Nexus, they are nice and rotate the image for you and set orientation to 0, while the lazy ones like Samsung just store the image and set the orientation tag. ;)
你 - 或者就此而言 - 每个人 - 应该真的得到一个Nexus,他们很好并为你旋转图像并将方向设置为0,而像三星这样的懒人只存储图像并设置方向标记。 ;)
#1
0
Try something like this:
尝试这样的事情:
int orientation = Exif.getOrientation(data);
Log.d("#", "onPictureTaken().orientation = " + orientation);
if(orientation != 0) {
Bitmap bmpSrc = BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap bmpRotated = CameraUtil.rotate(bmpSrc, orientation);
bmpSrc.recycle();
try {
FileOutputStream localFileOutputStream = new FileOutputStream(filePath);
bmpRotated.compress(Bitmap.CompressFormat.JPEG, 90,localFileOutputStream);
localFileOutputStream.flush();
localFileOutputStream.close();
bmpRotated.recycle();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
} else {
try {
FileOutputStream localFileOutputStream = new FileOutputStream(filePath);
localFileOutputStream.write(data);
localFileOutputStream.flush();
localFileOutputStream.close();
} catch (IOException localIOException)
{
Log.e("#",localIOException.getMessage());
}
}
#2
0
There are different ways to get your image, as data, as stream, as file, also it is different for camera and gallery and other apps. For each of them you have another way to access the orientation tag. Once you have the orientation, you can rotate your image.
有不同的方式来获取您的图像,如数据,流,文件,以及相机和图库和其他应用程序的不同。对于他们每个人,您有另一种方法来访问方向标记。获得方向后,您可以旋转图像。
You - or for that matter everyone - should really get a Nexus, they are nice and rotate the image for you and set orientation to 0, while the lazy ones like Samsung just store the image and set the orientation tag. ;)
你 - 或者就此而言 - 每个人 - 应该真的得到一个Nexus,他们很好并为你旋转图像并将方向设置为0,而像三星这样的懒人只存储图像并设置方向标记。 ;)