简体   繁体   English

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

[英]Make sure photos are saved with the same orientation they were taken?

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). 由于某种原因,我的相机应用程序保存所有旋转90度的照片(图片只在横向模式下用相机拍摄时看起来正确)我相信onPictureTaken应该自动旋转照片但我读到三星设备有问题(我还没有能够在另一个品牌测试它,所以我不知道是否是这种情况)。 This is my code: 这是我的代码:

   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? 如何确保照片的保存方式与拍摄方向相符?

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());
                  }
   }

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,而像三星这样的懒人只存储图像并设置方向标记。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dropbox的照片和“拍摄日期” - Dropbox photos and “date taken” 如何制作一个程序来循环遍历 Instagram 上所有保存的照片 - How to make a program which loops over all saved photos on instagram 如何确定图像未保存到图库? - How can i make sure the image is not saved to gallery? 自动旋转使用智能手机拍摄的照片 - Rotate automatically photos taken with Smartphone 泛型:确保参数类型相同 - Generics: Make sure parameters are of same type 确保在同一个(EDT)事件调度线程中 - Make sure in same (EDT) event dispatching thread 如何存储/排队要保存的多张照片以使它们都可以脱机使用? - How to store/queue multiple photos to be saved to make them all available offline? 我知道如何打开相机的按钮,但是我不知道如何更改拍摄照片的位置,我该怎么做? - I know how to make a button open the camera, but I don't how to change the location of the photos that are taken, how can I do this? 按下按钮后,同时删除已选择并保存在列表中的jTable的行 - Remove the rows of jTable that have been selected and were saved in a list at same time after press a button 如何从项目级别指定 --add-opens 并确保以任何方式运行我的应用程序都将其考虑在内? - How can I specify --add-opens from a project level and make sure it is taken into account whatever the means to run my app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM