Android Question Reduce image size during Media Chooser Capture

I am using media chooser to capture image and after reducing the size, I am sending it to Gemini API. Is it possible to capture the lowest possible resolution when using the camera and then send that to the Gemini API ?
Current Code:
current code captures image and then reduces size:
Private Sub Capture_Click
    Wait For (chooser.CaptureImage) Complete (Result As MediaChooserResult)
   
    Log($"The media directory is ${Result.MediaDir} and the file name is ${Result.MediaFile} and the mime is ${Result.Mime} ."$)
    B4XLoadingIndicator1.Show
    ShowMedia(Result)
    ConvertImageToBase64ResizeImage(Result.MediaFile,Result.MediaDir)
End Sub

Sub ConvertImageToBase64ResizeImage(filename As String, dir As String) As String
    Dim su As StringUtils
    Dim in As InputStream
    Dim bmp As Bitmap
    Dim resizedBmp As Bitmap
    Dim out As OutputStream
    Dim data() As Byte
    Dim base64 As String
   
    ' Load the original image
    bmp.Initialize(dir,filename)
    ' Resize the image to a smaller size
    resizedBmp = bmp.Resize(300, 300, True)
    Log($"Image resized to:Width: ${resizedBmp.Width} Heigth: ${resizedBmp.Height}"$)
    ' Save the resized image to a byte array
    out.InitializeToBytesArray(0)
    resizedBmp.WriteToStream(out, 100, "JPEG") ' Adjust quality as needed (100 = max quality)
    out.Close
   
    ' Convert byte array to Base64
    data = out.ToBytesArray
    base64 = su.EncodeBase64(data)
    SendImageToGemini(base64)
   
    Return base64
End Sub
 

drgottjr

Expert
Licensed User
Longtime User
resizing the image doesn't change the resolution. it drops pixels.
asking for something like the "lowest possible resolution" is difficult to resolve,
so to speak. you can still have a giant image with terrible resolution (that is,
resolution so low that it might render the image useless for anyalysis in spite
of its ginormous dimensions. android's cameras base themselves on screen
density, which changes depending on the model. i don't think it's possible for
the camera to take a picture at a different density. you would have to massage
the resulting bitmap. even then, what exactly is "the lowest possible resolution"?
how low do you go before gemini sees mud?
 
Upvote 0
@drgottjr , thank you for your response. I agree that the question isn't clear. I assume that resolution is directly linked to megapixels. I'm looking for a way to capture an image at the lowest possible resolution (megapixel) setting supported by the mobile camera that is using in my app. If there is a way for Media Chooser to select the lowest resolution and lowest image capture size setting in the mobile camera, that would be helpful.
 
Upvote 0
Top