Android Question Help: Lost in capturing panel to file and ImageView

Said

Member
Licensed User
Longtime User
Hi,

I am planning to capture whole panel and save it to PNG file to archive. Because i am new to B4A (i skip .NET, last programming experience in VB6), i used ilan's codesnippet link below;

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

It works but not correct because of some reasons. First reason, i am not using panel only. I am using panel inside tabhost. The second problem is, i cant get exact positions of image and i get cropped image or streched image.

Here my Code;

B4X:
Sub PanelCapture(pnl As Panel)

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(Testimage)
Dim scrt As Rect
scrt.Initialize(pnl.left, pnl.top,  pnl.Width, pnl.Height)
'scrt.Initialize(0, 0,  680, 998)

Dim rectPanel1 As Rect
rectPanel1.Initialize(pnl.left, pnl.top,  pnl.Width, pnl.Height)
canvas1.DrawBitmap(bmp, scrt , rectPanel1)
Testimage.Invalidate

'here writing canvas to file

Dim Out As OutputStream
Dim MyFileName As String

Log(DateTime.Now)
MyFileName = DateTime.Now & ".png"
Out = File.OpenOutput(File.DirDefaultExternal, MyFileName, False)
canvas1.Bitmap.WriteToStream(Out, 100, "PNG")
Out.Close

First screen shot is original emulator screen (taken shot by ddms)

Second one is, capture after code above. As you see my code captures whole screen instead of panel. And crop right side and bottom of panel (because of correct panel sizes). I dont want to see Tabhost headers. Then i tried to change both rectangle lines in the code like below;

scrt.Initialize(pnl.left+10dip, pnl.top+50dip, pnl.Width+10dip, pnl.Height+50dip)
rectPanel1.Initialize(pnl.left+10dip, pnl.top+50dip, pnl.Width+10dip, pnl.Height+50dip)​

Then i got third screen shot. As you see, this doesnt work. Blank top and left. Then I changed rectangle codes like below. Source rectangle changed, target Rectangle same as previous.

scrt.Initialize(pnl.left+10dip, pnl.top+50dip, pnl.Width+10dip, pnl.Height+50dip)
rectPanel1.Initialize(pnl.left, pnl.top, pnl.Width, pnl.Height)​

Then I got 4th screen this time. As you see, it doesnt work. Now, blank (truncated) right and bottom. And I changed rectangle codes like below this time (original source and modified target)

scrt.Initialize(pnl.left, pnl.top, pnl.Width, pnl.Height)
rectPanel1.Initialize(pnl.left+10dip, pnl.top+50dip, pnl.Width+10dip, pnl.Height+50dip)​

It doesnt work again and i got 5th screen this time. As you see, this is worst one :) Blank top and left plus unwanted tabhost (and of course truncated right and bottom)

I tried some other variations but they didnt work. Because i am new, i cant modify code much. Actually i dont understand first 10-15 lines of snippet. After long variation tries, i didnt get any correct result.

Now what i want ?

1. If possible, take shot directly from panel, instead of whole screen. Because its hard to find real "plus dips" and this may causes some strech, which is unwanted.

2. If not possible, what is my correct rectangle coordinates. I know its simple "mathematichs" but i am completely screwed up :(

3. Tabhost is important. I cant change it after weeks. But if no way to crop tabhost banner, i can live with it.

Regards,

-Said

ps: player names in the screenshots are "coincidence". I am not sending any message to experts who can help me. Trust me :)
 

Attachments

  • device-2014-09-21-235819.png
    device-2014-09-21-235819.png
    107 KB · Views: 224
  • 1411332872480.png
    1411332872480.png
    206.9 KB · Views: 241
  • 1411333388538.png
    1411333388538.png
    194.5 KB · Views: 238
  • 1411333668936.png
    1411333668936.png
    194.7 KB · Views: 240
  • 1411334028563.png
    1411334028563.png
    185.5 KB · Views: 238
Top