Android Question Capture Panel to image, how??

ilan

Expert
Licensed User
Longtime User
hi

i would like to make a screenshot of a panel and put it to a imageview
i search a lot but could not find anything that does it

i could make a screen shot but its not capturing only the panel it capture also some of the activity

my panel is not in pos 0 its some where in the activity

this is how i tried

B4X:
Sub createsmallimage

screenshot
cardb.Visible = True
cardb2.Visible = False
cardb2.Bitmap = LoadBitmap(File.DirInternal, "small.png")
cardb2.Gravity = Gravity.FILL

End Sub

'screen shot
Sub screenshot

'if file exist the delet first
  If File.Exists(File.DirInternal, "small.png") = True Then
  File.Delete(File.DirInternal, "small.png")
  End If


'test1
Dim jpegPath As Path
jpegPath.Initialize(Panel1.Left,Panel1.Top)
jpegPath.LineTo(Panel1.Left + Panel1.Width,Panel1.Top)
jpegPath.LineTo(Panel1.Left + Panel1.Width,Panel1.Top + Panel1.Height)
jpegPath.LineTo(Panel1.Left,Panel1.Top + Panel1.Height)
jpegPath.LineTo(Panel1.Left,Panel1.Top)

Dim Obj1, Obj2 As Reflector
Dim bmp As Bitmap
Dim c As Canvas
Obj1.Target = Obj1.GetActivityBA
Obj1.Target = Obj1.GetField("vg")
bmp.InitializeMutable(Panel1.Width, Panel1.Height)
c.Initialize2(bmp)
c.ClipPath(jpegPath)
Dim args(1) As Object
Dim types(1) As String
Obj2.Target = c
Obj2.Target = Obj2.GetField("canvas")
args(0) = Obj2.Target
types(0) = "android.graphics.Canvas"
Obj1.RunMethod4("draw", args, types)
Dim Out As OutputStream
Out = File.OpenOutput(File.DirInternal, "small.png", False)
bmp.WriteToStream(Out, 100, "PNG")
Out.Close
 
End Sub

how can i capture only the panel and not from pos 0,0??

thenx
 

ilan

Expert
Licensed User
Longtime User
hi lucas
thanx for your answer

i checked the thread you post here but this doenot solve my problem

i get a screen shoft but it starts from point (x=0) and (y=0) of the activity
and i want it to start from panel1.left and panel1.top

i use this code

B4X:
    Dim Obj1, Obj2 As Reflector
    Dim bmp As Bitmap
    Dim c As Canvas
    Obj1.Target = Obj1.GetActivityBA
    Obj1.Target = Obj1.GetField("vg")
    bmp.InitializeMutable(Panel1.Width, Panel1.Height)
    c.Initialize2(bmp)
    Dim args(1) As Object
    Dim types(1) As String
    Obj2.Target = c
    Obj2.Target = Obj2.GetField("canvas")
    args(0) = Obj2.Target
    types(0) = "android.graphics.Canvas"
    Obj1.RunMethod4("draw", args, types)
    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirInternal, "small.png", False)
    bmp.WriteToStream(Out, 100, "PNG")
    Out.Close
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok after about 4 hours i could make it work

this is my working code:

B4X:
Sub screenshot

'if file exist the delete first
If File.Exists(File.DirInternal, "small.png") = True Then
File.Delete(File.DirInternal, "small.png")
End If

Dim Obj1, Obj2 As Reflector
Dim bmp As Bitmap
Dim c As Canvas
Obj1.Target = Obj1.GetActivityBA
Obj1.Target = Obj1.GetField("vg")
bmp.InitializeMutable(Panel1.left + Panel1.Width, Panel1.Top + Panel1.Height)  
c.Initialize2(bmp)
Dim args(1) As Object
Dim types(1) As String
Obj2.Target = c
Obj2.Target = Obj2.GetField("canvas")
args(0) = Obj2.Target
types(0) = "android.graphics.Canvas"
Obj1.RunMethod4("draw", args, types)
Dim Out As OutputStream
Out = File.OpenOutput(File.DirInternal, "small.png", False)
bmp.WriteToStream(Out, 50, "PNG")
Out.Close

'draw from image to canvas
Dim canvas1 As Canvas
canvas1.Initialize(cardb2)
Dim scrt As Rect
scrt.Initialize(panel1.left, panel1.top, panel1.left + panel1.Width, panel1.Top + panel1.Height)
Dim rectPanel1 As Rect
rectPanel1.Initialize(0, 0,cardb2.Width, cardb2.Height)
canvas1.DrawBitmap(LoadBitmap(File.DirInternal, "small.png"), scrt , rectPanel1)
cardb2.Invalidate

End Sub

the question is do i really need first save my panel1 to a png file and then load it to canvas and put it in my view "cardb2"??
i belive its possible to put it directly without save to external file and load it again...

thanx
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Have you tried this way?

hi lucas thanks for your fast response

i have tried it and i am doing somthing like this in my code but my question is:

is it possible to draw a rect in my activity (let say in the position of panel1) and then capture this are to a imageview without saving the captured area
to external file and then load it again?
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok i think i got it:

B4X:
Dim Obj1, Obj2 As Reflector
Dim bmp As Bitmap
Dim c As Canvas
Obj1.Target = Obj1.GetActivityBA
Obj1.Target = Obj1.GetField("vg")
bmp.InitializeMutable(Panel1.left + Panel1.Width, Panel1.Top + Panel1.Height)   
c.Initialize2(bmp)
Dim args(1) As Object
Dim types(1) As String
Obj2.Target = c
Obj2.Target = Obj2.GetField("canvas")
args(0) = Obj2.Target
types(0) = "android.graphics.Canvas"
Obj1.RunMethod4("draw", args, types)

'draw from image to canavas
Dim canvas1 As Canvas
canvas1.Initialize(cardb2)
Dim scrt As Rect
scrt.Initialize(Panel1.left, Panel1.top, Panel1.left + Panel1.Width, Panel1.Top + Panel1.Height)
Dim rectPanel1 As Rect
rectPanel1.Initialize(0, 0,cardb2.Width, cardb2.Height)
canvas1.DrawBitmap(bmp, scrt , rectPanel1)
cardb2.Invalidate

like this i dont save to external file just copy it to the imageview
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
you could make it a routine and post it on Snippets

routine? you mean like this? (where the user just put the view to capture and the view to put the captured area?)

http://www.b4x.com/android/forum/threads/capture-complete-panel-to-imageview.40657/

B4X:
Sub screenshot

Dim pnl As Panel 'this is the panel you want to capture
pnl.Initialize("")
pnl = Panel1

Dim img2 As ImageView 'this is the imageview you want to put the captured area
img2.Initialize("")
img2 = cardb2

Dim Obj1, Obj2 As Reflector
Dim bmp As Bitmap
Dim c As Canvas
Obj1.Target = Obj1.GetActivityBA
Obj1.Target = Obj1.GetField("vg")
bmp.InitializeMutable(pnl.left + pnl.Width, pnl.Top + pnl.Height)
c.Initialize2(bmp)
Dim args(1) As Object
Dim types(1) As String
Obj2.Target = c
Obj2.Target = Obj2.GetField("canvas")
args(0) = Obj2.Target
types(0) = "android.graphics.Canvas"
Obj1.RunMethod4("draw", args, types)

'draw from image to canavas
Dim canvas1 As Canvas
canvas1.Initialize(img2)
Dim scrt As Rect
scrt.Initialize(pnl.left, pnl.top, pnl.left + pnl.Width, pnl.Top + pnl.Height)
Dim rectPanel1 As Rect
rectPanel1.Initialize(0, 0,img2.Width, img2.Height)
canvas1.DrawBitmap(bmp, scrt , rectPanel1)
img2.Invalidate

End Sub
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok i understand what you mean

here is the code (i will put it also to snippets

B4X:
Sub Button1_Click
    'capture panel to imageview
      PanelCapture(Panel1,cardb2)
End Sub

'Capture Panel to imageview
Sub PanelCapture(pnl As Panel, Img2 As ImageView)

Dim Obj1, Obj2 As Reflector
Dim bmp As Bitmap
Dim c As Canvas
Obj1.Target = Obj1.GetActivityBA
Obj1.Target = Obj1.GetField("vg")
bmp.InitializeMutable(pnl.left + pnl.Width, pnl.Top + pnl.Height)   
c.Initialize2(bmp)
Dim args(1) As Object
Dim types(1) As String
Obj2.Target = c
Obj2.Target = Obj2.GetField("canvas")
args(0) = Obj2.Target
types(0) = "android.graphics.Canvas"
Obj1.RunMethod4("draw", args, types)

'draw from image to canavas
Dim canvas1 As Canvas
canvas1.Initialize(Img2)
Dim scrt As Rect
scrt.Initialize(pnl.left, pnl.top, pnl.left + pnl.Width, pnl.Top + pnl.Height)
Dim rectPanel1 As Rect
rectPanel1.Initialize(0, 0,Img2.Width, Img2.Height)
canvas1.DrawBitmap(bmp, scrt , rectPanel1)
Img2.Invalidate

End Sub
 
Upvote 0
Top