gpt4 book ai didi

java - 确保照片以与拍摄时相同的方向保存?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:39 25 4
gpt4 key购买 nike

出于某种原因,我的相机应用程序会保存所有旋转 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com