Share My Creation Camera2 - widen your horizons, reduce your footprint

remember Camera2? looks like it will soon go the way of the original Camera api in android's
Hall of Deprecation once people figure out how to use CameraX.

anyway, i had occasion to revisit the Camera2 example app, more out of curiosity than anything
else. there seemed to be no way to invoke Camera2's so-called "wide" angle setting. also, what
i really missed was a way to set the "capture" size. so i added them. a (possibly) interesting capture
setting is a square image. in looking at the various supported settings, i thought there was a typo:
1080x1080. a square image! once again google has given us a solution to which there was no problem.
regarding the wide angle setting, if your camera doesn't support it, you don't get it.
 

Attachments

  • normal.png
    normal.png
    166.4 KB · Views: 1,873
  • wide.png
    wide.png
    181.2 KB · Views: 147
  • 2x.png
    2x.png
    192.4 KB · Views: 151
  • size-1.png
    size-1.png
    49.5 KB · Views: 143
  • size-2.png
    size-2.png
    49 KB · Views: 147
  • square.png
    square.png
    97.9 KB · Views: 159
Last edited:

drgottjr

Expert
Licensed User
Longtime User
device manufacturers supply their own camera hardware and
software. as a result your camera will either have the
so-called wide angle feature or it won't.

the first thing you need to do is determine your camera's
upper and lower zoom limits. if the lower limit is less than
1 (normal view), you have wide angle. if not, you don't.

assuming you have the feature, in order to use it, you set the
camera's zoom to that minimum limit.

1) declare a minimum and maximum zoom levels as floats in process global.
2) run a small javaobject routine to access those levels.
3) set the wide angle view in the camex class.

code snippets follow. i think that's all you need to do.

in main:
B4X:
Sub Process_Globals
    Dim minzoom, maxzoom, currentzoom As Float
    ....
    ....
End Sub

B4X:
Sub PrepareSurface As ResumableSub
    ....
    ....
    Dim zjo As JavaObject
    zjo = cam.MinDigitalZoom
    ' public static final Key<Range<Float>>
    
    minzoom = zjo.RunMethod("getLower",Null)
    maxzoom = zjo.RunMethod("getUpper",Null)
    Log("min: " & minzoom & "  max: " & maxzoom)

    ....
    ....
End Sub

in camex:
B4X:
Public Sub StartPreview (MyTaskIndex As Int, VideoRecording As Boolean)
'    Dim PreviewBuilder As JavaObject              ' i made it a global, but it could probably stay local
    PreviewBuilder = Camera.CreatePreviewBuilder
    PreviewSettingsMap.Put("CONTROL_ZOOM_RATIO", Main.currentzoom)      ' <----------------------
    SetSettingsFromMap(PreviewBuilder, PreviewSettingsMap)
    
    ' want to set capture zoom level here, not when picture is taken
    CaptureBuilder = Camera.CreateCaptureBuilder
    Log("hintorientation: " & GetHintOrientation)
    CaptureSettingMap.Put("JPEG_ORIENTATION", GetHintOrientation)
    CaptureSettingMap.Put("CONTROL_ZOOM_RATIO", Main.currentzoom)     ' <--------------------------
    SetSettingsFromMap(CaptureBuilder, CaptureSettingMap)
'    Dim CaptureRequest As Object = Camera.AddCaptureRequest(builder)
    
    PreviewRequest = Camera.SetRepeatingRequest(PreviewBuilder)
    If PrintKeys Then PrintAllKeys(PreviewRequest, "Preview Capture Request")
End Sub

to go wide:
B4X:
   cam.Stop
    currentzoom = minzoom
    OpenCamera(frontCamera)

to return to normal:
B4X:
    cam.Stop
    currentzoom = 1.0f
    OpenCamera(frontCamera)
 

Drago Bratko

Active Member
Licensed User
Longtime User
I'm playing with it, but can't make it work.

In your example:
B4X:
    Dim zjo As JavaObject
    zjo = cam.MinDigitalZoom

There is no property MinDigitalZoom on my side.

Is it possible that you share your workable solution? Or I'm asking too much ...
 

drgottjr

Expert
Licensed User
Longtime User
this is the sub that finds whether you can handle wide:

B4X:
public Sub getMinDigitalZoom As Object
    Return GetFromCameraCharacteristic("CONTROL_ZOOM_RATIO_RANGE")
End Sub

i noticed that i reworked erel's Camex2's class a bit... i probably added that sub to it. (it's called a "getter" if you're wondering why it doesn't appear to be anywhere.)
anyway, i'm attaching my class. it's called Camex3. copy Camex2 to someplace safe, remove it from the project, and then add Camex3 in its place.
 

Attachments

  • CamEx3.bas
    18.7 KB · Views: 19
Top