InitializeSample

nico78

Active Member
Licensed User
Longtime User
B4X:
image.InitializeSample(File.DirInternalCache, "image.jpg",294dip,294dip)
Log(image.Width) ;=482
Log(image.Height); =482

why the dimension is not equal to 294?
 

nico78

Active Member
Licensed User
Longtime User
same thing with:
image.InitializeSample(File.DirInternalCache, "image.jpg",294,294)

the image is not resized
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is a comment in the manual saying:
Note that the actual dimensions may be larger than the specified values.

Sampling is done by merging blocks of pixels. This results in images that may be larger than the specified size. The purpose of InitializeSample is to reduce the required memory, not to resize the image.
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Like others, I've tried to use InitializeSample (and LoadBitmapSample), but they don't work as I expect. They load a much larger picture than I ask for.

For example, if I load a large pic (2448x3264 - the resolution of the S3's camera) into a bitmap, and set maxWidth and maxHeight to the activity's size (720x1134), both Sample methods return a bitmap sized 1224x1632. I saw the help text that says "may be larger", but I can't imagine how this function would be useful if I can't reliably receive the dimensions I ask for.

In the meantime, I found Informatix's BitmapPlus library, and when I replace the InitializeSample with his createScaledBitmap, it works perfectly.

Am I misunderstanding what InitializeSample and LoadBitmapSample are for? I assumed they were a way to load a big picture and downsize it to a specific resolution. But the resolution seems "fuzzy".
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
These methods are based on Options.inSampleSize field: BitmapFactory.Options | Android Developers

The purpose of these methods is to save memory. You can easily consume all available memory by loading a single large image.
You can then pass the bitmap to createScaledBitmap if you need to set the exact size (which in most cases is not really important).
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Ah, that makes some sense now - use InitializeSample to avoid loading a huge image in the first place, then resize it to specific dimensions using createScaledBitmap.

I'll give that a go. Thanks!
 
Upvote 0
Top