Android Question Code Optimization- Need suggestions for improvement

sangee

Member
Licensed User
Longtime User
Dear All,

I have the below code capturing images from the camera using preview once every 10 seconds. The code works but it takes more than a second to write the image to the storage. The code first captures the camera image, Then it creates a BMP with the timestamp overlay and superimposes with the camera image and is written to storage. I request the gurus to look into the code and recommend if there is any other efficient way to accomplish the same using less resources like cpu and memory. Every advice is valuable to me.. Thank you...

B4X:
Sub Camera1_Preview (PreviewPic() As Byte)
Try   
    Dim forDate As String
    DateTime.DateFormat ="HH.mm.ss.SS_dd-MM-yy"
    forDate=DateTime.Date(DateTime.now)
   
    Dim Tcr As String
    DateTime.DateFormat ="HH:mm:ss  dd-MM-yyyy"
    Tcr = DateTime.Date(DateTime.now)
   
'    Dim Qstr As String
   

    If DateTime.now > lastPreviewSaved + IntervalMs Then
                Dim jpeg() As Byte = camEx.PreviewImageToJpeg(PreviewPic, 90)
                'Save the image now
                Dim tempstr As String
                tempstr = forDate & "_" & Hostname & "_" '& "_" &  "0.0_0.0"
                Dim Filename As String = tempstr & ".jpeg"   
                camEx.SavePictureToFile(jpeg, File.DirRootExternal  & "/aaa/Images/",Filename)
                'Create the output folder if it does not exist
                File.MakeDir(File.DirRootExternal,"Download/aaa/Images/")
                btmp.InitializeMutable(640,480)
                Canvas1.Initialize2(btmp)
                Dim Bitmap1 As Bitmap
                Bitmap1.Initialize(File.DirRootExternal & "/aaa/Images/",Filename)

                Dim Rect1 As Rect
                Rect1.Initialize(0,0,btmp.Width ,btmp.Height)
                Canvas1.DrawRect(Rect1, Colors.Gray,True, 1dip)
                Canvas1.DrawBitmap(Bitmap1,Null,Rect1)
                Canvas1.DrawLine(btmp.Width -270,btmp.Height -20, btmp.Width -2, btmp.Height -20, Colors.Black , 16dip)
                'Canvas1.DrawLine(
                Canvas1.DrawText(Hostname & " - " & Tcr,btmp.Width -270,btmp.Height -15,Typeface.DEFAULT,15,Colors.White,"LEFT")
                Dim out As OutputStream
                out = File.OpenOutput(File.DirRootExternal, "/aaa/Images/" & tempstr & ".jpg", False)
                Canvas1.Bitmap.WriteToStream(out,45,"JPEG")
                   
                out.Close
                File.Delete (File.DirRootExternal & "/aaa/Images/",tempstr & ".jpeg")

                Dim Filename As String = tempstr & ".jpg"   
   
            lastPreviewSaved = DateTime.now
           
    End If
Catch
Log(LastException.Message)
End Try

End Sub
 

eps

Expert
Licensed User
Longtime User
You use DateTime.Now 3 times in quick succession, why not get it once and reuse? That would help a little, but not a great deal.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
It might also be quicker to use StringBuilder to build the strings, otherwise (afaik) there is a lot more background processing going on.
 
Upvote 0

sangee

Member
Licensed User
Longtime User
MakeDir outside the routine?
Any creation and accesses to files, if it is possible.
Yes. the program will need to ensure a directory called downloads/aaa also exists for the user to see the progress.

Thanks for the good suggestions... stringbuilder will be a useful one...
 
Upvote 0
Top