【问题标题】:Android image captured stored into server is rotated when retrieved存储到服务器中的 Android 图像在检索时旋转
【发布时间】:2015-11-02 15:32:47
【问题描述】:

在我的代码中,我允许用户从图库或相机上传照片。然后通过改造将图像存储到服务器中。如果照片是使用手机的相机拍摄的(无论是通过应用程序调用还是通过相机直接调用),检索时照片始终旋转 90 度。如果图像不是使用相机拍摄的,则方向是正确的。我该如何解决这个问题?

我知道我是否尝试直接显示图像而不将其存储在服务器中,并且可能处于正确的方向,因为我可以在显示之前旋转它。我正在做类似于这里的代码的事情:https://gist.github.com/Mariovc/f06e70ebe8ca52fbbbe2

但是因为我需要上传到服务器,然后再次从服务器检索。在存储到服务器之前如何旋转它,以便在检索它时它已经处于正确的方向?

以下是我的代码的一些部分(只是一些通常的图像处理,并且在服务完成图像下载后调用 displayAvatarInProfile()):

public void displayAvatarInProfile(String filePath) {
    if(filePath != null && filePath != ""){
        int targetW = mProfilePicImageView.getWidth();
        int targetH = mProfilePicImageView.getHeight();
        Bitmap bm = ImageStorageUtils.resizePic(filePath, targetW, targetH);
        mProfilePicImageView.setImageBitmap(bm);
    } else {
        mProfilePicImageView.setImageBitmap(null);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == Activity.RESULT_OK) {
        Uri fileUri = null;
        String filePath = null;
        switch(requestCode) {
            case REQUEST_PICK_IMAGE: 
                fileUri = data.getData();
                break;
            case REQUEST_CAPTURE_IMAGE:
                File imageFile = ImageStorageUtils.getFile(
                        getActivity().getResources().getString(R.string.ApptTitle), 
                        "avatar_" + mUser.getLogin());
                filePath = imageFile.getAbsolutePath();
                fileUri = ImageStorageUtils.getUriFromFilePath(mContext.get(), filePath);
                break;
            default:
                break;
        }
        if(fileUri != null) {
            getActivity().startService(ProfileService.makeIntentUploadAvatar(
                    mContext.get(), mUser.getId(), fileUri));
        }
    }
  }


public void pickImage() {
    final Intent imageGalleryIntent = 
        new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI)
            .setType("image/*")
            .putExtra(Intent.EXTRA_LOCAL_ONLY, true);

    // Verify the intent will resolve to an Activity.
    if (imageGalleryIntent.resolveActivity(getActivity().getPackageManager()) != null) 
        // Start an Activity to get the Image from the Image Gallery
        startActivityForResult(imageGalleryIntent,
            DisplayProfileFragment.REQUEST_PICK_IMAGE);

}

public void captureImage() {
    Uri captureImageUri = null;
    try {
        captureImageUri = Uri.fromFile(ImageStorageUtils.createImageFile(
                mContext.get(),
                getActivity().getResources().getString(R.string.ApptTitle), 
                "avatar_" + mUser.getLogin()));
    } catch (IOException e) {
        e.printStackTrace();
    }  

    // Create an intent that will start an Activity to get
    // capture an image.
    final Intent captureImageIntent =
            new Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            .putExtra(MediaStore.EXTRA_OUTPUT, captureImageUri);

    // Verify the intent will resolve to an Activity.
    if (captureImageIntent.resolveActivity(getActivity().getPackageManager()) != null) 
        // Start an Activity to capture an image
        startActivityForResult(captureImageIntent,
            DisplayProfileFragment.REQUEST_CAPTURE_IMAGE);
}

更新 1

有人评论说我需要更多描述:

当我上传文件时,我会这样做:

boolean succeeded = mImpatientServiceProxy.uploadAvatar(userId, new TypedFile("image/jpg", imageFile)); 

下载时,我得到一个 retrofit.client.Response 对象,然后我从响应中获取 InputStream 并使用输出流将数据写入文件。

final InputStream inputStream = response.getBody().in();
final OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(inputStream, outputStream);

更新 2

这不是现有解决方案的重复,因为用户可以从图库上传,您不知道它是从在线资源还是相机拍摄的照片,因此您在显示时无法旋转图像。

【问题讨论】:

  • 我们看不到您上传的内容。一份文件?位图?我们也看不到您下载的内容和下载方式。
  • 嗨@greenapps 请参考我上面的更新
  • why image captured using camera intent gets rotated on some devices in android 的可能副本。你应该打开onActivityResult()收到的Jpeg文件,随意旋转,然后再次压缩成Jpeg。
  • 抱歉,看到一个电话我看不到它是如何完成的。
  • @AlexCohn 哦...我想我没有仔细查看您发送的页面。因为那个时候我认为这不是同一个问题。是的,它对我有用。但是除了从 ExifInterface 中找出它的旋转之外,我还必须使用我自己的位图调整大小函数。谢谢!

标签: android image rotation android-camera


【解决方案1】:

使用相机 api 而不是意图。当您使用意图捕获图像时,它会显示旋转。在相机 api 中,您可以通过一些方法来更改相机的旋转以及捕获的照片。

【讨论】:

  • 相机 api 而不是意图是什么意思?你可以发布一些代码或链接吗?因为我在网上搜了一下,camera api相关代码也用到了intent
  • www.tutorialspoint.com/android/android_camera.htm 看看这个。你会明白我想说的一切......
  • 你看到那个链接的最后一个例子了吗?我想你已经在他使用意图的链接中看到了这个例子,只需向下滚动并查看他显示相机 api 的最后一个例子
  • 是的,如下所示。但是...如果我必须使用意图,还有其他方法吗?
  • 我认为没有其他方法。 Becoz 一旦我也遇到了这个问题,然后我通过使用相机 api 解决了这个问题。所以如果你使用相机 api 会更好,因为它有很多功能可以帮助你完成任务。
猜你喜欢
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 2023-03-25
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 2015-06-29
  • 2019-07-31
相关资源
最近更新 更多