iOS Question ImageView improperly Rotates when drawing on it's canvas (+1 other problem)

JohnC

Expert
Licensed User
Longtime User
Below is a simple app to take a picture using the LL Camera Lib and then allow the user to draw on the image after it's taken.

Problem #1: For some reason if you run the app in portrait mode, take the picture in portrait and then try to draw on the taken picture all while still in portrait mode, the image view will suddenly rotate 90 degrees to the right for some strange reason.

Problem #2: As you can see, this module is called from another module (because it has the Show method). If when I call this module when in "Landscape" mode, the preview image sometimes does NOT display a "Landscape" image - the image will be a stretched portrait view of what the camera sees.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.

    Private llc As LLCamera
    Private pg As Page

    Private currentFlash As Int
    Private currentPreset As String
    Private frontCamera As Boolean = False

    Private cmdSnap As Button
    Private imgPic As ImageView
    Private pnlPreview As Panel

    Private CVS As Canvas
    Private DrawMode As Boolean
    Dim px, py As Int

End Sub

Public Sub Show

    If pg.IsInitialized = False Then
        pg.Initialize("pg")
        pg.RootPanel.Color = Colors.White
        pg.RootPanel.LoadLayout("camera")
    End If
    Main.NavControl.ShowPage(pg)

    currentFlash = llc.FLASH_OFF
    currentPreset = llc.PRESET_PHOTO

    InitializeCamera(frontCamera)

End Sub

Private Sub pg_Resize(Width As Int, Height As Int)
    Log("pg_Resize")
    llc.Resize
End Sub

Sub InitializeCamera(front As Boolean)
    If llc.IsInitialized Then llc.StopPreview
    llc.Initialize(pnlPreview, "llc", front)

    DrawMode = False
    imgPic.Visible = False
    pnlPreview.Visible = True
    pnlPreview.BringToFront
    cmdSnap.BringToFront
    llc.StartPreview
    currentPreset = llc.Preset
    ConfigureCamera
End Sub

Private Sub ConfigureCamera
    Try
        llc.BeginConfiguration
        llc.FlashMode = currentFlash
        llc.Zoom = 1 + (Min(llc.ZOOM_MAX, 5) - 1) * 1 / 100
        llc.PreserveRatio = False
        llc.CommitConfiguration
    Catch
        Log("Error configuring camera: " & LastException.Description)
    End Try
End Sub

Sub cmdSnap_Click
    Try
        llc.TakePicture
    Catch
        Log(LastException)
    End Try
End Sub

Sub llc_PictureTaken (Data() As Byte)
    If Data.Length = 0 Then Return
    Log("PictureTaken")

    Dim In As InputStream
    In.InitializeFromBytesArray(Data, 0, Data.Length)
    Dim bmp As Bitmap
    bmp.Initialize2(In)
    In.Close

    imgPic.Bitmap = bmp
    llc.StopPreview
    pnlPreview.Visible = False
    imgPic.Visible = True
    imgPic.BringToFront
    Log("Image size: " & bmp.Width & "x" & bmp.Height)

    DoPaint    'start finger draw mode

End Sub

Sub pnlPreview_Resize (Width As Float, Height As Float)
    Log("pnlPreview_Resize")
    llc.Resize
End Sub

Sub DoPaint

    CVS.Initialize(imgPic)
    DrawMode = True

End Sub

Sub pg_Touch(Action As Int, tx As Float, ty As Float)
    If DrawMode = False Then Return

    If Action = 0 Then 'mouse down constant
        px = tx
        py = ty
    Else
        CVS.DrawLine(px+1, py+1, tx+1, ty+1, Colors.Black, 2dip)
        CVS.DrawLine(px, py, tx, ty, Colors.Yellow, 2dip)
        CVS.Refresh
        px = tx
        py = ty
    End If

End Sub
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
Here it is...
 

Attachments

  • Demos.zip
    9.1 KB · Views: 300
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The orientation issue seems to be related to the way ImageView handles the image. Instead of setting the bitmap and then initializing the canvas over the bitmap, you should draw the bitmap with the canvas:
B4X:
Sub llc_PictureTaken (Data() As Byte)
    If Data.Length = 0 Then Return
    Log("PictureTaken")
    
    Dim In As InputStream
    In.InitializeFromBytesArray(Data, 0, Data.Length)
    Dim bmp As Bitmap
    bmp.Initialize2(In)
    In.Close
    llc.StopPreview
    pnlPreview.Visible = False
    imgPic.Visible = True
    imgPic.BringToFront
    Log("Image size: " & bmp.Width & "x" & bmp.Height)
    
    CVS.Initialize(imgPic)
    DrawMode = True
    CVS.DrawBitmap(bmp, CVS.TargetRect) 
    CVS.Invalidate
End Sub

CVS is a B4XCanvas. Better to use it instead of the platform specific Canvas.

You should release the canvas in Page_Disappear.

I wasn't able to reproduce the second issue you described.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Thank you - that works great.
 
Upvote 0
Top