Pong

November 11th 2014, Android JNI Torch

November 6th 2014 Android JNI | | November 12th 2014 Android JNI Revisited

In QML there is a “Torch” component:

import QtQuick 2.0
import QtMultimedia 5.3

Torch {
    power: 100
    enabled: true
}

The documentation says that it lets you switch the flash light led of your mobile phone’s camera on or off. The reality says, that it don’t work on Android, at least not on my LG L7II (all versions up to Qt 5.4).

The solution is to use Java code to switch the torch light on:

// Torch On
cam = Camera.open();
params = cam.getParameters();
List<String> flashModes = params.getSupportedFlashModes();
if (flashModes != null && flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH))
    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
else
    params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
cam.setParameters(params);
cam.setPreviewTexture(new SurfaceTexture(0));
cam.startPreview();
cam.autoFocus(new Camera.AutoFocusCallback() { public void onAutoFocus(boolean success, Camera cam) {} });

Note: The above code is delicate in such, that it has been tailored very carefully so that it is “working on most Android phones”. But the dev forums are full of discussions where a particular phone needs special treatment. For now we just assume that it works. On my LG L7II it indeed does!

Turning the torch off is reported to be working on all platforms (even on those platforms which had trouble turning it on, off course ;-):

// Torch Off
params = cam.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
cam.setParameters(params);
cam.stopPreview();
cam.release();

Using that Java code from inside Qt is achieved with the Qt AndroidExtras module as described on the previous page.

November 6th 2014 Android JNI | | November 12th 2014 Android JNI Revisited

Options: