【问题标题】:Image rotates after bitmap.compress图像在 bitmap.compress 后旋转
【发布时间】:2018-12-20 23:22:16
【问题描述】:

所以我试图让用户从他们的照片库中选择一张图片,然后将其上传到 Firebase 存储......到目前为止,我的代码如下所示:

@Override
protected void onActivityResult(int requestCode,int resultcode, Intent data){
    super.onActivityResult(requestCode,resultcode,data);

    if(requestCode == REQUEST_IMAGE_CAPTURE && resultcode == RESULT_OK) {
        final Uri uri = data.getData();

        if (uri != null){
            Bitmap bmp = null;
            try {

                bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

            } catch (IOException e){
                e.printStackTrace();
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 25, baos);



            byte[] fileInBytes = baos.toByteArray();
            bmp.recycle();
            FirebaseAuth auth = FirebaseAuth.getInstance();
            FirebaseStorage storage = FirebaseStorage.getInstance();
            String imageString = "";
            if(clickPhoto ==1){imageString = "Image 1"; }
            else if(clickPhoto ==2){imageString = "Image 2"; }
            else if (clickPhoto ==3){imageString = "Image 3"; }
            else if (clickPhoto ==4){imageString = "Image 4"; }
            final StorageReference storageReference = storage.getReference().child("Images").child("users").child(auth.getCurrentUser().getUid()).child(imageString);
            storageReference.putBytes(fileInBytes).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            String downloadUrl = uri.toString();
                            String urlString ="";
                            if(clickPhoto ==1){ urlString = "photo1URI";}
                            if(clickPhoto ==2){ urlString = "photo2URI";}
                            else if (clickPhoto ==3){ urlString = "photo3URI";}
                            else if (clickPhoto ==4){urlString = "photo4URI";}

                            toMap.put(urlString, downloadUrl);
                            updatedb();
                            toMap.clear();
                            Log.d("uriii",downloadUrl);}


                    });
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d("UploadFailure",e.getMessage());
                }
            });




        }
        else {
            Toast.makeText(this,"Didn't work",Toast.LENGTH_LONG).show();
        }
    }
}

我的问题是上传的图像改变了方向......似乎是随机的......我如何保持方向?我查看了 Exif 界面,但看不到将其作为 ByteArrayOutputStream 上传的答案?

任何帮助将不胜感激。

【问题讨论】:

  • 它并没有解决我的问题,因为我仍然无法将 exif 接口应用于 bmp.compress(Bitmap.CompressFormat, quality, output) 方法?
  • 查看 Manuel 的答案,了解如何将旋转应用于位图 stackoverflow.com/questions/3647993/…
  • 这不是将其应用于位图的情况,位图使用 bmp.compress 压缩,然后在上传到 firebase 之前输出到 ByteArrayOutputStream ....所以我需要将旋转应用于上传前的字节数组输出?

标签: java android exif


【解决方案1】:

问题是图像中的 EXIF 信息(包括方向)在压缩后被删除。您需要将 EXIF 信息从原始图像复制到压缩图像。解决方案解释here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多