Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if picture is landscape or portrait?

I have pictures in my Gallery that are both landscape or portrait. The show up correctly in the Gallery application. When I use an intent to select the picture from the gallery I get a URI back. But before I display the picture how do I know if the picture is portrait or landscape?

My application selects pictures using an Intent like this:

    private OnClickListener btnChooseFromLibraryListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
        intent.setType("image/*");
        startActivityForResult(intent, REQ_CODE_PICK_IMAGE);
    }
};

Here is how I get the intent back:

    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            SetPicture(filePath);
        }
    }
}

private void SetPicture(String filePath) {
    Bitmap bm = BitmapFactory.decodeFile(filePath);
    Log.d("TW", "Picture Path:" + filePath);
    String size = String.format("Width:%d Height:%d", bm.getWidth(), bm.getHeight());
    Log.d("TW", size);
    ivPicture.setImageBitmap(bm);
    ui.setLastPicture(filePath);        
}
like image 287
user846566 Avatar asked Dec 26 '22 21:12

user846566


2 Answers

Use this inside onActivityResult()

Uri selectedImage = imageReturnedIntent.getData();
                    String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
                    Cursor cur = managedQuery(selectedImage, orientationColumn, null, null, null);
                    int orientation = -1;
                    if (cur != null && cur.moveToFirst()) {
                        orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
                    }  

And use Matrix object to rotate your images

  Matrix matrix = new Matrix();
  matrix.postRotate(orientation);
like image 195
K_Anas Avatar answered Jan 09 '23 12:01

K_Anas


There's a MediaStore.Images.Media.ORIENTATION field used by the content provider.

The code would need to be slightly modified by including the field to tell you what orientation the image is at which is expressed in degrees, 0, 90, 180, 270.

String[] filePathColumn = {
         MediaStore.Images.Media.DATA, 
         MediaStore.Images.Media.ORIENTATION
};
Cursor cursor = getContentResolver().query(selectedImage, 
                   filePathColumn, 
                   null, 
                   null, 
                   null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
int orientationIndex = cursor.getColumnIndex(filePathPathColumn[1]);
String filePath = cursor.getString(columnIndex);
String degreesOrientation = cursor.getString(orientationIndex);
cursor.close();
// Now degreesOrientation will tell you exactly the rotation, as in
int nDegrees = Integer.parse(degreesOrientation);
// Check nDegrees - for example: if (nDegrees == 0 || nDegrees == 180) portrait.
like image 31
t0mm13b Avatar answered Jan 09 '23 10:01

t0mm13b