Android捕获图像EXIF方向标记值始终显示为0

时间:2015-07-15 06:08:48

标签: android camera exif

我曾试图在某些Android设备中使用Back camera捕获图像,使用postRotate(90)可以正常工作(不旋转)。当我试图用三星Galaxy S5前置摄像头拍摄图像时,它显示旋转/角度图像

Original Image Rotated Image

这是我的代码:

    private static final int PICK_FROM_CAMERA = 1;
    private static final int PICK_FROM_FILE = 2;
    private Uri mImageCaptureUri;

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    mImageCaptureUri = Uri.fromFile(new File(Environment
                        .getExternalStorageDirectory(), "Img"
                        + String.valueOf(System.currentTimeMillis())
                        + ".jpg"));
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                                mImageCaptureUri);
    try {
      intent.putExtra("return-data", true);
      startActivityForResult(intent, PICK_FROM_CAMERA);
    } catch (ActivityNotFoundException e) {
       e.printStackTrace();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK)
            return;
        Drawable drawable = null;
        switch (requestCode) {
        case PICK_FROM_CAMERA:
            if (resultCode == Activity.RESULT_OK) {
                if (mImageCaptureUri != null) {
                    drawable = getDrawableImage(this,
                            mImageCaptureUri.toString(), true);

                }
            }

            break;
    }

    public static Drawable getDrawableImage(Context context, String path,
            boolean isCamera) {
        if (path == null || path.length() == 0)
            return null;
        else
            try {
                InputStream is = (InputStream) new URL(path).getContent();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                Bitmap oBitmap = BitmapFactory.decodeStream(is, null, options);
                int width_tmp = options.outWidth;
                int height_tmp = options.outHeight;
                WindowManager wm = ((WindowManager) context
                        .getSystemService(Context.WINDOW_SERVICE));
                Display display = wm.getDefaultDisplay();
                int heightRatio = (height_tmp / display.getHeight());
                int widthRatio = (width_tmp / display.getWidth());

                int s = 1;

                if (heightRatio > 1 || widthRatio > 1) {
                    if (heightRatio > widthRatio) {
                        s = heightRatio;
                    } else {
                        s = widthRatio;
                    }
                }
                Drawable drawable;
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = s;
                Bitmap bitmap = BitmapFactory.decodeStream(
                        (InputStream) new URL(path).getContent(), null, o2);

                if (isCamera) {
                    int w = bitmap.getWidth();
                    int h = bitmap.getHeight();

                    int rotate = 0;
                    Matrix mat = new Matrix();
                    try {
                        ExifInterface exif = new ExifInterface(path);
                        int orientation = exif.getAttributeInt(
                                ExifInterface.TAG_ORIENTATION,
                                ExifInterface.ORIENTATION_NORMAL);
                        switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            rotate = 270;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            rotate = 180;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            rotate = 90;
                            break;
                        }
                    }catch(Exception e){
                        e.printStackTrace();
                    }
                    mat.postRotate(rotate);
                    /*if (w > h)
                        mat.postRotate(90);*/
                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mat, true);
                }

                drawable = new BitmapDrawable(bitmap);

                return drawable;
            } catch (Exception e) {
                return null;
            }
    }

0 个答案:

没有答案