Android Question JPCT-AE: how to take a screenshot

AndrewKing

Member
Licensed User
Longtime User
I guess I should use FrameBuffer.getPixels2 to get the buffer data, but timer after time, it always gets nothing. The byte array is still all zero.

This is my simple code - It clear the screen with the red color, then the buffer data should not be all 0.

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim FrameBuffer As JpctFrameBuffer
    Dim World As JpctWorld
    Dim back As JpctRGBColor
End Sub

Sub Globals
    Dim Jpct As JpctSurface
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Jpct.Initialize("Jpct", 1, Jpct.RENDER_CONTINUOUSLY)
    Activity.AddView(Jpct,0,0,Activity.width,Activity.height)
   
    Button1.Initialize("Button1")
    Activity.AddView(Button1,20%x,0,60%x,80dip)
    Button1.Text="test"
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Jpct_SurfaceChanged(Width As Int, Height As Int)
    FrameBuffer.Initialize1(Jpct.glContext, Width, Height)

    World.Initialize
    World.setAmbientLight(120, 120, 120)
End Sub

Sub Jpct_SurfaceDraw
    back.Initialize2(255, 0, 0)
    FrameBuffer.clear2(back)
   
    World.renderScene(FrameBuffer)
    World.draw(FrameBuffer)
    FrameBuffer.display
End Sub

Sub Button1_Click
    Dim bye(FrameBuffer.Width*FrameBuffer.Height) As Int
    FrameBuffer.getPixels2(bye)
   
    Dim i As Int
    For i=1000 To 1100
        Log(bye(i))   'always =0
    Next
End Sub
 

ricardossy1

Member
Licensed User
Longtime User
Framebuffer.getpixels2() should be used only in the thread "JPCT"- Jpct_SurfaceDraw :

the fix is :

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim FrameBuffer As JpctFrameBuffer
    Dim World As JpctWorld
    Dim back As JpctRGBColor
    Dim Touch As Boolean
End Sub

Sub Globals
    Dim Jpct As JpctSurface
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Jpct.Initialize("Jpct", 1, Jpct.RENDER_CONTINUOUSLY)
    Activity.AddView(Jpct,0,0,Activity.width,Activity.height)

    Button1.Initialize("Button1")
    Activity.AddView(Button1,20%x,0,60%x,80dip)
    Button1.Text="test"

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub Jpct_SurfaceChanged(Width As Int, Height As Int)
    FrameBuffer.Initialize1(Jpct.glContext, Width, Height)

    World.Initialize
    World.setAmbientLight(120, 120, 120)
End Sub

Sub Jpct_SurfaceDraw
    back.Initialize2(255,0, 0)
    FrameBuffer.clear2(back)

    World.renderScene(FrameBuffer)
    World.draw(FrameBuffer)
    FrameBuffer.display
  
    If Touch=True Then
        Touch=False
        Dim bye(FrameBuffer.Width*FrameBuffer.Height) As Int
        FrameBuffer.getPixels2(bye)
          Dim i As Int
        For i=1000 To 1100
            Log(bye(i))  'always =-16711936
        Next
    End If
End Sub

Sub Button1_Click
  Touch=True
End Sub
 
Last edited:
Upvote 0

AndrewKing

Member
Licensed User
Longtime User
Let me finish the code

This is the code to get the buffer: FrameBuffer -> bye -> jpg (the 'jpeg' libray is needed)
B4X:
Sub Jpct_SurfaceDraw
    back.Initialize2(255, 0, 0)
    FrameBuffer.clear2(back)
  
    World.renderScene(FrameBuffer)
    World.draw(FrameBuffer)
    FrameBuffer.display
  
    If Touch=True Then
        Touch=False
      
        Dim bye(FrameBuffer.Width*FrameBuffer.Height) As Int
        FrameBuffer.getPixels2(bye)

        jpg.BmpWidth=FrameBuffer.Width
        jpg.BmpHeight=FrameBuffer.Height
        jpg.BmpPixels=bye

    End If
End Sub

This is the code to convert the byte array (color array) to bitmap (can not be done inside sub Jpct_SurfaceDraw, so I use button2_click):
B4X:
Sub Button2_Click
    Dim b As Bitmap
  
    jpg.BmpConfig="ARGB_8888"
    jpg.PixelsABGRtoARGB
    b=jpg.GetBmpFromPixels

End Sub
 
Upvote 0
Top