Android Question Bitmap WriteToStream

LucaMs

Expert
Licensed User
Longtime User
I'm trying to "lighten the weight" of a bitmap.

Initially I used RSImageProcessing but it seems to save a wrong bitmap... sometimes :eek: (I think it does not accept bitmaps with much depth).

Then I used WriteToStream; if I save in JPEG format, decreasing the quality, it seems to increase the "weight" for the bitmap :eek:.

"Solved" saving as PNG.

Code I used:
B4X:
Sub Compress(Bmp As Bitmap, Quality As Int) As Bitmap
   Dim FileName As String = "temp.png"
  
   Dim OStream As OutputStream
   OStream = File.OpenOutput(File.DirInternal, FileName, False)

'   mRSImageProcessing.compress(Bmp, "JPEG", Quality, OStream)
   Bmp.WriteToStream(OStream, Quality, "PNG")
  
   Dim IStream As InputStream
   IStream = File.OpenInput(File.DirInternal, FileName)
   Dim BmpCompressed As Bitmap
   BmpCompressed.Initialize2(IStream)
  
   File.Delete(File.DirInternal, FileName)

   Return BmpCompressed
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code should never be used. A short explanation:

1. You are not closing the output stream so the file is likely to become corrupted.
2. You should never use Bitmap.Initialize2 unless you don't have any other option.
3. The code doesn't do anything useful. If you want to decrease the memory usage then use LoadBitmapResize. Once a bitmap is loaded its memory usage will be Width * Height * 4. The source file format doesn't matter.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
but what I need is to obtain a new bitmap with the same width & height of the original one but which needs less memory.
There is no such thing.

Once a bitmap is loaded its memory usage will be ~ Width * Height * 4. The source file format doesn't matter.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Uhm...

My app allows the user to select part (or all) of a his image. The selection size is fixed, 20%y (landscape) for both dimensions (w & h).

The selection (a bitmap) on my device (1920x1080 - scale 2.65) is too "heavy" (I create a Base64 string from it and I need it is less than 128KB - as you know, the websocket issue).

So I don't know how to use LoadBitmapResize in this situation (the activity which allows the user to select the image - or part of it - must show a square with the size I wrote above and that will be shown in the game - the bitmap will be used as avatar of players).
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
This thread is a good example of how not to post a question. My answers would have been completely different if I understood that you are trying to generate a smaller base64 string from a bitmap. I cannot help you here. Sorry.
I don't think so, because the base64 string size will be proportionate to the bitmap size and to an image file size.

I mean, of course, that a "light" bitmap will "produce" a light base64 string.

I did not need help with creating a base64 string but, as the title and the first post, a way to lighten the weight of a bitmap. In my first post I used the PNG format and WriteToStream to decrease the quality but if works only with jpg (I found that also PC tools work so).

However, it is not possible to do what I need because if I want to use websockets , the text message can not exceed 128KB and a 127KB Base64 string can only represent very small and little detailed images (an image file that on PC occupies about 19KB, 154x200 px, as Base64 string would exceed 240KB).
 
Upvote 0
Top