Android Question Is there a camera library that doesn't use a view?

NeoTechni

Well-Known Member
Licensed User
Longtime User
To keep it simple, I want to use the camera in a game engine which uses no views/panels at all.
I'd like a way to sample either the front/rear camera at a set resolution, and it just returns a bitmap or a canvas.
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
Dang. I didn't want to hard code a reference to the activity from a module but I guess I'll have to. Thank you
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
How do I get the image from the panel, as a bitmap?
I need to store the current and previous frame

I've been using

BG2.Initialize(SRC)
BG.DrawBitmap( BG2.Bitmap , Null, SetRect(0,0, SRC.Width,SRC.Height))

where bg and bg2 are canvases, and SRC is the panel
BG is initialized with one of the 2 frame bitmaps (I flip between them each frame)

The image I get is black however...
I also tried SRC.Background and that came back null..

EDIT: Trying to convert it to bitmapdrawable then bitmap.
Nope, src.background is null/not initialized despite me seeing the camera preview.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The panel background will not help in this case. The images are drawn directly from the camera to a SurfaceView that is a child of the panel.

You should handle the Preview event (uncomment it in CameraEx class) and call PreviewImageToJpeg to convert the data to an image.

You should do it every few frames. See the CCTV for an example.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Couldn't use CameraEx, the ready event refused to fire on my nexus 5 (im thinking cause the panel wasnt visible, as that caused problems in the ACL lib). And converting it to JPEG sounds like a lot of overhead, and this is already dragging my framerate down as it is. What I'm trying to do won't work with a low framerate.

Is there no way to get the BMP from the surfaceview?

EDIT: Yup. The camera libraries require visible panels. ;_;
 
Last edited:
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
What format is that preview data in?
Just a long array of R G B bytes? Cause I can just use that.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Dang, I was wrong. I took your save as jpeg sub and cut off the jpeg part.

B4X:
Public Sub PreviewImageToYUB(data() As Byte) As bitmap
    Dim size, previewFormat As Object, BMP as bitmap
    r.target = parameters
    size = r.RunMethod("getPreviewSize")
    previewFormat = r.RunMethod("getPreviewFormat")
    Log("previewFormat: " & previewFormat)
    r.target = size
    Dim width = r.GetField("width"), height = r.GetField("height") As Int
    Dim yuvImage As Object = r.CreateObject2("android.graphics.YuvImage", Array As Object(data, previewFormat, width, height, Null), Array As String("[B", "java.lang.int", "java.lang.int", "java.lang.int", "[I"))
    
'How do I go from a yuvImage to a Bitmap?

    Return BMP
End Sub
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Yup, this reduces the framerate to 1. ;_; and the preview orientation doesnt match the device's
 
Upvote 0
Top