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

Firebase存储图像被旋转

白才艺
2023-03-14

在我的应用中,我提示用户捕获图像。用户相机活动在按钮单击时开始,用户可以选择使用相机捕获图像。

此图像保存在用户手机上。然后将图像上传到firebase Storage,一切正常。来自一位使用三星Note 8的用户的反馈是,当图像以肖像模式拍摄时,他稍后会在应用程序中以风景模式显示。

我查看了firebase存储,发现图像以横向模式保存在存储中,即使用户以纵向模式捕获图像。

我想我必须将元数据传递到jpeg文件,该图像是在肖像模式下捕获的,以便firebase知道它实际上是肖像模式。

这是捕获图像的代码:

private void takePhoto() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {


            photoFile = createImageFile();


        } catch (IOException ex) {
            // Error occurred while creating the File

        }


        if (photoFile != null) {


            imageUri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);

        }
    }
}

这是CreateImageFile方法:

public File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyy.MM.dd_HH:mm:ss").format(new Date());

    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "MAP_NAME");

    if (!storageDir.exists()) {
    if (!storageDir.mkdir()) {
        return null;
    }

}

    File image = File.createTempFile(
            imageFileName,
            ".jpg",
            storageDir
    );


    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;


}

最后是用于将图像上传到firebase存储的代码:

private StorageTask mUploadTask;

private void uploadFile() {
    final String dateStamp = new SimpleDateFormat("dd MM").format(new Date());
    final String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date());

    if (mImageUri != null) {
        StorageReference fileReference = mStorageRef.child(System.currentTimeMillis() + "");

        mUploadTask = fileReference.putFile(mImageUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mProgressBar.setProgress(0);
                            }
                        }, 500);

稍后显示 firebase 存储中的图像时,我使用毕加索库,如下所示:

Picasso.with(mContext)
            .load(uploadCurrent.getImageUrl())
            .placeholder(R.mipmap.ic_launcher)
            .centerCrop()
            .fit()
            .into(holder.imageView);

有没有可能在毕加索的帮助下检测到旋转?

共有1个答案

郝永思
2023-03-14

我在使用谷歌示例拍照时也遇到了同样的问题,问题是当我通过滑动将路径设置为imageView时,它会以正确的方向显示,但在上传后,它会在Firebase存储中旋转。

因此,在<code>onActivityResult<code>中,我将路径设置为imageView,然后从路径中获取位图,旋转它并将其保存回路径,然后将文件上传到Firebase存储。

private Disposable disposable;

disposable = Completable
     .fromAction(this::handleImageOrientation)
     .subscribeOn(Schedulers.io())
     .observeOn(AndroidSchedulers.mainThread())
     .subscribe(this::uploadProfilePic);

@Override
public void onDestroy() {
    super.onDestroy();
    if (disposable != null) {
        disposable.dispose();
        disposable = null;
    }
}

如果您不使用RxRava

new Thread() {
     @Override
     public void run() {
         super.run();
         handleImageOrientation();
         getActivity().runOnUiThread(() -> uploadProfilePic());
     }
}.start();

我使用的是android.support.media中的<code>。退出支持库中的接口,因此退出android.media中的接口。ExifInterface实现在旧版本的Android中存在一些已知的安全漏洞。

build.gradle

implementation "com.android.support:exifinterface:27.1.1"

handleImageOrientation()

private void handleImageOrientation() {
        Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath);
        Bitmap rotatedBitmap;
        try {
            ExifInterface ei = new ExifInterface(currentPhotoPath);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotatedBitmap = Utils.rotateImage(bitmap, 90);
                    break;

                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotatedBitmap = Utils.rotateImage(bitmap, 180);
                    break;

                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotatedBitmap = Utils.rotateImage(bitmap, 270);
                    break;

                case ExifInterface.ORIENTATION_NORMAL:
                default:
                    rotatedBitmap = bitmap;
            }
         if (rotatedBitmap != bitmap) {
            FileOutputStream fOut = new FileOutputStream(currentPhotoPath);
            rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
        }
            bitmap.recycle();
            rotatedBitmap.recycle();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Utils.rotateImage()

public static Bitmap rotateImage(Bitmap source, float angle) {
     Matrix matrix = new Matrix();
     matrix.postRotate(angle);
     return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
 类似资料:
  • 我正在开发一款用于拍照的Android应用程序。首先,用户拍摄个人资料图片。此图像的Url保存在Firebase实时数据库中。用于检索以下下载URL的示例代码 这个url被保存在数据库中的多个地方,我需要在那里加载用户的个人资料图片——他的每个帖子的条目,他给应用程序上其他人的消息,等等。 现在,如果他以后更改个人资料图片,我有以下两个选项 更新每个数据库条目-非常繁琐、暴力和资源密集 那么,有没

  • 我正在尝试让Firebase存储与Imgix或cloudinary这样的图像服务一起工作。然而,Firebase提供的下载URL似乎不能与这些服务一起工作。 例如:Cloudinary说您可以获取如下所示的图像: http://res.cloudinary.com/demo/image/fetch/http://upload.wikimedia.org/wikipedia/commons/0/0C

  • 我从Firebase存储中获取图像,用相机拍照,或者从图书馆中挑选一张。当其中一个完成时,我有一个类来存储,以便在需要时使用它。 现在我需要将此图像上传到Firebase存储(修改或新建,无所谓)。Firebase允许我使用以下选项之一:或,每个选项分别需要或。 我怎么能把我的,并从它获得一个或用于上传? -- 或者,为了解决这个问题,当我从Firebase存储中检索图像时,如何获取图像的? 无论

  • 下面是我得到的控制台输出和错误: 下面是我按下Get Images按钮后的控制台输出,您可以看到图像的url为:

  • 问题内容: 在我的应用程序中,我在div中有一个图像,一个按钮。 我想旋转显示的图像,并在使用jquery单击按钮时保存旋转的图像。 我已经使用了代码: http://code.google.com/p/jquery-rotate/ 和jQuery代码: html代码: 当我使用上述代码时,有两个图像,一个是旧图像,另一个是旋转图像。 在这里我想旋转相同的图像并仅显示旋转的图像。并将旋转的图像保存