简体   繁体   English

Android相机定位问题

[英]Android Camera Orientation ISsue

pictures taken in vertical format are saved in landscape format and vice-versa. 以垂直格式拍摄的照片以横向格式保存,反之亦然。 I am using Android camera by using this intent 我使用这个意图使用Android相机

Intent captureImage = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureImage.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);               
startActivityForResult(captureImage, CAMERA_PIC_REQUEST);

onActivityResult() I am just saving image URL to my database and displaying it in a listview. onActivityResult()我只是将图像URL保存到我的数据库并在列表视图中显示它。 but there orientatiuon changes. 但是有了方向性的变化。 The same will happen if I choose image from gallery and save it. 如果我从图库中选择图像并保存它,也会发生同样的情况。

I want the orientation in which photo has been taken. 我想要拍摄照片的方向。 I dont want to change it. 我不想改变它。 Is anybody have a solutin on this. 有没有人对此有解决方法。

Some devices doesn't rotate image after it was taken but just write its orientation information into Exif data. 有些设备在拍摄后不会旋转图像,只是将其方向信息写入Exif数据。 So before using taken photo you should call method like : 因此在使用拍摄照片之前,您应该调用以下方法:

private int resolveBitmapOrientation(File bitmapFile) throws IOException {
        ExifInterface exif = null;
        exif = new ExifInterface(bitmapFile.getAbsolutePath());

        return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    }

to check its orientation. 检查其方向。 Then apply: 然后申请:

private Bitmap applyOrientation(Bitmap bitmap, int orientation) {
        int rotate = 0;
        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;
            default:
                return bitmap;
        }

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix mtx = new Matrix();
        mtx.postRotate(rotate);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    }

and use this new bitmap in your listview. 并在列表视图中使用此新位图。 Or it's even better to call this methods just after your photo was taken and override it with new rotated one. 或者,最好在拍摄照片后立即调用此方法,并使用新的旋转照片覆盖它。

In case if you are receiving Bitmap data as Uri the following method can be used to retrieve its filepath: 如果您将位图数据作为Uri接收,则可以使用以下方法来检索其文件路径:

public static String getPathFromURI(Context context, Uri contentUri) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT &&
            DocumentsContract.isDocumentUri(context, contentUri)) {
        return getPathForV19AndUp(context, contentUri);
    } else {
        return getPathForPreV19(context, contentUri);
    }
}

private static String getPathForPreV19(Context context, Uri contentUri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
        try {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            return cursor.getString(columnIndex);
        } finally {
            cursor.close();
        }
    }

    return null;
}

@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getPathForV19AndUp(Context context, Uri contentUri) {
    String documentId = DocumentsContract.getDocumentId(contentUri);
    String id = documentId.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };
    String sel = MediaStore.Images.Media._ID + "=?";
    Cursor cursor = context.getContentResolver().
            query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    column, sel, new String[]{ id }, null);

    if (cursor != null) {
        try {
            int columnIndex = cursor.getColumnIndex(column[0]);
            if (cursor.moveToFirst()) {
                return cursor.getString(columnIndex);
            }
        } finally {
            cursor.close();
        }
    }

    return null;
}

You can also follow by this way: 你也可以这样跟着:

static Uri image_uri;
static  Bitmap taken_image=null;

                image_uri=fileUri; // file where image has been saved

          taken_image=BitmapFactory.decodeFile(image_uri.getPath());
          try
            {
                ExifInterface exif = new ExifInterface(image_uri.getPath()); 
//Since API Level 5
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);


                switch(orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                        RotateBitmap(taken_image, 90);
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                        RotateBitmap(taken_image, 180);

                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                        RotateBitmap(taken_image, 270);

                        break;
                    case ExifInterface.ORIENTATION_NORMAL:
                        taken_image=decodeScaledBitmapFromSdCard(image_uri.getPath(), 200, 200);
                        RotateBitmap(taken_image, 0);

                        break;
                }

            }
            catch (OutOfMemoryError e)
            {
                Toast.makeText(getActivity(),e+"\"memory exception occured\"",Toast.LENGTH_LONG).show();


            }



public Bitmap RotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);

    round_Image = source;
    round_Image = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);


    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM