4

通过意图捕获图像时,我遇到了一个问题。

我正在使用默认意图代码来捕获图像。如下。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                 startActivityForResult(intent, TAKE_PHOTO_CODE);

并在 onActivityResult 中接收它,如下所示。

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if( TAKE_PHOTO_CODE==requestCode){

        try {
             Bundle extras = data.getExtras();
             Bitmap mImageBitmap = (Bitmap) extras.get("data");
             selectedImagePath = saveImageInfo(mImageBitmap, "image");
             ExifInterface exif = new ExifInterface(selectedImagePath);
            Toast.makeText(getBaseContext()," "+exif.getAttribute(ExifInterface.TAG_ORIENTATION), Toast.LENGTH_SHORT).show();

         try {
                showDialog(DIALOG_UPLOADING);

            } catch (Exception e) {

                e.printStackTrace();
            }
          } catch (Exception e) {

          e.printStackTrace();
          Toast.makeText(getApplicationContext(), "No image selected.. ", Toast.LENGTH_SHORT).show();
        }
    }

}

我想要在这个 onActivityResult 方法中捕获的图像方向,我已经搜索并找到了 ExifInterface,但它对我不起作用,我在 LG Optimus Android 设备上进行测试,它总是为我返回 0。

问题是在捕获图像后它会旋转。任何解决方案?

4

1 回答 1

1

您可以使用该方法获得旋转,然后根据结果旋转图像 -

private int getDisplayRotation() {
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    switch (rotation) {
        case Surface.ROTATION_0: return 0;
        case Surface.ROTATION_90: return 90;
        case Surface.ROTATION_180: return 180;
        case Surface.ROTATION_270: return 270;
    }
    return 0;
}

为了摆脱许多设备特定的问题,我从头开始创建相机您可以从https://github.com/vinsol/expense-tracker/tree/master/src/com/vinsol/expensetracker/cameraservice获取代码

然后使用以下方法调用它

private void startCamera() {
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
        Intent camera = new Intent(this, Camera.class);
        File file = new File("123.jpg");
        Log.d("camera file path "+file.toString() );
        camera.putExtra("FullSizeImagePath", file.toString());
        startActivityForResult(camera, PICTURE_RESULT);
    } else {
        Toast.makeText(this, "sdcard not available", Toast.LENGTH_LONG).show();
    }
}
于 2012-08-31T08:07:18.647 回答