B4A Library CameraView - Take Pictures, record Video with the Camera

this is a quick (and incomplete) hack for this github project.

CameraView
Version:
1
  • CameraView
    Events:
    • onCameraClosed ( As )
    • onCameraOpened (options As Object)
    • onExposureCorrectionChanged (newValue As Float, bounds() As Float, fingers() As Object)
    • onFocusEnd (success As Boolean, x As Int, y As Int)
    • onFocusStart (x As Int, y As Int)
    • onOrientationChanged (orientation As Int)
    • onPictureTaken (jpeg() As Byte)
    • onVideoTaken (path As String, filename As String)
    • onZoomChanged (newValue As Float, bounds() As Float, fingers() As Object)
    Fields:
    • ba As BA
    Methods:
    • BringToFront
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • Initialize (EventName As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImageNew (arg0 As Bitmap) As BitmapDrawable
    • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
    • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • capturePicture
      Asks the camera to capture an image of the current scene.
      This will trigger {@link CameraListener#onPictureTaken(byte[])} if a listener
      was registered.

      Note that if sessionType is {@link SessionType#VIDEO}, this
      might fall back to {@link #captureSnapshot()} (that is, we might capture a preview frame).
    • captureSnapshot
      Asks the camera to capture a snapshot of the current preview.
      This eventually triggers {@link CameraListener#onPictureTaken(byte[])} if a listener
      was registered.

      The difference with {@link #capturePicture()} is that this capture is faster, so it might be
      better on slower cameras, though the result can be generally blurry or low quality.
    • clearCameraListeners
      Clears the list of {@link CameraListener} that are registered
      to camera events.
    • destroy
    • pause
      Stops the current preview, if any was started.
      This should be called onPause().
    • setLocation (latitude As Double, longitude As Double)
      Set location coordinates to be found later in the jpeg EXIF header
      latitude: current latitude
      longitude: current longitude
    • setSessionPicture
    • setSessionVideo
    • start
      Starts the camera preview, if not started already.
      This should be called onResume(), or when you are ready with permissions.
    • startAutoFocus (x As Float, y As Float)
      Starts an autofocus process at the given coordinates, with respect
      to the view width and height.
      x: should be between 0 and getWidth()
      y: should be between 0 and getHeight()
    • startCapturingVideo (path As String, filename As String)
      Starts recording a video with selected options. Video will be written to the given file,
      so callers should ensure they have appropriate permissions to write to the file.
      file: a file where the video will be saved
    • stop
      Stops the current preview, if any was started.
      This should be called onPause().
    • stopCapturingVideo
      Stops capturing video, if there was a video record going on.
      This will fire {@link CameraListener#onVideoTaken(File)}.
    • toggleFacing As Facing
      Toggles the facing value between {@link Facing#BACK}
      and {@link Facing#FRONT}.
      Return type: @return:the new facing value
    • toggleFlash As Flash
      Toggles the flash mode between {@link Flash#OFF},
      {@link Flash#ON} and {@link Flash#AUTO}, in this order.
      Return type: @return:the new flash value
    Properties:
    • Background As Drawable
    • CameraOptions As CameraOptions [read only]
      Returns a {@link CameraOptions} instance holding supported options for this camera
      session. This might change over time. It's better to hold a reference from
      {@link CameraListener#onCameraOpened(CameraOptions)}.
    • CaptureSize As Size [read only]
      Returns the size used for the capture,
      or null if it hasn't been computed yet (for example if the surface is not ready).
    • Color As Int [write only]
    • CropOutput As Boolean [write only]
      Whether we should crop the picture output to match CameraView aspect ratio.
      This is only relevant if CameraView dimensions were somehow constrained
      (e.g. by fixed value or MATCH_PARENT) and do not match internal aspect ratio.

      Please note that this requires additional computations after the picture is taken.
    • Enabled As Boolean
    • ExposureCorrection As Float
      Returns the current exposure correction value, typically 0
      at start-up.
    • ExtraProperties As ExtraProperties [read only]
      If present, returns a collection of extra properties from the current camera
      session.
    • Facing As Facing
      Gets the facing camera currently being used.
    • Flash As Flash
      Gets the current flash mode.
    • Grid As Grid
      Gets the current grid mode.
    • Height As Int
    • JpegQuality As Int [write only]
      Sets the JPEG compression quality for image outputs.
    • Left As Int
    • Padding()() As Int
    • Parent As Object [read only]
    • PreviewSize As Size [read only]
      Returns the size used for the preview,
      or null if it hasn't been computed (for example if the surface is not ready).
    • SessionType As SessionType
      Gets the current session type.
    • SnapshotSize As Size [read only]
      Returns the size used for capturing snapshots.
      This is equal to {@link #getPreviewSize()}.
    • Started As Boolean [read only]
      Returns whether the camera has started showing its preview.
    • Tag As Object
    • Top As Int
    • VideoQuality As String
      Gets the current video quality.
    • Visible As Boolean
    • WhiteBalance As WhiteBalance
      Returns the current white balance behavior.
    • Width As Int
    • Zoom As Float
      Returns the current zoom value, something between 0 and 1.
 

Attachments

  • CameraViewEx.zip
    8.4 KB · Views: 1,164
  • CameraViewV1.01.zip
    100.5 KB · Views: 1,151
Last edited:

nicolino33

Active Member
Licensed User
Longtime User
No, this library is very good, come on, implement this feature, you did 30 make 31!
 

cliv

Member
Licensed User
Longtime User
How can i use autofocus?
This code ...
B4X:
Sub Cam_onFocusStart(x As Int, y As Int)
    Log($"Cam_onFocusStart(${x},${y})"$)
End Sub
Sub btnFocus_Click
    cam.startAutoFocus(cam.Width/2,cam.Height/2)
End Sub
... generate error:
B4X:
java.lang.Exception: Sub cam_onfocusstart signature does not match expected signature.
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:193)
    at anywheresoftware.b4a.BA$2.run(BA.java:365)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5769)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
 

Ivan Aldaz

Member
Licensed User
Longtime User
Probably the error is generated because the Sub Cam_onFocusStart is expecting Int values, and is receiving non-Int values
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
try
B4X:
Sub Cam_onFocusStart(x As Float, y As Float)
    Log($"Cam_onFocusStart(${x},${y})"$)
End Sub
 

cliv

Member
Licensed User
Longtime User
try
B4X:
Sub Cam_onFocusStart(x As Float, y As Float)
    Log($"Cam_onFocusStart(${x},${y})"$)
End Sub

I already try this, same error, only difference ...error after Cam_onFocusStart(540.0,760.5) event.
Before (with x As Int and y As Int) onFocusStart event is not triggered.
B4X:
Cam_onOrientationChanged(0)
Cam_onCameraOpened()
Cam_onFocusStart(540.0,760.5)
java.lang.Exception: Sub cam_onfocusend signature does not match expected signature.
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:193)
    at anywheresoftware.b4a.BA$2.run(BA.java:365)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5769)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
 

idrotelapp

Member
Licensed User
Dear Erel,
I am working on a widget and after clicking on widget I want to use cameraview to take a photo but cameraview does not start in widget . I tested the cameraview in new app without widget and it works properly !
How i can use cameraview in widget ?
many tanks for your kind help.
 
Top