Android的 - 降低图像文件的大小图像文件、大小、Android

2023-09-04 02:51:28 作者:人生何处不相逢

我有一个URI图像文件,我想减少它的大小将其上传。从最初的移动图像文件的大小取决于移动(可2MB一样可以500KB),但我想最终大小约为200KB,这样我就可以把它上传。 从我读,我有(至少)2个选择:

I have an URI image file, and I want to reduce its size to upload it. Initial image file size depends from mobile to mobile (can be 2MB as can be 500KB), but I want final size to be about 200KB, so that I can upload it. From what I read, I have (at least) 2 choices:

使用的 BitmapFactory.Options.inSampleSize 的,子采样原始图像,并得到一个较小的图像; 使用的 Bitmap.com preSS 的对COM preSS图象指定COM pression质量。 Using BitmapFactory.Options.inSampleSize, to subsample original image and get a smaller image; Using Bitmap.compress to compress the image specifying compression quality.

什么是最好的选择吗?

我想最初调整图像的宽度/高度,直到宽度或高度超过1000px(像1024x768或其他),那么COM preSS图像质量下降,直到文件大小超过200KB。这里有一个例子:

I was thinking to initially resize image width/height until width or height is above 1000px (something like 1024x768 or others), then compress image with decreasing quality until file size is above 200KB. Here's an example:

int MAX_IMAGE_SIZE = 200 * 1024; // max final file size
Bitmap bmpPic = BitmapFactory.decodeFile(fileUri.getPath());
if ((bmpPic.getWidth() >= 1024) && (bmpPic.getHeight() >= 1024)) {
    BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
    bmpOptions.inSampleSize = 1;
    while ((bmpPic.getWidth() >= 1024) && (bmpPic.getHeight() >= 1024)) {
        bmpOptions.inSampleSize++;
        bmpPic = BitmapFactory.decodeFile(fileUri.getPath(), bmpOptions);
    }
    Log.d(TAG, "Resize: " + bmpOptions.inSampleSize);
}
int compressQuality = 104; // quality decreasing by 5 every loop. (start from 99)
int streamLength = MAX_IMAGE_SIZE;
while (streamLength >= MAX_IMAGE_SIZE) {
    ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
    compressQuality -= 5;
    Log.d(TAG, "Quality: " + compressQuality);
    bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
    byte[] bmpPicByteArray = bmpStream.toByteArray();
    streamLength = bmpPicByteArray.length;
    Log.d(TAG, "Size: " + streamLength);
}
try {
    FileOutputStream bmpFile = new FileOutputStream(finalPath);
    bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpFile);
    bmpFile.flush();
    bmpFile.close();
} catch (Exception e) {
    Log.e(TAG, "Error on saving file");
}

有没有更好的办法做到这一点?我应该尽量保持使用所有2种方法或只有一个?谢谢

Is there a better way to do it? Should I try to keep using all 2 methods or only one? Thanks

推荐答案

使用 Bitmap.com preSS()您只需指定COM pression算法顺便说一句COM pression操作所花费的时间相当大的量。如果您需要的尺寸,以发挥减少内存分配你的形象,你到底需要使用使用图像的缩放 Bitmap.Options ,起初,然后计算位图界限解码到您指定的大小。

Using Bitmap.compress() you just specify compression algorithm and by the way compression operation takes rather big amount of time. If you need to play with sizes for reducing memory allocation for your image, you exactly need to use scaling of your image using Bitmap.Options, computing bitmap bounds at first and then decoding it to your specified size.

这是我发现的StackOverflow的最好样本这个。

The best sample that I found on StackOverflow is this one.

 
精彩推荐
图片推荐