gpt4 book ai didi

android - 如何知道图片是风景还是肖像?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:48 26 4
gpt4 key购买 nike

我的图库中有横向或纵向的图片。在图库应用程序中正确显示。当我使用 Intent 从图库中选择图片时,我得到了一个 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);
}
};

以下是我如何恢复 Intent :

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

最佳答案

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

关于android - 如何知道图片是风景还是肖像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11485188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com