Android Question Camex preview to image file ?


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


Well-Known Member
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
Sub Globals
   Dim flg_take_picture as Boolean=False

  • Change Camera1_Preview(...) for this one
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")
    End If

    If flg_take_picture=True Then 'Set it to true externally when you want to take a preview picture
        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)   

        'Otherwise, if you want to save with the original preview width and height, just call this
    End If
End Sub

  • and add the following 2 Subs
Sub ScaleToPanelDims( b As Bitmap) As Bitmap
    Dim bscaled As Bitmap
    Dim c As Canvas
    Dim R As Rect
    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")
End Sub
Upvote 0