Android Question [Solved] MediaChooser/SMM - limit image file size

udg

Expert
Licensed User
Longtime User
Hi all,
today I used Mediachooser to take a couple of photos, limiting the file size, but I found that a "complex" image returns an error about the final file size exceeding the limit imposed.
With "complex" image I mean an image with many colors and labels. The same code on a simpler version of the same image doesn't raise the error.

I tried the following code:
B4X:
smm.MaxImageSize = 1 * 1024 * 1024
smm.MaxImageFileSize = 4 * 1024 * 1024 
'...
Private Sub lblTakeFr_Click
    KeepRunningService.NotificationTitle = "Taking a photo"
    Wait For (chooser.CaptureImage) Complete (Result As MediaChooserResult)
    ShowMedia(pnlFronte,Result)
End Sub

Private Sub ShowMedia (apnl As B4XView, Result As MediaChooserResult)
    If Result.Success Then
        smm.SetMediaFromFile(apnl, Result.MediaDir, Result.MediaFile, Result.Mime, Null)
    Else
        apnl.RemoveAllViews
        Log("errore smm")
    End If
End Sub
The only change in Erel's code is about a parameter for ShowMedia which is used to select among two different target panels.
MaxImageFileSize alone raised the error about file size
MaxImageSize was an attempt to keep the image smaller "before" saving to a file, but it didn't work

So, the question is: how to save to a 2MB (or less) file an image taken with a camera able to take photos at 12Mpixels (or whatever) ?
Or should I leave a file size limit high enough for the camera and make use of panel.snapshot to save a smaller file?

Fundamentally, I'd like to save a couple of photos along with other simple data in a compact "message" to be sent to a remore server. The goal is to minimize data traffic in both directions.

TIA
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 1

udg

Expert
Licensed User
Longtime User
So, I've to go with panel.snapshot and resize until the file size is withinn "acceptable" limits?
Do you recommend to set a higher limit with SMM in order to account for cameras producing large files? I'd like to avoid the gray screen when taking photos with a "powerful" camera.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Thank you for your support.
Please find below the whole log.
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
*** Service (keeprunningservice) Create ***
** Service (keeprunningservice) Start **
** Activity (main) Pause event (activity is not paused). **
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
** Activity (main) Resume **
File larger than MaxFileSize 4,010,581

A bit of context. I'm using MediaChooser to take photos of ID cards (front and back) so I created a B4xPage where two panels are used as targets.
Depending on the type of ID card the error (warning) above is showed iin Log whereas the panel turns to dark grey (as expected).
Based on your comment in post #3 I guess that to avoid the error I've to set smm.MaxImageFileSize to an higher value, presumably based on the resolution of modern smartphones' cameras.
I don't know what smm.MaxImageSize is used for. Maybe similar to above but related to space in memory?

Anyway, once both photo shoots are regularly taken, I've to save them in a file (or stream of bytes) in order to send them to a remote server. My goal is to keep the size of this final object as low as possible without compromising photo quality too much. Photos don't need to merge in a single file, but it could be a nice addition.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
1. Increase SMM max sizes.
Both smm.MaxImageSize and smm.MaxImageFileSize? Set to the same value?
Should the value relate somehow to used camera resolution or any other item? I ask this because I don't know which smartphone they will use on the field (and mine isan old model with relatively low grade camera..)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
MaxImageFileSize - Gets or sets the maximum file size of images that will be loaded. Default size is 8mb (8 * 1024 * 1024)
You can increase it to 40mb.

MaxImageSize - If the loaded image size will be wider or taller than this size then the loaded image will be resized.

The message you are asking about is related to MaxImageFileSize.
 
  • Like
Reactions: udg
Upvote 0
Top