【问题标题】:Too slow handling Base64 String image?处理 Base64 字符串图像太慢?
【发布时间】:2018-12-28 16:49:53
【问题描述】:

我从服务器获取字符串变量中的图像,我需要在移动设备中减少并重新上传。我有下一个功能,但我不知道为什么要花这么多时间,我真的不知道它是否在某个时候被阻止/冻结,或者这个过程需要这么多时间。

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
    {
        ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
        image.compress(compressFormat, quality, byteArrayOS);
        return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
    }

    public static Bitmap decodeBase64(String input)
    {
        byte[] decodedBytes = Base64.decode(input, Base64.DEFAULT);
        return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
    }

    public static String compressImage(String base64ImageData){

        long SIZE_1_5_MB = 1572864;
        int QUALITY = 100;

        Bitmap decodedByte = decodeBase64(base64ImageData);

        StringBuilder sb = new StringBuilder();

        do{
            sb.setLength(0);
            sb = new StringBuilder();
            String eBase64 = encodeToBase64(decodedByte, Bitmap.CompressFormat.JPEG, QUALITY);
            sb.append(eBase64);
            Log.e("IMAGE", "QUALITY: " + QUALITY + " SIZE: " + sb.length());
            QUALITY = QUALITY - 5;
        }while (sb.length() > SIZE_1_5_MB);

        return sb.toString();

    }

我想将照片缩小到小于 1.5 mb

【问题讨论】:

    标签: android bitmap size photo


    【解决方案1】:

    你必须改变你的实现方法,这是我的建议。

    在您的代码库中,每次循环中都会发生 64 位编码和解码,这会导致速度变慢。相反,从源代码读取时使用一次 base 64 解码,最后只编码一次。

    您可以通过简单的数学计算确定 base 64 字符串的大致大小。因为它会大 1.33 倍。

    这是你修改后的代码。

    public static String compressImage(String base64ImageData){
    
        long SIZE_1_5_MB = 1572864;
        int QUALITY = 100;
    
        Bitmap bitmap = decodeBase64(base64ImageData);
    
        //        long currentImageLenghtIn100Percent = decodedByte.getByteCount()         / 12; //approxvalue, It vary based on device arch and image format
        //        { //Calculate actual size in 100 percent compressed format
        //            ByteArrayOutputStream stream = new ByteArrayOutputStream();
        //            decodedByte.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        //            currentImageLenghtIn100Percent = stream.toByteArray().length;
        //        }
        //
        //        //If you are targed teh base 64 string to be limited to 1.5 mb then convert size to base 64
        //        currentImageLenghtIn100Percent =  (long) ((currentImageLenghtIn100Percent * 4.0)/3.0f);
        //
        //        //If possible try calculate the compress ratio, it will avoid the do while loop
        //        double compressRatio = ((double) currentImageLenghtIn100Percent)/((double) SIZE_1_5_MB);
    
        ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    
        do{
            bitmap.compress(Bitmap.CompressFormat.JPEG, QUALITY, byteArrayOS);
    
            Log.e("IMAGE", "QUALITY: " + QUALITY + " SIZE of Image: " + byteArrayOS.toByteArray().length + " ~Size of Base 64" + byteArrayOS.toByteArray().length * 4 /3);
            QUALITY = QUALITY - 5;
        }while ((byteArrayOS.toByteArray().length * 4 /3) > SIZE_1_5_MB); //~ base 64 is 1.33 times larger than actual size
    
        return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
    
    }
    

    【讨论】:

      【解决方案2】:

      您可以调整图像位图的大小。它可以用来减小图像的大小

      public Bitmap getResizeBitmap(Bitmap bm, int newWidth, int newHeight) {
              int width = bm.getWidth();
              int height = bm.getHeight();
              float scaleWidth = ((float) newWidth) / width;
              float scaleHeight = ((float) newHeight) / height;
              // CREATE A MATRIX FOR THE MANIPULATION
              Matrix matrix = new Matrix();
              // RESIZE THE BIT MAP
              matrix.postScale(scaleWidth, scaleHeight);
      
              // "RECREATE" THE NEW BITMAP
              return Bitmap.createBitmap(
                      bm, 0, 0, width, height, matrix, false);
          }
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2019-04-11
        • 2018-04-30
        • 1970-01-01
        • 1970-01-01
        • 2019-01-27
        • 1970-01-01
        • 1970-01-01
        • 2013-09-20
        • 2014-10-18
        相关资源
        最近更新 更多