Android Question How can I make the Camera2 PreviewTaken (Image As Object) event to happen?

knutf

Member
Licensed User
In CamEx2 the Camera2 PreviewTaken (Image As Object) event is commented:

B4X:
'Private Sub Camera_PreviewTaken (Image As Object)
'    Log(Image)
'End Sub
I tried uncommenting the Sub, but the event never happens: :(

B4X:
Private Sub Camera_PreviewTaken (Image As Object)
    Log("PreviewTaken")
End Sub
The other preview event "PreviewCaptureComplete" happens very often: :D

B4X:
Private Sub Camera_PreviewCaptureComplete (CaptureResult As Object)
    FocusState = IntToConst(GetFromCaptureResult(CaptureResult, "CONTROL_AF_STATE"), AF_STATE)
    Log("PreviewCaptureComplete")
    'Log(IntToConst(GetFromCaptureResult(CaptureResult, "FLASH_STATE"), FLASH_STATE))
    'PrintAllKeys(CaptureResult, "Capture Result")
End Sub
I'm planning to change my app from useing Camera to Camera2 library. My app use the old camera library's event Preview (Data() As Byte) to examine the preview picture by code, so now I'm playing with the CamEx2 to understand how it could be done.

How can I make the PreviewTaken event to happen?
 
Last edited:

Erel

Administrator
Staff member
Licensed User
You should use a timer and call CamEx2.GetPreviewBitmap.
 

knutf

Member
Licensed User
Why should I do that? It gives me a bitmap, I allready has an effective code to examine a preview in YUV_420_888 format.

After reading answers to this question https://stackoverflow.com/questions/25462277/camera-preview-image-data-processing-with-android-l-and-camera2-api I thaught that changing the StartSession line of the PrepareSurface sub from
B4X:
Camera.StartSession(tv, PreviewSize, CaptureSize, 256, 0, False) '256 = JPEG
to
B4X:
Camera.StartSession(tv, PreviewSize, CaptureSize, 256, 35, False) '256 = JPEG, 35 = YUV_420_888
could solve my problem.

The camera2 library has a PreviewTaken event, so there must be a way to activate it.
 

Erel

Administrator
Staff member
Licensed User
That's true. If the PreviewFormat is different than 0 then it will be raised.
 

knutf

Member
Licensed User
I forgot to tell: PreviewTaken event still do not raise, although I set the PreviewFormat parameter in the camera.StartSession to 35.

I made this sub:
B4X:
Public Sub getOutputFormats As Int()
    Dim SCM As JavaObject = GetFromCameraCharacteristic("SCALER_STREAM_CONFIGURATION_MAP")
    Dim OF() As Int = SCM.RunMethod("getOutputFormats", Array As Object())
    For Each f As Int In OF
        Log(f)
    Next
    Return OF
End Sub
Which gave me the following output formats: 32, 256, 34, 35, 36, 37

I have tried all of them:
  • Using outputformat 32, 256, 36 and 37 as PreviewFormat parameter causes "failed to open camera"
  • Using outputformat 36 and 37 opens the camera, shows preview on phone display, but the PreviewTaken does not raise.

So I still can't get PreviewTaken event to raise.
 

Attachments

knutf

Member
Licensed User
I am sorry, the list of formats I tried was wrong. The correct list is

  • Using outputformat 32, 256, 36 and 37 as PreviewFormat parameter causes "failed to open camera"
  • Using outputformat 34 and 35 opens the camera, shows preview on phone display, but the PreviewTaken does not raise.
I have tried 35, on a phone and on a tablet. The PreviewTaken event does not raise on either of them.

I have now decided to use PreviewCaptureComplete something like this
B4X:
Private Sub Camera_PreviewCaptureComplete (CaptureResult As Object)
    FocusState = IntToConst(GetFromCaptureResult(CaptureResult, "CONTROL_AF_STATE"), AF_STATE)
    
    Dim preview As Bitmap = GetPreviewBitmap(4,4)
    'examine the preview picture here
 

Erel

Administrator
Staff member
Licensed User
It is indeed not raised. I need to further check it. For now use GetPreviewBitmap.
 
Top