出于某种原因,我的相机应用程序会保存所有旋转 90 度的照片(只有在横向模式下使用相机拍摄的照片才看起来正确)我相信 onPictureTaken 应该会自动旋转照片,但我读到三星设备存在问题(我没有'无法在其他品牌上进行测试,所以我不知道是否如此)。这是我的代码:
public void onPictureTaken(byte[] data, Camera camera) {
// Generate file name
FileOutputStream outStream = null;
outStream = new FileOutputStream(filePath);
outStream.write(data);
outStream.close();
我认为它可以通过检查方向和旋转字节数组来修复,但必须有更直接的方法来完成,因为处理字节数组很痛苦。 如何确保照片的保存方向与拍摄方向一致?
最佳答案
尝试这样的事情:
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());
}
}
关于java - 确保照片以与拍摄时相同的方向保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14556569/