6

setRotation methodinCamera.Parameters不适用于所有设备。有人建议手动更改EXIF信息以解决问题。您能否给我一个简短的示例,说明如何以将图像方向设置为纵向的方式设置exif信息?ExifInterface

private int savePicture(byte[] data)
{
       File pictureFile = getOutputMediaFile();
       if (pictureFile == null)
           return FILE_CREATION_ERROR;

       try {
           FileOutputStream fos = new FileOutputStream(pictureFile);
           fos.write(data);
           fos.close();
       } catch (FileNotFoundException e) {
           return FILE_NOT_FOUND;
       } catch (IOException e) {
           return ACCESSING_FILE_ERROR;
       }

   return OKAY;
}

我试过这个:

    try {
        ExifInterface exifi = new ExifInterface(pictureFile.getAbsolutePath());
        exifi.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_ROTATE_90));
        exifi.saveAttributes();
    } catch (IOException e) {
        Log.e(TAG, "Exif error");
    }

但是当我从 android 库中可视化图片时,没有任何变化。

4

3 回答 3

10

对于那些真正想写出这些 EXIF 信息的人,这里有一些代码:

ExifInterface exifInterface = new ExifInterface(someFile.getPath());
exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION,
                           String.valueOf(orientation));
exifInterface.saveAttributes();

而是orientation标准方向之一,即ExifInterface.ORIENTATION_ROTATE_{90,180,270}

于 2017-03-02T16:30:32.417 回答
3

如果方向已保存到文件中但未出现在图库中,则可能是因为方向已缓存在 MediaStore 中。因此,您还需要尝试在那里更新此信息。

这是截断的代码(未经测试)

/**
 * @param fileUri the media store file uri
 * @param orientation in degrees 0, 90, 180, 270
 * @param context
 * @return
 */
public boolean setOrientation(Uri fileUri, int orientation, Context context) {
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.ORIENTATION, orientation);
    int rowsUpdated = context.getContentResolver().update(fileUri, values, null, null);
    return rowsUpdated > 0;
}

/**
 * Get content uri for the file path
 * 
 * @param path
 * @param context
 * @return
 */
public Uri getContentUriForFilePath(String path, Context context) {
    String[] projection = {
        MediaStore.Images.Media._ID
    };
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
            MediaStore.Images.Media.DATA + " = ?", new String[] {
                path
            }, null);
    Uri result = null;
    if (cursor != null) {
        try {
            if (cursor.moveToNext()) {
                long mediaId = cursor.getLong(0);
                result = ContentUris.withAppendedId(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mediaId);
            }
        } finally {
            cursor.close();
        }
    }
    return result;
}
于 2014-03-13T06:59:46.057 回答
1

好吧,我陷入了这种问题,并尝试了您正在研究的解决方案并最终使用了 Matrix 函数。

无论我拍摄的是什么图像,都是风景,所以在应用程序中我只是应用了以下代码,并且效果很好:-

Matrix matrix = new Matrix();
//set image rotation value to 90 degrees in matrix.
matrix.postRotate(90);
//supply the original width and height, if you don't want to change the height and width of //bitmap.
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true);
于 2013-11-03T15:27:04.583 回答