Camera sourcing doesn't stop.

Hey all in my painting app there’s a option to take a picture using the back camera. Camera input requires about 30 fps itself but then after camera sourcing the camera source continues drawing input from the camera and I still remain at 30fps without even showing the camera. I tried cameraSource(0) which seemed to work before, but now it doesn’t… Does anyone know how to stop camera feed after it has started?

@Luatee I tried some camera code that I have. I added FPS to one of them and started the camera. My FPS remained at 60 FPS as I viewed the live images. I switched from back to front with no change in speed. I couldn’t find anyway to turn off the cameraSource either, but still at 60 FPS it didn’t seem to matter. Are you saying your FPS drops to 30 when you use your camera. Here’s the code I used to check FPS.

function setup()
    parameter.action("Front Camera",function() cameraSource(CAMERA_FRONT) end)
    parameter.action("Back Camera",function() cameraSource(CAMERA_BACK) end)
    parameter.action("Take picture",function() img=image(CAMERA) end)
    parameter.action("Clear",function() img=nil end)

function draw()
    background(40, 40, 50)
    if img~=nil then
    tot = tot + 1/DeltaTime
    count = count + 1

@Luatee, bit of an amateurish guess, but are you saving the camera image to a local image variable or a global? If local, then collectgarbage() when leaving the function might clear it?

@dave1707 I will get the code for you in a minute and put it up.
@time_trial I do need to make a garbage collection somewhere but it’s not related to this I don’t think. I’m thinking when the best time to collect garbage is.

Drawing (the slow down afaik)

if camalpha > 0 and tool == "camera" then
        camalpha = camalpha - 5
    elseif camalpha < 0 then
        camalpha = 0
    if tool == "camera" and cam then
        local w,h = spriteSize(CAMERA)
    if tool ~= "camera" and cam then
        cam = false

It’s a bit odd to me for this to happen…
Edit: garbage collection does nothing, text tool seems to add to the slow down, the only tools I can identify with this problem.

@Luatee Did you run my example above to see what the FPS is for you. I’m not seeing what’s going on with your code to slow things down.

Me neither thats the problem I’m having! But the reason for the drop isn’t the camera actually now I’ve used your test, it’s the fact I’m using setContext instead of drawing straight, but I would rather use setContext as that’s what the engine for the drawing is based around, for everything.

Turns out putting the drawing part in its own function stopped the slow down… How does that work?

@Luatee: Is the original function very large?

Well the draw function is about 1000 lines?

I was thinking there was maybe a register exhaustion issue, but looking at the Lua parser and VM source, that doesn’t seem possible (if you run out of registers/localvars you just get an error, I think). I suppose there could be caching issue, though that seems unlikely.

@daveedvdv I think a bit of it might have had to do with the lack of garbage collection which is now fixed, but it still occurred afterwards until I put it in a function.