当前位置: 首页 > 知识库问答 >
问题:

如何设置相机图像方向?

闻人英韶
2023-03-14

在我的应用程序中,我添加了图像上传功能,它可以很好地处理除相机图像之外的所有图像,每当我从画廊浏览相机图像时,肖像图像旋转90度。下面是我的代码片段。有人能帮我吗?我遵循了很多教程,但它们在kikat中都很好。但当同一教程不适用于ics、jellybean等时。。

public class MainActivity extends Activity {
private Button browse;
private String selectedImagePath="";
private ImageView img;

private TextView messageText;

private static int SELECT_PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img = (ImageView)findViewById(R.id.imagevw);
    browse=(Button)findViewById(R.id.browseimg);
    messageText  = (TextView)findViewById(R.id.messageText);
    browse.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
        }
    });
}
 @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (resultCode == RESULT_OK) {
           if (requestCode == SELECT_PICTURE) {
               Uri selectedImageUri = data.getData();

               /*String filePath = getRealPathFromURI(getActivity(), selectedImageUri );
               messageText.setText(filePath );
               Picasso.with(getActivity())
                                      .load(new File(filePath ))
                                      .centerCrop()
                                      .resize(60, 60).into( img);*/

               selectedImagePath = getPath(selectedImageUri);
               messageText.setText(selectedImagePath);
               System.out.println(requestCode);
               System.out.println("Image Path : " + selectedImagePath);
               img.setImageURI(selectedImageUri);
           }
       }
 }
 @SuppressWarnings("deprecation")
    public String getPath(Uri uri) {
       String[] projection = { MediaStore.Images.Media.DATA };
       Cursor cursor = managedQuery(uri, projection, null, null, null);
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       cursor.moveToFirst();
       return cursor.getString(column_index);
   }

   }

共有3个答案

穆俊哲
2023-03-14
     String filePath = getRealPathFromURI(getActivity(), selectedImageUri );
     messageText.setText(filePath );
     Picasso.with(getActivity())
                            .load(new File(filePath ))
                            .centerCrop()
                            .resize(60, 60).into( img);
顾宏朗
2023-03-14

有几种方法,但我找到的最简单的方法是使用毕加索库。由于这是一个上传情况,我们将获得正确的方向,也可以调整图像位图大小。

东方森
2023-03-14

只需包含此代码

public void rotateImage(String file) throws IOException{

    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(file, bounds);

    BitmapFactory.Options opts = new BitmapFactory.Options();
    Bitmap bm = BitmapFactory.decodeFile(file, opts);

    int rotationAngle = getCameraPhotoOrientation(getActivity(), Uri.fromFile(file1), file1.toString());

    Matrix matrix = new Matrix();
    matrix.postRotate(rotationAngle, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0, bounds.outWidth, bounds.outHeight, matrix, true);
    FileOutputStream fos=new FileOutputStream(file);
    rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
}

public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
    int rotate = 0;
    try {
        context.getContentResolver().notifyChange(imageUri, null);
        File imageFile = new File(imagePath);
        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
        switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            rotate = 0;
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}
 类似资料:
  • 我正在试着做记忆游戏。我有12个按钮和6个图像。 我想随机设置图像到按钮。一个图像到2个按钮。 我知道这个但是不知道如何在这里使用它。 编辑: 你用过这个吗 如何设置此图标的可见(假)之类的内容?

  • 我正在开发一个应用程序,在这个应用程序中,我以纵向方向拍摄照片,问题是当我稍后检索图像时,它是横向方向的(图片已逆时针旋转90度)。我曾经在课下使用过,但这里每次都是0(零)。所以,我不知道怎么解决它。

  • E/AndroidRuntime:致命异常:main java.lang.OutOfMemoryError位于Android.Graphics.BitmapFactor.NativeDecodeStream(原生方法)位于Android.Graphics.BitmapFactor.DecodeStream(BitmapFactor.JAVA:623)位于Android.Graphics.Bitma

  • 问题内容: 我正在学习如何使用mplot3d生成漂亮的3d数据图,到目前为止我很高兴。我现在想做的是旋转表面的动画效果。为此,我需要为3D投影设置相机位置。我猜这一定是有可能的,因为在交互使用matplotlib时,可以使用鼠标旋转表面。但是如何从脚本执行此操作?我在mpl_toolkits.mplot3d.proj3d中找到了很多转换,但是我找不到如何使用这些转换的目的,也没有找到任何尝试的示例

  • 我正在写一个定制的肖像相机,我在图像方向上有一些问题。具体而言,我需要始终将相机设置为纵向,并使用清单配置: Android:屏幕方向="肖像" 我在应用程序中设置: 这是有效的,因为当我旋转设备(90,180,270度)时,我可以看到(从相机预览)正确的图像。 我不在乎设备方向何时被锁定(禁用),但当它启用时,我似乎没有得到任何方向更改。我总是使用代码获得0度: 从技术上讲,我需要写入拍摄的照片

  • 问题内容: 我试图弄清楚如何调整图像的大小,以使其保持高宽比,但是要调整大小,直到图像的高度与包含div的高度匹配为止。我有这些非常大和很长的图像(屏幕截图),我想将它们放入200px宽,180px高的div中以进行显示,而无需手动调整图像的大小。为使外观更好看,图像的侧面需要溢出并用包含的div隐藏。这是我到目前为止的内容: HTML CSS 如您所见,图像父容器上显示灰色,根本不显示灰色。为了