【问题标题】:Image orientation and ExifInterface图像方向和 ExifInterface
【发布时间】:2014-10-13 17:29:50
【问题描述】:

我想获得位图的方向。我使用此代码:

ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,      
ExifInterface.ORIENTATION_NORMAL);

switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
    rotateImage(bitmap, 90);
    break;
case ExifInterface.ORIENTATION_ROTATE_180:
    rotateImage(bitmap, 180);
    break;
// etc.
}

我的问题是这一行:

ExifInterface ei = new ExifInterface(photoPath);

在此代码中 photoPath 是一个字符串,但我没有任何字符串路径!我在位图内的内存中加载了一个位图!如何设置 ExifInterface 的路径?

【问题讨论】:

    标签: java android bitmap


    【解决方案1】:
    • 第一个解决方案是创建一个临时文件并调用标准new ExifInterface(tempFile)。我不喜欢它效率低下

    • here 给出了其他解决方案,但您必须通过添加 3rd 方代码来使您的 APK 更大。

    • 我个人选择的解决方案是在捕获图像时将图像旋转到临时文件中,然后将其保存在 Sqlite 数据库的博客字段中。然后在查看时,我不需要处理旋转,以及无法读取 EXIF 标头的问题,因为我没有文件。

    【讨论】:

      【解决方案2】:

      如何设置 ExifInterface 的路径?

      你没有。

      但是,当您以Bitmap 结束时,您也没有任何 EXIF 标头。

      如果您尝试将其应用于相机拍摄的照片,并且该照片是 JPEG 格式,则表示已编码照片的 byte[] 可以传递给 ExifInterface 对象的 readExif() 方法(从零参数构造函数创建):

      ExifInterface exif=new ExifInterface();
      
      exif.readExif(data);
      

      否则,您可能希望解释此Bitmap 的来源。

      【讨论】:

      • 在 API 19 源代码中,我没有看到任何名为 readExif() 的方法,也没有看到无参数构造函数。 :-(
      • @david.perez:啊,好点子。我以为我忘记了什么。在我的相机库中,我没有使用 SDK 的 ExifInterface,而是使用来自 AOSP 消息传递客户端的类似名称的类:github.com/commonsguy/cwac-camera/tree/master/camera/src/com/…
      【解决方案3】:
          private File createImageFile() throws IOException {
                  // Create an image file name
                  String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                  String imageFileName = "JPEG_" + timeStamp + "_";
                  File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
                  File image = File.createTempFile(
                          imageFileName,  /* prefix */
                          ".jpg",         /* suffix */
                          storageDir      /* directory */
                  );
          
                  // Save a file: path for use with ACTION_VIEW intents
                  currentPhotoPath = image.getAbsolutePath();
                  return image;
              }
      
          File file=createImageFile() 
          pass file to ExifInteface
      
      
         
       photoURI = FileProvider.getUriForFile(YourActivity.this, getApplicationContext().getPackageName() + ".provider", file);
      
      Make sure you click on the same file path
       intent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多