【问题标题】:How to know if picture is landscape or portrait?如何判断图片是横向还是纵向?
【发布时间】:2012-07-14 15:58:41
【问题描述】:

我的图库中有横向或纵向的图片。在画廊应用程序中正确显示。当我使用意图从图库中选择图片时,我会返回一个 URI。但是在显示图片之前,我如何知道图片是纵向还是横向?

我的应用程序使用这样的 Intent 选择图片:

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

这是我恢复意图的方法:

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

【问题讨论】:

标签: android android-intent


【解决方案1】:

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

并使用 Matrix 对象来旋转您的图像

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

【讨论】:

    【解决方案2】:

    内容提供者使用了一个MediaStore.Images.Media.ORIENTATION 字段。

    代码需要稍作修改,通过包含字段来告诉您图像所在的orientation 以度数、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.
    

    【讨论】:

      【解决方案3】:

      在上述“答案”之后,我编写了以下方法。希望这对其他人有帮助。

      private int GetRotateAngle(Uri imageUri) {
          String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.ORIENTATION};
          Cursor cursor = getContentResolver().query(imageUri, columns, null, null, null);
          if (cursor == null) { return 0; }
          cursor.moveToFirst();
      
          int orientationColumnIndex = cursor.getColumnIndex(columns[1]);
          int orientation = cursor.getInt(orientationColumnIndex);
          cursor.close();
          return orientation;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多