B4J Question [Solved] Join images side by side

jroriz

Active Member
Licensed User
Longtime User
Hi.

I captured 2 portions of the screen, now I want to put them together in one image, side by side.

After that I want to write that image to HD as a png file.

This is what I have so far ...

B4X:
Dim Bitmap1 As B4XBitmap = Capture(x, y, width, height)
Dim Bitmap2 As B4XBitmap = Capture(x, y, width, height)

' join images side by side

' write png file.

Sub Capture(x As Int, y As Int, width As Int, height As Int) As B4XBitmap
    c3po.ScreenCurrentRectangleSetAsArbitrary(x, y, width, height)
    Return BytesToImage(c3po.ScreenCaptureAsByteArray)
End Sub

Public Sub BytesToImage(bytes() As Byte) As B4XBitmap
    Dim In As InputStream
    In.InitializeFromBytesArray(bytes, 0, bytes.Length)

    Dim bmp As Image
    bmp.Initialize2(In)

    Return bmp
End Sub

Image 1:
img1.PNG


Image 2:
img2.PNG


Final Image:
img3.PNG
 

jroriz

Active Member
Licensed User
Longtime User
I dont know how to save a canvas as bitmap.

I did it with BitmapCreator.

Any better idea?

B4X:
Dim Bitmap1 As B4XBitmap = Capture(0, 0, 50, 50)
Dim Bitmap2 As B4XBitmap = Capture(50, 0, 50, 50)

Dim bc1, bc2, bc3 As BitmapCreator

bc1.Initialize(50,50)
bc1.CopyPixelsFromBitmap(Bitmap1)

bc2.Initialize(50,50)
bc2.CopyPixelsFromBitmap(Bitmap2)

bc3.Initialize(100,50)

For x = 0 To 49
    For y = 0 To 49
        bc3.CopyPixel(bc1, x, y, x, y)
        bc3.CopyPixel(bc2, x, y, x+50, y)
    Next
Next

Dim out As OutputStream
out = File.OpenOutput(dirapp, "join.png", False)
bc3.Bitmap.WriteToStream(out, 100, "PNG")
out.Close
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Untested code:
B4X:
Sub MergeTwoBitmapsSideBySide(bmp1 As B4XBitmap, bmp2 As B4XBitmap) As B4XBitmap
   Dim bc As BitmapCreator
   bc.Initialize(bmp1.Width + bmp2.Width, Max(bmp1.Height, bmp2.Height))
   Dim r As B4XRect
   r.Initialize(0, 0, bmp1.Width, bmp1.Height)
   bc.DrawBitmap(bmp1, r, True)
   r.Initialize(bmp1.Width, 0, bc.mWidth, bmp2.Height)
   bc.DrawBitmap(bmp2, r, True)
   Return bc.Bitmap
End Sub
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
Untested code:
B4X:
Sub MergeTwoBitmapsSideBySide(bmp1 As B4XBitmap, bmp2 As B4XBitmap) As B4XBitmap
   Dim bc As BitmapCreator
   bc.Initialize(bmp1.Width + bmp2.Width, Max(bmp1.Height, bmp2.Height))
   Dim r As B4XRect
   r.Initialize(0, 0, bmp1.Width, bmp1.Height)
   bc.DrawBitmap(bmp1, r, True)
   r.Initialize(bmp1.Width, 0, bc.mWidth, bmp2.Height)
   bc.DrawBitmap(bmp2, r, True)
   Return bc.Bitmap
End Sub

Perfect!
 
Upvote 0
Top