Skip to content

Latest commit

 

History

History
106 lines (95 loc) · 5.03 KB

10-Ti.CameraParameters.md

File metadata and controls

106 lines (95 loc) · 5.03 KB

#Ti.CameraParameters

For some reason some people needs details about all embedded cameras in a phone. This module exposes all needed informations.

##Basics The module only statically ask the system, therefore we don't need a proxy – a module is enough. The walking thrue system needs time and is a good though to realize this as async task. Therefore we cannot work with result => we need callbacks.

##Implementation First we transfer the javascript callbacks to java KrollFunctions in the single exported method:

public void getAllCams(KrollDict opts) {
		if (opts != null && opts instanceof KrollDict) {
			if (opts.containsKeyAndNotNull("onSuccess")) {
				successCallback = (KrollFunction) opts.get("onSuccess");
			}
			if (opts.containsKeyAndNotNull("onError")) {
				errorCallback = (KrollFunction) opts.get("onError");
			}

If the javascript coder forget or mistyped, then nothing happens. ;-)) All the stuff runs in a second thread (async task):

	AsyncTask<Void, Void, Void> doRequest = new AsyncTask<Void, Void, Void>() {
				@Override
				protected Void doInBackground(Void[] dummy) {

The currentActivity we need for asking for permissions and the context we need for getting the Camera systemService.

Activity currentActivity = TiApplication.getInstance()
							.getCurrentActivity();
Context context = TiApplication.getInstance()
							.getApplicationContext();

For building of result object we need a KrollDict (resultDict) and for property 'cameras' an Arraylist (listOfCameras) object.

KrollDict resultDict = new KrollDict();
ArrayList<KrollDict> listOfCameras = new ArrayList<KrollDict>();

For newest devices with Marshmellow we need to ask by CAMERA permissions:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
	if (currentActivity .checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_DENIED) {
		resultDict.put("error","No permission to access camera manager.");
			if (errorCallback != null) errorCallback.call(getKrollObject(), resultDict);
	}
}

If permission is not granted, the function will stopped and the user gets an error callback.

Unfortunately we need different APIs for older and newer Android levels. This depend on level on device, not on compiler sdk level.

For older devices the 'android.hardware.Camera' API is right. It is relative simple:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
	int countOfCams = android.hardware.Camera.getNumberOfCameras();
	resultDict.put("count", countOfCams);
	resultDict.put("api", "android.hardware.Camera");
	for (int i = 0; i < countOfCams; i++) {
		KrollDict dict = new KrollDict();
		Camera cam = android.hardware.Camera.open(i);
		android.hardware.Camera.Parameters parameters = cam.getParameters();
		String flashMode = parameters.getFlashMode();
		android.hardware.Camera.Size size = parameters.getPictureSize();
		dict.put("pixelResolution", size.width + "×"	+ size.height);
		dict.put("flashAvailable",(flashMode != null) ? true : false);
		listOfCameras.add(dict);
		cam.release();
	}
}

Dont forget to release()!

For newer API levels the Camera API is deprecated and we use android.hardware.camera2. The API knows 87 properties, we use a little part: Because we use the CameraManager and do not open the Camara by self, we don't need to release or close some stuff. 'getCameraIdList()' returns a list of Strings. In the end we ask the CameraManager by characterics.

try {
	Context context = TiApplication.getInstance().getApplicationContext();
	CameraManager cameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
	resultDict.put("count",cameraManager.getCameraIdList().length);
	resultDict.put("api", "android.hardware.camera2");
	for (String id : cameraManager.getCameraIdList()) {
		CameraCharacteristics character = cameraManager.getCameraCharacteristics(id);
		Boolean flashAvailable = character.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
		SizeF physicalsize = character.get(CameraCharacteristics.SENSOR_INFO_PHYSICAL_SIZE);
		Rect activeArray = character.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
		int cOrientation = character.get(CameraCharacteristics.LENS_FACING);

		KrollDict dict = new KrollDict();
		dict.put("orientation",(cOrientation == CameraCharacteristics.LENS_FACING_FRONT) ? "front": "rear");
		dict.put("flashAvailable", flashAvailable);
		dict.put("pixelResolution", activeArray.width() + "×" + activeArray.height());
		dict.put("megapixel", ((double) activeArray.width() * (double) activeArray.height()) / 1000000.0f);
		dict.put("chipSize", physicalsize.getWidth() + "×" + physicalsize.getHeight());
		listOfCameras.add(dict);
	}
} catch (CameraAccessException e) {
	Log.e(LCAT, "Failed to interact with camera.", e);
	resultDict.put("error","Failed to interact with camera.");
	if (errorCallback != null) errorCallback.call(getKrollObject(), resultDict);
}