How to get degree of rotation of image

为君一笑 提交于 2019-12-13 04:56:31

问题


I'm getting an image from gallery and display it in an imageView. Sometimes, image displays 90 degrees CCW.

So, my question is: 1- is it possible to get angle of rotation of image? 2- is it possible to get angle by a face detection library? if yes, which library you suggest?

any recommendation would be appreciated. Thanks.


回答1:


Okay, I found a solution that I share it with you.

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;
        }
    }
}


来源:https://stackoverflow.com/questions/24968677/how-to-get-degree-of-rotation-of-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!