Hi all,
I created an encoder to encode particular custom video files.
This encoder encodes audio+video interleaved on same file, in a particular way that microcontrollers can read as fast possible.
After this I created a player for it on desktop side, to inspect the final video before to use on microcontrollers (becuse it is my custom format it cannot play on any default players and I had to develop my custom player).
After some troubles the video player now works well, video+audio are synced.
The problem now is that I want to add the zoom.
I already use the B4XPages_Resize event to resize the panel and the canvas inside it based on form size. I know I have to calculate and mantain the aspect ratio, this is not a big problem now. The problem is that I cannot use LoadBitmapResize or similar, because I don't have saved images, they are just arrays of bytes I read from the file.
I want to know how I can scale that, only bigger size is needed, there is no need to scale smaller, this because the videos are small in resolution, to be adapted to TFT screens, so, the max size of 480x320.
Actually I convert bytes to an Image and use Canvas.DrawBitmap to draw.
May using an ImageView I can get it to work ? But there are ways to adapt it to a Canvas without switch to an ImageView ?
Here some relevant parts of my code:
Here some code I can use to mantain aspect ratio when form is scaled:
I created an encoder to encode particular custom video files.
This encoder encodes audio+video interleaved on same file, in a particular way that microcontrollers can read as fast possible.
After this I created a player for it on desktop side, to inspect the final video before to use on microcontrollers (becuse it is my custom format it cannot play on any default players and I had to develop my custom player).
After some troubles the video player now works well, video+audio are synced.
The problem now is that I want to add the zoom.
I already use the B4XPages_Resize event to resize the panel and the canvas inside it based on form size. I know I have to calculate and mantain the aspect ratio, this is not a big problem now. The problem is that I cannot use LoadBitmapResize or similar, because I don't have saved images, they are just arrays of bytes I read from the file.
I want to know how I can scale that, only bigger size is needed, there is no need to scale smaller, this because the videos are small in resolution, to be adapted to TFT screens, so, the max size of 480x320.
Actually I convert bytes to an Image and use Canvas.DrawBitmap to draw.
May using an ImageView I can get it to work ? But there are ways to adapt it to a Canvas without switch to an ImageView ?
Here some relevant parts of my code:
B4X:
Try
FramePos = Video.DataChunkStart + (Video.FrameBlockSize * FrameNumber)
Dim Image(FrameLen) As Byte ' Redim
ReadBytes = RAF.ReadBytes(Image, 0, FrameLen, FramePos)
If ReadBytes <> Video.VideoBlockSize Then
LogError("Different frame read bytes. Read bytes: " & ReadBytes & " Expected: " & Video.VideoBlockSize)
Return False
End If
Dim Rect As B4XRect
Rect.Initialize(0, 0, Video.Width, Video.Height)
Canvas1.DrawBitmap(BytesToImage(Image), Rect)
Canvas1.Invalidate
...........
...........
Sub B4XPage_Resize (Width As Int, Height As Int)
Panel1.SetSize(Width, Height - lblTime.PrefHeight - Slider1.PrefHeight)
Canvas1.Resize(Width, Height - lblTime.PrefHeight - Slider1.PrefHeight)
Clear(xui.Color_Black)
Canvas1.Invalidate
End Sub
Public Sub BytesToImage(bytes() As Byte) As B4XBitmap
Dim In As InputStream
In.InitializeFromBytesArray(bytes, 0, bytes.Length)
#if B4A or B4i
Dim bmp As Bitmap
bmp.Initialize2(In)
#else
Dim bmp As Image
bmp.Initialize2(In)
#end if
Return bmp
End Sub
Here some code I can use to mantain aspect ratio when form is scaled:
B4X:
Public Sub ResizeWithAspectRatio(newWidth As Int)
Dim aspectRatio As Float = Video.Width / Video.Height
Dim newHeight As Int = newWidth / aspectRatio
Dim f As Form = B4XPages.GetNativeParent(Me)
f.WindowWidth = newWidth + 16
f.WindowHeight = newHeight + 39 + lblTime.PrefHeight + Slider1.PrefHeight
Panel1.SetLayoutAnimated(0, 0, 0, newWidth, newHeight)
Canvas1.Resize(newWidth, newHeight)
End Sub
Attachments
Last edited: