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,292
  • CameraViewV1.01.zip
    100.5 KB · Views: 1,268
Last edited:

DonManfred

Expert
Licensed User
Longtime User
I can upload the source and you can complete it if you want...
There is no support for the CameraOptions as yet
 

KingEdem

Member
Licensed User
Longtime User
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.

Hi Don,

I would like to superimpose (transparent 30%) date n time on the recording video and snap picture. Please suggest.
 

GDO

Member
Licensed User
Longtime User
Hello,

Could you please add an : onPreview(pic() as Bytes) event ?

Thank you.
 

Ivan Aldaz

Member
Licensed User
Longtime User
Hi. I've been trying this library and find it very simple and useful, but there are a couple of things I can't handle.

- The first is video quality. It's set by
B4X:
cam.VideoQuality = "XXXXXXXX"
...and can be seen later with
B4X:
Log("cam.CaptureSize: " & cam.capturesize)

Different values are obtained changing parameter "XXXXXX" for "MAX_720P", "LOWEST", "MAX_QVGA", etc., but the values I obtain do not correspond neither are proportional to the parameters For example, on my Asus Zenfone 2 I get a resolution of "4096x3072" for "MAX_480P", "MAX_720P", "MAX_1080P" and "MAX_QVGA", and "4096x3072" for "MAX_2160" and "HIGHEST". On a Vodafone Prime 6, some similar occurs: "352x288" for "LOWEST", "720x480" for "MAX_480P", "3264x1836" for "MAX_720P", "MAX_1080P", "MAX_2160P" and "HIGHEST", and "3264x2448" for "MAX_QVGA".

How can it be set/controlled the right way?

- The second one is preview size. In Portrait mode I haven't detected any problem, but in Landscape mode Preview is seen elongated vertically, although video is recorded right. I've tried to set camera layout proportional to resolution and fiot to screen, but no luck.

Is there any way to control the preview dimensions?

Thanks in advance.
 

DonManfred

Expert
Licensed User
Longtime User
im sorry, i can't find the image file in your example when snap picture
  • onPictureTaken (jpeg() As Byte)
  • onVideoTaken (path As String, file

onPictureTaken get a bytearray with the image. If you want to save it you need to do it manually.
 

nicolino33

Active Member
Licensed User
Longtime User
eheheh.... I'v look.... it lacks an essential function! This library make photo at max resolution, and with new phone the dimension of photo an even exceed 10/11 mb. Is not possible to make a new implementation of picture size setting? maybe through in-line java, why this? why the time for take picture at max resolution are very long... you should also update the target sdk to 26.
 
Last edited:
Top