About bitmap array

doraneko

Member
Licensed User
Longtime User
I want to read or write a file to keep bitmap array.
Not each jpeg files, but a single file consists of multiple bitmap data.
Each bitmaps are same Height and Width.
Should I use RandomAccessFile or Input/OutputStream ?
Please teach me.:sign0085:
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
You cannot directly write an array of bitmaps to a file.
You can do something like:
- Create a list.
- Convert each bitmap to a bytes array by writing it to an OutputStream that was initialized with OutputStream.InitializeToBytesArray, and add the bytes array to the list (as single item).
- Save the whole list with RandomAccessFile.WriteObject.

To read the images:
- Read the whole list with RandomAccessFile.ReadObject.
- Convert each bytes array to a bitmap by first creating an InputStream with InputStream.InitializeFromBytesArray and then using Bitmap.Initialize2.
 
Upvote 0

doraneko

Member
Licensed User
Longtime User
Thank you, Erel.

I can understand about 'write procedure', but can not about 'read procedure'.
Especially, Please teach me how to convert List into InputStream.
And, if I use 'PNG' with WriteToStream and 'Compress' with WriteObject in 'write procedure', Can I reconstruct in 'read procedure' ?
 
Upvote 0

doraneko

Member
Licensed User
Longtime User
For Erel.

You said
To read the images:
- Read the whole list with RandomAccessFile.ReadObject.
- Convert each bytes array to a bitmap by first creating an InputStream with InputStream.InitializeFromBytesArray and then using Bitmap.Initialize2.

Then, I can not understand as follows...

RandomAccessFile1.Initialize(FPath, FileName, True)
List1.Initialize
List1.AddAll(RandomAccessFile1.ReadObject(0))
InputStream1.InitializeFromBytesArray(buffer, 0, buffer.length)?
?? how to convert List1 into InputStream1 ? List1 is buffer ?
InputStream1.ReadBytes(buffer, 0, buffer.length)?
Bitmap1(i).Initialize2(InputStream1)

Please help me.:sign0085:
 
Last edited:
Upvote 0

doraneko

Member
Licensed User
Longtime User
Thank you, Erel.

' Read the list: List1 = RandomAccessFile.ReadObject(0) ' is a very useful advice.
And next step, How should I do ?:confused:

How about buffer for InputStream ?
Is List1.Get needed for InputStream ?
More over, Is InputStream.ReadBytes(buffer, 0, buffer.length) needed?

Although you are too busy, please teach me.
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Thank you, Erel.

' Read the list: List1 = RandomAccessFile.ReadObject(0) ' is a very useful advice.
And next step, How should I do ?:confused:

How about buffer for InputStream ?
Is List1.Get needed for InputStream ?
More over, Is InputStream.ReadBytes(buffer, 0, buffer.length) needed?

Although you are too busy, please teach me.
To quote an earlier post by Erel
Convert each bytes Array To a Bitmap by first creating an InputStream with InputStream.InitializeFromBytesArray and then using Bitmap.Initialize2.
 
Upvote 0

macerau

Member
Licensed User
Longtime User
Hi Erel,
Sorry, I'm newbie,
searched a lot on the site but did not see a working example,
Please see if you can complete this routine
Sub SavePhoto (Pt As Bitmap,NewFile As String)
Dim LT As List
Dim RF As RandomAccessFile
RF.Initialize(File.DirAssets , NewFile, True)
LT.Initialize
LT.AddAll(Pt.WriteToStream) ' RF.ReadObject(0) ??

rf.WriteObject(LT, True, 0)
Dim ip As InputStream ?
ip.InitializeFromBytesArray(buffer, 0, buffer.length) ?
?? how To convert List1 into InputStream1 ? List1 Is buffer ?
InputStream1.ReadBytes(buffer, 0, buffer.length)?
B.Initialize2(InputStream1) ?


End Sub
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
To Erel,

Sorry to pick this up.

Your method described in post #4 does work, however, it takes about 10 seconds to
save a list containing 30 PNGs (64 x 64 each), and another 10 seconds retrieving one
from the loaded list.

Out of curiosity, which is the bottleneck, InitializeToBytesArray or Read/WriteObject?
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Frankly, I was not aware of such a class. I'll try it.
Anyway, what is it that slows down the routines?
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Yes, also, if compression is set to False it takes 3 seconds each for saving all images and retrieving one image (i/o 10).
 
Upvote 0
Top