I am using the HttpServer library and want to create JPG and/or PNG images on the fly and return them from the HttpServer HandleRequest event handling sub.
The ServletResponse object has SendFile and SendString methods - i think i need a SendBytes method.
This is my HandleRequest sub:
So i have a Bitmap named Tile that i wish to serve as a JPG or PNG image, currently i'm writing it to a file and using the ServletResponse SendFile method to serve it.
Ideally i'd be able to use WriteToStream to write the image to an OutputStream then send the OutputStream bytes with no need for the temporary file.
Is this possible - can a SendBytes method be added to the ServletResponse?
Thanks.
Martin.
The ServletResponse object has SendFile and SendString methods - i think i need a SendBytes method.
This is my HandleRequest sub:
B4X:
Sub HttpServer1_HandleRequest (Request As ServletRequest, Response As ServletResponse)
Dim Paths() As String=Regex.Split("/", Request.RequestURI)
If Paths.Length=5 AND Paths(1)="tile" Then
' a tile request to serve
Dim Zoom As Int=Paths(2)
Dim TileX As Int=Paths(3)
Dim Temp() As String=Regex.Split("\.", Paths(4))
Dim TileY As Int=Temp(0)
Dim TileType As String="PNG"
If Temp(1)="jpg" Then
TileType="JPEG"
End If
Dim North As Double=Mercator1.YToLat((TileY*256)-CIRCLE_RADIUS, Zoom)
Dim South As Double=Mercator1.YToLat(((TileY+1)*256)+CIRCLE_RADIUS, Zoom)
Dim West As Double=Mercator1.XToLon((TileX*256)-CIRCLE_RADIUS, Zoom)
Dim East As Double=Mercator1.XToLon(((TileX+1)*256)+CIRCLE_RADIUS, Zoom)
Dim SpatialiteStatement As Spatialite_Stmt
SpatialiteStatement=SpatialiteDatabase.Prepare("SELECT X(Geometry) AS Longitude, Y(Geometry) AS Latitude FROM "&TABLE_NAME&" WHERE MBRWithin(Geometry, BuildMBR(?, ?, ?, ?, ?));") ' ORDER BY?
SpatialiteStatement.BindDouble(1, West)
SpatialiteStatement.BindDouble(2, North)
SpatialiteStatement.BindDouble(3, East)
SpatialiteStatement.BindDouble(4, South)
SpatialiteStatement.BindInt(5, SRID)
Dim Tile As Bitmap
Tile.InitializeMutable(256, 256)
Dim Canvas1 As Canvas
Canvas1.Initialize2(Tile)
Dim X As Int
Dim Y As Int
Dim Latitude As Double
Dim Longitude As Double
Dim Count As Int=0
Do While SpatialiteStatement.Step
Count=Count+1
Latitude=SpatialiteStatement.ColumnDouble(1)
Longitude=SpatialiteStatement.ColumnDouble(0)
X=Mercator1.LonToX(Longitude, Zoom)-(TileX*256)
Y=Mercator1.LatToY(Latitude, Zoom)-(TileY*256)
Canvas1.DrawCircle(X, Y, CIRCLE_RADIUS, CIRCLE_COLOR, CIRCLE_FILL, CIRCLE_STROKE_WIDTH)
Loop
Log("Count: "&Count)
' here i have to save the image to a temp file
TileId=TileId+1
Dim TempFileName As String="tile-"&TileId&"."&Temp(1)
Dim TempFile As OutputStream=File.OpenOutput(File.DirInternalCache, TempFileName, False)
Tile.WriteToStream(TempFile, 100, TileType)
TempFile.Close
If TileType="PNG" Then
Response.SetContentType("image/png")
Else
Response.SetContentType("image/jpeg")
End If
Response.SendFile(File.DirInternalCache, TempFileName)
File.Delete(File.DirInternalCache, TempFileName)
Return
End If
Response.SetContentType("text/html")
Response.SendString("No valid tile request received")
End Sub
So i have a Bitmap named Tile that i wish to serve as a JPG or PNG image, currently i'm writing it to a file and using the ServletResponse SendFile method to serve it.
Ideally i'd be able to use WriteToStream to write the image to an OutputStream then send the OutputStream bytes with no need for the temporary file.
Is this possible - can a SendBytes method be added to the ServletResponse?
Thanks.
Martin.