【问题标题】:compress image file from camera to certain size将相机中的图像文件压缩到一定大小
【发布时间】:2015-04-29 22:06:47
【问题描述】:

我正在尝试压缩保存在文件中的图像。我正在尝试将文件压缩为 1MB。我尝试了几种方法,但通常会产生 OutofMemoryError。 然后我尝试使用此解决方案,但它使位图空白。

How to compress bitmap from 10mb image from camera to 300kb beforw setting to imageview in android

这是我的代码:

    System.gc();
    getActivity().getContentResolver().notifyChange(mImageTempUri, null);
    Bitmap bitmap;
    bitmap = BitmapFactory.decodeFile(mImageDirectory + mImageName, options);
    if(bitmap == null){
    howRequestFailedErrorMessage("Gambar gagal di-upload");
    return;

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();   


    bitmap.compress(Bitmap.CompressFormat.JPEG, 25, bytes);
    File f = new File(mImageDirectory + mImageName);
    if(f.exists()){
        f.delete();
    }
    FileOutputStream fo;

    try {
        fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.flush();
        fo.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    bitmap.recycle();

【问题讨论】:

  • 你试过直接把压缩数据放到fileOutputStream吗?您创建了两组 jpeg 图像数据,一组显式在 ByteArrayOutputStream 中,另一组临时在 bytes.toBytesArray 中。我猜这些数据可能会导致“内存不足”错误。您可以通过代码“bitmap.compress(Bitmap.CompressFormat.JPEG, 25, fo);”避免这些数据
  • @Fumu7 我不太明白,你能给出代码中的例子吗?实际上现在内存不足并不是真正的问题,主要问题是我无法动态压缩图像,我必须静态调整质量(现在是 25)

标签: android memory-management bitmap android-bitmap


【解决方案1】:

科特林方式

        if (file.length() > MAX_IMAGE_SIZE) {
            var streamLength = MAX_IMAGE_SIZE
            var compressQuality = 105
            val bmpStream = ByteArrayOutputStream()
            while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
                bmpStream.use {
                    it.flush()
                    it.reset()
                }

                compressQuality -= 5
                val bitmap = BitmapFactory.decodeFile(file.absolutePath, BitmapFactory.Options())
                bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream)
                val bmpPicByteArray = bmpStream.toByteArray()
                streamLength = bmpPicByteArray.size
                if (BuildConfig.DEBUG) {
                    Log.d("test upload", "Quality: $compressQuality")
                    Log.d("test upload", "Size: $streamLength")
                }
            }

            FileOutputStream(file).use {
                it.write(bmpStream.toByteArray())
            }
        }

常数

companion object {
    //2000 * 1024 = 2 MB
    private const val MAX_IMAGE_SIZE = 2048000
}

【讨论】:

    【解决方案2】:

    好的,我有自己的答案

        File f = new File(mImageDirectory + mImageName);
        if(f.exists()){
            f.delete();
        }
    
        int MAX_IMAGE_SIZE = 1000 * 1024;
        int streamLength = MAX_IMAGE_SIZE;
        int compressQuality = 105;
        ByteArrayOutputStream bmpStream = new ByteArrayOutputStream();
        while (streamLength >= MAX_IMAGE_SIZE && compressQuality > 5) {
            try {
                bmpStream.flush();//to avoid out of memory error
                bmpStream.reset();
            } catch (IOException e) {
                e.printStackTrace();
            }
            compressQuality -= 5;
            bitmap.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream);
            byte[] bmpPicByteArray = bmpStream.toByteArray();
            streamLength = bmpPicByteArray.length;
            if(BuildConfig.DEBUG) {
                Log.d("test upload", "Quality: " + compressQuality);
                Log.d("test upload", "Size: " + streamLength);
            }
        }
    
        FileOutputStream fo;
    
        try {
            fo = new FileOutputStream(f);
            fo.write(bmpStream.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    【讨论】:

    • 你可以在不创建字节[]的情况下检查大小,只需获取 bmpStream.size() mBitmapImage.compress(Bitmap.CompressFormat.JPEG, percentDecompression, outputStream); actualSize = outputStream.size();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 2017-04-24
    • 1970-01-01
    相关资源
    最近更新 更多