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

标签 java android camera bytearray photo

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

相关文章:

java - 需要根据黑名单解析日志文件

Java 日历 : calculate start date and end date of the previous week

android - 如何在 Eclipse 中配置 Android NDK 的位置?

java - JAXB:我应该如何编码复杂的嵌套数据结构?

java - JqG​​rid 不从 JSON 加载数据

android - 如何在不重复自己的情况下在子项目中拥有相同的依赖关系?

android - 在操作栏中实现向上导航按钮时出错 : java. lang.RuntimeException:无法启动 Activity ComponentInfo

tensorflow - Tensorflow lite 对象检测示例中相机的屏幕尺寸

objective-c - 如何从特定文件夹中读取图像?

android - 使用新的 Camera API 捕捉高帧率视频