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

Discussion in 'Android Questions' started by knutf, Mar 23, 2018.

  1. knutf

    knutf Member Licensed User

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

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

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

    Code:
    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: Mar 23, 2018
  2. Erel

    Erel Administrator Staff Member Licensed User

    You should use a timer and call CamEx2.GetPreviewBitmap.
     
  3. knutf

    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...ata-processing-with-android-l-and-camera2-api I thaught that changing the StartSession line of the PrepareSurface sub from
    Code:
    Camera.StartSession(tv, PreviewSize, CaptureSize, 2560False'256 = JPEG
    to
    Code:
    Camera.StartSession(tv, PreviewSize, CaptureSize, 25635False'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.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    That's true. If the PreviewFormat is different than 0 then it will be raised.
     
  5. knutf

    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:
    Code:
    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.
     

    Attached Files:

  6. Erel

    Erel Administrator Staff Member Licensed User

    Try it with 35
     
  7. knutf

    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
    Code:
    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
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    It is indeed not raised. I need to further check it. For now use GetPreviewBitmap.
     
  9. knutf

    knutf Member Licensed User

    Thank you!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice