【问题标题】:Image compression before uploading to FirebaseStorage上传到 FirebaseStorage 之前的图像压缩
【发布时间】:2023-03-19 01:02:01
【问题描述】:

我有一个应用程序,用户可以在其中创建事件并从他们的画廊上传图片并将其上传到 FirebaseStorage。一切正常,但图像的大小真的很大,我想优化加载/下载过程。我已经试过了:

// Get the data from an ImageView as bytes
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();

UploadTask uploadTask = mountainsRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
    // Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    // taskSnapshot.getMetadata() contains file metadata such as size,  content-type
    Uri downloadUrl = taskSnapshot.getDownloadUrl();
}
});

并设置bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos); 这行得通,但这基本上使每个图像都很难看。 如果可能的话,有没有办法在不完全破坏质量的情况下减小图像的大小?

【问题讨论】:

标签: java firebase firebase-storage


【解决方案1】:

对我有帮助的是我在转换后缩小图像分辨率 捕获的图像到 1280 x 720 然后它将我的文件大小降低了更多 50%。

这是一个示例方法。

   /**
     * Compress the file and return the compressed photo
     *
     * @return photoFile
     */
    public File PhotoCompressor(File PhotoFile) {
        Bitmap b = BitmapFactory.decodeFile(photoFile.getAbsolutePath());


        int originalWidth = b.getWidth();
        int originalHeight = b.getHeight();
        int boundWidth = COMPRESSED_PHOTO_WIDTH;
        int boundHeight = COMPRESSED_PHOTO_HEIGHT;
        int newWidth = originalWidth;
        int newHeight = originalHeight;

        //check if the image needs to be scale width
        if (originalWidth > boundWidth) {
            //scale width to fit
            newWidth = boundWidth;
            //scale height to maintain aspect ratio
            newHeight = (newWidth * originalHeight) / originalWidth;
        }

        //now check if we need to scale even the new height
        if (newHeight > boundHeight) {
            //scale height to fit instead
            newHeight = boundHeight;
            //scale width to maintain aspect ratio
            newWidth = (newHeight * originalWidth) / originalHeight;
        }
        Log.i(TAG, "Original Image:" + originalHeight + " x" + originalWidth);
        Log.i(TAG, "New Image:" + newHeight + " x" + newWidth);
        try {
            Bitmap out = Bitmap.createScaledBitmap(b, newWidth, newHeight, true);
            FileOutputStream fOut;
            fOut = new FileOutputStream(photoFile);
            out.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            fOut.flush();
            fOut.close();
            b.recycle();
            out.recycle();
        } catch (OutOfMemoryError exception) {
            Log.e(TAG, "OutofMemory excpetion" + exception);
            exception.printStackTrace();
        } catch (FileNotFoundException e) {
            Log.e(TAG, "File not found excpetion" + e);
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "IO exception excpetion" + e);
            e.printStackTrace();
        }
        return photoFile;
    }

【讨论】:

    猜你喜欢
    • 2021-01-03
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 2013-11-04
    相关资源
    最近更新 更多