如何获得图像的旋转度

时间:2014-07-26 07:31:27

标签: android image android-imageview android-gallery

我从图库中获取图像并将其显示在imageView中。有时,图像显示CCW为90度。

所以,我的问题是: 1-是否可以获得图像的旋转角度? 2-是否可以通过人脸检测库获取角度?如果是的话,你建议使用哪个库?

任何建议将不胜感激。感谢。

1 个答案:

答案 0 :(得分:2)

好的,我找到了一个与你分享的解决方案。

public class ImageCorrection extends Activity {

    private final static String TAG = MainActivity.class.getSimpleName();
    private final static int RESULT_LOAD_IMAGE = 100;

    private ImageButton imageButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_detection);

        imageButton = (ImageButton) findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, RESULT_LOAD_IMAGE);
            }
        });
    }

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

        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (data != null) {
                    Uri imageUri = data.getData();
                    Log.d(TAG, "Image Uri: " + imageUri.toString());

                    try {
                        Bitmap myImg = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
                        if(myImg == null) {
                            Log.e(TAG, "Image bitmap is null...");
                            return;
                        }

                        Cursor cur;
                        String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
                        if(Build.VERSION.SDK_INT < 11)
                            cur = managedQuery(imageUri, orientationColumn, null, null, null);
                        else
                            cur = getContentResolver().query(imageUri, orientationColumn, null, null, null);

                        int orientation = -1;
                        if (cur != null && cur.moveToFirst()) {
                            orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
                            Log.d(TAG, "Image Orientation: " + orientation);
                        }

                        Matrix matrix = new Matrix();
                        matrix.reset();
                        matrix.postRotate(orientation);

                        Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(), matrix, true);
                        imageButton.setImageBitmap(rotated);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
            default:
                break;
        }
    }
}