处理Base64字符串图像太慢?

时间:2018-07-20 11:23:51

标签: android bitmap size photo

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

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

2 个答案:

答案 0 :(得分:1)

您可以调整图像位图的大小。可以用来缩小图片大小

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);
    }

我希望这对您有帮助

答案 1 :(得分:0)

您必须更改实现方法,这是我的建议。

在您的代码库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);

}