Android Question Camex preview to image file ?

Devv

Active Member
Licensed User
Longtime User
thanks for fast replay bro
i tried the code in the link you provided but the result was some difference between the preview and the saved file . + i couldn't control when to save the image cuz
camera1_Preview(Data() As Byte) is always running and i couldn't even stop it .




any other methods ?
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Look HERE. It uses cameraEX class plus a small add-on and converts the camera preview to a RGB bitmap on each preview event (already rotated when needed)

In your case you don't need the bitmap to be generated each time, only when you want to save it.

So you have to modify it slightly:

  • Set parameter myscale=1
  • Set myIV.visible=False (don't need it)
  • Declare a global flag that you will use to take the preview picture each time you want
B4X:
Sub Globals
   Dim flg_take_picture as Boolean=False
   ....

  • Change Camera1_Preview(...) for this one
B4X:
Sub Camera1_Preview (PreviewPic() As Byte)

    'prevent queued events from previous camera settings, if any. Just in case
    If PreviewPic.Length<>(3*myBitmap.Width*myScale*myBitmap.Height*myScale/2) Then
        Log("Not processing")
        Return
    End If

    If flg_take_picture=True Then 'Set it to true externally when you want to take a preview picture
        flg_take_picture=False
        NV21toRGB.proceed( PreviewPic, myBitmap.Width*myScale, myBitmap.Height*myScale, myScale, myBitmap, camVertical , camEx.Front, camEx.DispRotation, myIndexEffect )
        Dim b2 As Bitmap

        ' Call these two lines if you want the bitmap to be saved with the same dimensions as the preview panel
        b2=ScaleToPanelDims( myBitmap)   
        SaveBitmap(b2,File.DirRootExternal,"test.jpg")

        'Otherwise, if you want to save with the original preview width and height, just call this
        'SaveBitmap(myBitmap,File.DirRootExternal,"test.jpg")
    End If
End Sub


  • and add the following 2 Subs
B4X:
Sub ScaleToPanelDims( b As Bitmap) As Bitmap
    Dim bscaled As Bitmap
    bscaled.initializeMutable(Panel1.Width,Panel1.Height)
    Dim c As Canvas
    c.Initialize2(bscaled)
    Dim R As Rect
    R.Initialize(0,0,bscaled.Width,bscaled.Height)
    c.DrawBitmap(b,Null,R)
    Return bscaled
End Sub

Sub SaveBitmap (b As Bitmap, myDir As String, myFileName As String)
    Dim out As OutputStream
    out = File.OpenOutput(myDir, myFileName, False)
    b.WriteToStream(out, 100, "JPEG")
    out.Flush
    out.Close
End Sub
 
Upvote 0
Top