Bitmap.compress不会减少字节数

时间:2017-07-03 17:04:48

标签: android bitmap compression image-compression

我需要压缩图像才能将其发送到我的服务器。我试着这样做:

(
        {
        bookingTime = "<null>";
        clubAddress = "Grand by GRT Hotels, Lobby Level, Sir Thyagaraya Road, Thyagaraya Nagar , Chennai";
        clubCity = "Chennai,India";
        clubFacilities = "<null>";
        clubInfoID = 270;
        clubLatitude = "13.0402612";
        clubLoc = Chennai;
        clubLongitude = "80.2423";
        clubMobileNumber = "<null>";
        clubName = "The Code-Grand by GRT Hotels";
        clubType = "Night club";
        clubberMobile = "<null>";
        clubberName = "<null>";
        count = 0;
        coupleCover = 0;
        coupleEntry = 0;
        couplePayment = "<null>";
        description = "<null>";
        distance = 0;
        djId = "<null>";
        dressCode = "<null>";
        endDate = "2017-07-03 18:00:00";
        entryCancellationTime = "<null>";
        eventOffer = "<null>";
        eventType = DANCE;
        favoriteType = NO;
        festivalList = "<null>";
        "idclub_event" = 6042;
        moodDJ = "<null>";
        moodMusic = "Salsa, Bachata, Kizomba";
        moodTheme = "LATIN CODE NIGHT";
        offerCredits = 0;
        paidStatus = NOTPAID;
        personAllowed = 0;
        pictureType = IMAGE;
        rateAmbience = 0;
        rateAvg = 0;
        rateFood = 0;
        rateMusic = 0;
        rateService = 0;
        rsvpEntryTime = "<null>";
        rsvpRequestTime = "<null>";
        rsvpStatus = NO;
        rsvpType = BOTH;
        stagCover = 0;
        stagEntry = 0;
        stagPayment = "<null>";
        startDate = "2017-07-03 15:30:00";
        stopEdit = "<null>";
    },
        {
        bookingTime = "<null>";
        clubAddress = "309 Rajiv Gandhi Salai (OMR), Sholinganallur, Chennai, Tamil Nadu 600119";
        clubCity = "Chennai,India";
        clubFacilities = "<null>";
        clubInfoID = 276;
        clubLatitude = "12.9056392";
        clubLoc = Sholinganallur;
        clubLongitude = "80.2256728";
        clubMobileNumber = "<null>";
        clubName = "The Gateway Hotel";
        clubType = Pub;
        clubberMobile = "<null>";
        clubberName = "<null>";
        count = 0;
        coupleCover = 0;
        coupleEntry = 0;
        couplePayment = "<null>";
        description = "<null>";
        distance = 0;
        djId = "<null>";
        dressCode = "<null>";
        endDate = "2017-09-23 16:00:00";
        entryCancellationTime = "<null>";
        eventOffer = "<null>";
        eventType = DANCE;
        favoriteType = NO;
        festivalList = "<null>";
        "idclub_event" = 5945;
        moodDJ = "<null>";
        moodMusic = "Bachata ";
        moodTheme = "Toke D Keda: Bachata Concert";
        offerCredits = 0;
        paidStatus = NOTPAID;
        personAllowed = 0;
        pictureType = IMAGE;
        rateAmbience = 0;
        rateAvg = 0;
        rateFood = 0;
        rateMusic = 0;
        rateService = 0;
        rsvpEntryTime = "<null>";
        rsvpRequestTime = "<null>";
        rsvpStatus = NO;
        rsvpType = BOTH;
        stagCover = 0;
        stagEntry = 0;
        stagPayment = "<null>";
        startDate = "2017-09-23 10:00:00";
        stopEdit = "<null>";
    },

但是当我比较原始Bitmap对象和压缩对象的字节数时,我得到相同的数字:

private Bitmap compressImage(Bitmap bitmapImg){
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmapImg.compress(Bitmap.CompressFormat.JPEG, 50, out);
    Bitmap compressed = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

    return compressed;
}

如何修复此文件以获得较小的文件?

2 个答案:

答案 0 :(得分:4)

  

但是当我比较原始Bitmap对象和压缩对象的字节数时,我得到相同的数字:

内存中Bitmap的大小仅基于 的分辨率(宽度和高度,以像素为单位)和位深度(每个像素的字节数,用于控制多少种颜色)可以按像素使用。)

  

如何修复此文件以获得较小的文件?

您没有文件。内存中有一个Bitmap对象。图像文件通常以压缩形式存储。特别是,JPEG,PNG,WebP和GIF(Android中使用的四种主要图像格式)都是如此。因此,例如,out.toByteArray()将小于23,970,816字节。

此外,您没有向服务器发送Bitmap。您正在将图像发送到服务器。您需要阅读服务器的文档,或与服务器开发人员交谈,以确定它们支持的图像格式以及如何将图像发送到服务器(理想情况下,像HTTP PUT一样高效)。

如果您想减少Bitmap的内存大小,请将其缩放为较低分辨率的图像(例如,通过createScaledBitmap())。

答案 1 :(得分:1)

您可以将位图格式从RGB_565更改为ARGB_8888。这会将位图的内存占用减少一半,但也会导致质量下降。不幸的是,这是你用Bitmap做的最多的事情。

话虽如此,您使用的压缩方法在大多数情况下都可以正常工作。它也是许多平台的倡导方法。 Firebase的一个示例是this