Android - 确保通过Camera Intent拍摄照片时保留照片方向?

时间:2016-10-06 13:02:03

标签: android android-intent camera android-camera android-camera-intent

我正在构建一个Android应用程序,用户可以在其中拍照,然后将其保存到某个文件路径。我没有使用相机意图并将结果保存到自定义文件路径以及在ImageView中向用户显示它的问题。但是,当我在几台设备上测试它时,我最近遇到了一种奇怪的行为。

每当我拍摄肖像照片时,我都会得到风景照片。

以下是我使用的代码:

public void onTakePhoto(View view){


    //camera stuff
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    //folder stuff
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "folderHere");
    imagesFolder.mkdirs();

    File image = new File(imagesFolder, timeStamp + ".png");
    Uri uriSavedImage = Uri.fromFile(image);

    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    startActivityForResult(imageIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}

打开相机并拍摄1张照片。在1次拍摄之后,用户可以选择重试拍摄照片。我发现,对于某些设备,即使我拍摄照片肖像,输出也会变成风景。我已将活动的方向设置为肖像。

到目前为止我做了什么来获得结果,使用Exif文件确定旋转,然后相应地旋转它(如我所拥有的问题的一些答案中详细说明的那样)。然而,这里的问题是需要一些时间,在我回到我在ImageView中看到它的活动之前,我得到3-4秒的黑屏。

这是我使用的轮换代码:

File是String中的文件路径。

BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file, bounds);

BitmapFactory.Options opts = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(file, opts);
ExifInterface exif = null;

try {
    exif = new ExifInterface(file);
} catch (IOException e) {
    Log.e(TAG, "exception hit while making exif!", e);
}

String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
int orientation = orientString != null ? Integer.parseInt(orientString) :  ExifInterface.ORIENTATION_NORMAL;

int rotationAngle = 0;
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) rotationAngle = 90;
if (orientation == ExifInterface.ORIENTATION_ROTATE_180) rotationAngle = 180;
if (orientation == ExifInterface.ORIENTATION_ROTATE_270) rotationAngle = 270;

Matrix matrix = new Matrix();
matrix.setRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);

FileOutputStream out = null;
try {
    out = new FileOutputStream(imagePath);
    rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
    // PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
    Log.e(TAG, "exception hit while exporting!", e);
    e.printStackTrace();
} finally {
    try {
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

保存照片时是否有可靠的方法确保相机的方向得以保留?如果相机处于人像状态,那么结果也应该是纵向的,横向的情况也是如此。

我一直在四处寻找,但我没有运气。我能想到的一个解决方案是,也许我们可以传递一些与相机意图相关的东西,比如旋转常数或其他东西,这样输出就不会被搞乱,我们得到了我们想要的方向。

1 个答案:

答案 0 :(得分:4)

  

保存照片时是否有可靠的方法确保相机的方向得以保留?

没有

首先,您将拍照委托给第三方应用程序,这是数千个可能的相机应用程序之一。那些开发者可以做任何他们想做的事。

其次,其根本原因来自相机硬件和固件。您无法告诉某些芯片组旋转图像而不是使用EXIF方向标题。