Android Question I want to compress the image and then convert it to binary. How can I modify the “ReadFile” function?

bskotu555

Active Member
B4X:
Private Sub Button1_Click
    Dim CC As ContentChooser
    CC.Initialize("cc")
    CC.Show("image/*", "Choose image")
    Wait For CC_Result (Success As Boolean, Dir As String, FileName As String)
    If Success Then
        'ImageView1.Bitmap = LoadBitmap(Dir, FileName)
        Dim bmp As B4XBitmap = LoadBitmap(Dir, FileName)
        ImageView1.Bitmap = ResizeBitmapMaxByte(bmp, 600000)
        ar=ReadFile(Dir, FileName)  ‘??? 
    End If
End Sub

Sub ReadFile(Dir As String, FileName As String) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(100) 'size not really important
    File.Copy2(File.OpenInput(Dir, FileName), out)
    Return out.ToBytesArray
End Sub
 

teddybear

Well-Known Member
Licensed User
Except for the BMP extension, files with the JPG PNG extension are already compressed
Why do you want to convert(LoadBitmap) them to BMP and then compress them?
why not use File.ReadBytes to read directly the files?

 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Longtime User
files with the JPG PNG extension are already compressed

Sometimes people on this forum use the word "compressed" as in to make smaller, by rescaling.

Eg if you take a 6 megapixel JPEG image which is 3000 x 2000 pixels, and resize it to 1500 x 1000 pixels (still 4:3 aspect ratio, just half as many pixels in each direction) and then resave it as a JPEQ with same quality, then you'll have made it significantly smaller.

If an image is downloaded from the internet and only going to be used on the phone at a fixed size (eg background imagery, or images in a catalogue) then the images could be resized (shrunk, reduced, compressed) to that fixed size and it doesn't matter about the lost detail because it won't be visible at the fixed size anyway.
 
Upvote 0

bskotu555

Active Member
Except for the BMP extension, files with the JPG PNG extension are already compressed
Why do you want to convert(LoadBitmap) them to BMP and then compress them?
why not use File.ReadBytes to read directly the files?

Sometimes the image volume is too large, and uploading the database is too big. I need to compress it before uploading
 
Upvote 0

bskotu555

Active Member
That's right, that's what it means. If it's distorted, it's okay
Sometimes people on this forum use the word "compressed" as in to make smaller, by rescaling.

Eg if you take a 6 megapixel JPEG image which is 3000 x 2000 pixels, and resize it to 1500 x 1000 pixels (still 4:3 aspect ratio, just half as many pixels in each direction) and then resave it as a JPEQ with same quality, then you'll have made it significantly smaller.

If an image is downloaded from the internet and only going to be used on the phone at a fixed size (eg background imagery, or images in a catalogue) then the images could be resized (shrunk, reduced, compressed) to that fixed size and it doesn't matter about the lost detail because it won't be visible at the fixed size anyway.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
That's right, that's what it means. If it's distorted, it's okay

There is a "load image file" function that reduces it on the way in. It might be restricted to integral reductions eg half, third, quarter size (which reduces number of pixels to eg 1/4th, 1/9th, 1/16th of original, and file size by similar ratio). This is for Android, right?

https://www.b4x.com/android/forum/threads/what-loadbitmapsample-does-exactly.62065/

The thread indicates that it reduces it by powers of 2 eg half, quarter, eighth. Which would reduce the number of pixels down to a quarter, sixteenth, sixty-fourth of the original, and the subsequent memory and file size by similar ratio.

 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Longtime User
uploading the database

If you are saving images in a database as a BLOB ie Byte() then this post is an example of writing image to database and reading it back again:


Sometimes people like to be able to see the data, like perhaps they're use a separate database tool to examine the database when debugging, and so they convert the byte array to a hex string and store it in the database as a regular string.

Which is no problem, other than the hex string uses two bytes (characters) to represent each raw byte of data, and so it takes twice as much disk space compared to if it was stored directly as a Binary Large OBject.
 
Upvote 0
Top