Skip to content

Commit 618b498

Browse files
committed
Handle storage permissions. Require artoolkitX v1.1.12.
1 parent 3c87c83 commit 618b498

File tree

7 files changed

+150
-53
lines changed

7 files changed

+150
-53
lines changed

Android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<uses-permission android:name="android.permission.CAMERA" />
88
<uses-permission android:name="android.permission.INTERNET" />
99
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
10+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1011

1112
<uses-feature android:name="android.hardware.camera.any" />
1213
<uses-feature android:name="android.hardware.camera"

Android/app/src/main/cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ set(SOURCE
107107
${ARTOOLKITX_CAMERA_CALIBRATION_HOME}/version.h
108108
${ARTOOLKITX_CAMERA_CALIBRATION_HOME}/calib_camera.cpp
109109
${ARTOOLKITX_CAMERA_CALIBRATION_HOME}/calib_camera.h
110+
${ARTOOLKITX_CAMERA_CALIBRATION_HOME}/calib_camera_jni.cpp
110111
${ARTOOLKITX_CAMERA_CALIBRATION_HOME}/Calibration.hpp
111112
${ARTOOLKITX_CAMERA_CALIBRATION_HOME}/Calibration.cpp
112113
${ARTOOLKITX_CAMERA_CALIBRATION_HOME}/calc.cpp
Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
1+
/*
2+
* CameraCalibrationJNI.java
3+
* artoolkitX
4+
*
5+
* This file is part of artoolkitX.
6+
*
7+
* artoolkitX is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* artoolkitX is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with artoolkitX. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* As a special exception, the copyright holders of this library give you
21+
* permission to link this library with independent modules to produce an
22+
* executable, regardless of the license terms of these independent modules, and to
23+
* copy and distribute the resulting executable under terms of your choice,
24+
* provided that you also meet, for each linked independent module, the terms and
25+
* conditions of the license of that module. An independent module is a module
26+
* which is neither derived from nor based on this library. If you modify this
27+
* library, you may extend this exception to your version of the library, but you
28+
* are not obligated to do so. If you do not wish to do so, delete this exception
29+
* statement from your version.
30+
*
31+
* Copyright 2023 Philip Lamb.
32+
*
33+
* Author(s): Philip Lamb
34+
*
35+
*/
36+
37+
//
38+
// Prototypes for a handful of entry points to allow the Java code to call into the
39+
// native side.
40+
// The native side of this interface is in the file camera_calibration_jni.cpp.
41+
//
42+
143
package org.artoolkitx.utilities.cameracalibration;
244

345
public class CameraCalibrationJNI {
446

547
public static native void handleBackButton();
648
public static native void handleAddButton();
7-
849
public static native void sendPreferencesChangedEvent();
950
}

Android/app/src/main/java/org/artoolkitx/utilities/cameracalibration/CameraCalibrationSettingsFragment.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
import android.view.LayoutInflater;
99
import android.view.View;
1010
import android.view.ViewGroup;
11-
import android.widget.BaseAdapter;
1211
import android.widget.EditText;
1312

1413
import androidx.annotation.NonNull;
1514
import androidx.annotation.Nullable;
1615
import androidx.preference.EditTextPreference;
1716
import androidx.preference.ListPreference;
18-
import androidx.preference.Preference;
1917
import androidx.preference.PreferenceFragmentCompat;
2018
import androidx.preference.SwitchPreferenceCompat;
2119

calib_camera.cpp

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,11 @@ int main(int argc, char *argv[])
645645
if (ar2VideoGetParami(vid, AR_VIDEO_PARAM_AVFOUNDATION_CAMERA_POSITION, &frontCamera) >= 0) {
646646
gCameraIsFrontFacing = (frontCamera == AR_VIDEO_AVFOUNDATION_CAMERA_POSITION_FRONT);
647647
}
648+
} else if (vid->module == AR_VIDEO_MODULE_ANDROID) {
649+
int frontCamera;
650+
if (ar2VideoGetParami(vid, AR_VIDEO_PARAM_ANDROID_CAMERA_FACE, &frontCamera) >= 0) {
651+
gCameraIsFrontFacing = (frontCamera == AR_VIDEO_ANDROID_CAMERA_FACE_FRONT);
652+
}
648653
}
649654
bool contentRotate90, contentFlipV, contentFlipH;
650655
if (gDisplayOrientation == 1) { // Landscape with top of device at left.
@@ -1238,30 +1243,23 @@ static void saveParam(const ARParam *param, ARdouble err_min, ARdouble err_avg,
12381243

12391244
AR2VideoParamT *vid = vs->getAR2VideoParam();
12401245
if (ar2VideoGetParams(vid, AR_VIDEO_PARAM_DEVICEID, &device_id) < 0 || !device_id) {
1241-
ARLOGe("Error fetching camera device identification.\n");
1246+
ARLOGe("Video input module does not support fetching device identification.\n");
12421247
}
12431248
if (ar2VideoGetParams(vid, AR_VIDEO_PARAM_NAME, &name) < 0 || !name) {
1244-
ARLOGe("Error fetching camera name.\n");
1249+
ARLOGi("Video input module does not support fetching input name.\n");
1250+
}
1251+
if (!device_id && !name) {
1252+
ARLOGw("Neither video input device identification nor name available.\n");
12451253
}
12461254

1247-
if (vid->module == AR_VIDEO_MODULE_AVFOUNDATION) {
1248-
int focalPreset;
1249-
ar2VideoGetParami(vid, AR_VIDEO_PARAM_AVFOUNDATION_FOCUS_PRESET, &focalPreset);
1250-
switch (focalPreset) {
1251-
case AR_VIDEO_AVFOUNDATION_FOCUS_MACRO:
1252-
focal_length = strdup("0.01");
1253-
break;
1254-
case AR_VIDEO_AVFOUNDATION_FOCUS_0_3M:
1255-
focal_length = strdup("0.3");
1256-
break;
1257-
case AR_VIDEO_AVFOUNDATION_FOCUS_1_0M:
1258-
focal_length = strdup("1.0");
1259-
break;
1260-
case AR_VIDEO_AVFOUNDATION_FOCUS_INF:
1261-
focal_length = strdup("1000000.0");
1262-
break;
1263-
default:
1264-
break;
1255+
double f;
1256+
if (ar2VideoGetParamd(vid, AR_VIDEO_PARAM_CAMERA_FOCAL_LENGTH, &f) < 0) {
1257+
ARLOGi("Video input module does not support fetching focal length.\n");
1258+
} else {
1259+
if (f > 0.0) {
1260+
if (asprintf(&focal_length, "%.3f", f) < 0) {
1261+
ARLOGe("Error writing focal length.\n");
1262+
}
12651263
}
12661264
}
12671265
if (!focal_length) {
@@ -1270,7 +1268,11 @@ static void saveParam(const ARParam *param, ARdouble err_min, ARdouble err_avg,
12701268
}
12711269

12721270
if (gCalibrationSave) {
1273-
1271+
#ifdef ANDROID
1272+
if (SDL_AndroidRequestPermission("android.permission.WRITE_EXTERNAL_STORAGE") != SDL_TRUE) {
1273+
ARLOGe("Error: Unable to write to external storage.\n");
1274+
} else {
1275+
#endif
12741276
// Assemble the filename.
12751277
char calibrationSavePathname[SAVEPARAM_PATHNAME_LEN];
12761278
snprintf(calibrationSavePathname, SAVEPARAM_PATHNAME_LEN, "%s/camera_para-", gCalibrationSaveDir);
@@ -1297,6 +1299,10 @@ static void saveParam(const ARParam *param, ARdouble err_min, ARdouble err_avg,
12971299
} else {
12981300
ARLOGi("Saved calibration to '%s'.\n", calibrationSavePathname);
12991301
}
1302+
#ifdef ANDROID
1303+
}
1304+
#endif
1305+
13001306
}
13011307

13021308
// Check for early exit.
@@ -1448,31 +1454,3 @@ static void saveParam(const ARParam *param, ARdouble err_min, ARdouble err_avg,
14481454
}
14491455
free(cachePath);
14501456
}
1451-
1452-
#ifdef ANDROID
1453-
#include <jni.h>
1454-
#define JNIFUNCTION_CC(sig) Java_org_artoolkitx_utilities_cameracalibration_CameraCalibrationJNI_##sig
1455-
1456-
extern "C" JNIEXPORT void JNICALL JNIFUNCTION_CC(handleBackButton(void))
1457-
{
1458-
flowHandleEvent(EVENT_BACK_BUTTON);
1459-
}
1460-
1461-
extern "C" JNIEXPORT void JNICALL JNIFUNCTION_CC(handleAddButton(void))
1462-
{
1463-
flowHandleEvent(EVENT_TOUCH);
1464-
}
1465-
1466-
extern "C" JNIEXPORT void JNICALL JNIFUNCTION_CC(sendPreferencesChangedEvent(void))
1467-
{
1468-
SDL_Event event;
1469-
SDL_zero(event);
1470-
event.type = gSDLEventPreferencesChanged;
1471-
event.user.code = (Sint32)0;
1472-
event.user.data1 = NULL;
1473-
event.user.data2 = NULL;
1474-
SDL_PushEvent(&event);
1475-
}
1476-
1477-
#endif
1478-

calib_camera_jni.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* calib_camera_jni.cpp
3+
* artoolkitX
4+
*
5+
* This file is part of artoolkitX.
6+
*
7+
* artoolkitX is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* artoolkitX is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with artoolkitX. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* As a special exception, the copyright holders of this library give you
21+
* permission to link this library with independent modules to produce an
22+
* executable, regardless of the license terms of these independent modules, and to
23+
* copy and distribute the resulting executable under terms of your choice,
24+
* provided that you also meet, for each linked independent module, the terms and
25+
* conditions of the license of that module. An independent module is a module
26+
* which is neither derived from nor based on this library. If you modify this
27+
* library, you may extend this exception to your version of the library, but you
28+
* are not obligated to do so. If you do not wish to do so, delete this exception
29+
* statement from your version.
30+
*
31+
* Copyright 2023 Philip Lamb.
32+
*
33+
* Author(s): Philip Lamb
34+
*
35+
*/
36+
37+
//
38+
// Presents a handful of entry points to allow the Java code to call into the
39+
// native side.
40+
// The Java side of this interface is in the class
41+
// org.artoolkitx.utilities.cameracalibration.CameraCalibrationJNI.
42+
//
43+
44+
#ifdef ANDROID
45+
46+
#include "flow.hpp"
47+
#include "calib_camera.h"
48+
49+
#include <jni.h>
50+
51+
#define JNIFUNCTION_CC(sig) Java_org_artoolkitx_utilities_cameracalibration_CameraCalibrationJNI_##sig
52+
53+
extern "C" {
54+
55+
JNIEXPORT void JNICALL JNIFUNCTION_CC(handleBackButton(void))
56+
{
57+
flowHandleEvent(EVENT_BACK_BUTTON);
58+
}
59+
60+
JNIEXPORT void JNICALL JNIFUNCTION_CC(handleAddButton(void))
61+
{
62+
flowHandleEvent(EVENT_TOUCH);
63+
}
64+
65+
JNIEXPORT void JNICALL JNIFUNCTION_CC(sendPreferencesChangedEvent(void))
66+
{
67+
SDL_Event event;
68+
SDL_zero(event);
69+
event.type = gSDLEventPreferencesChanged;
70+
event.user.code = (Sint32)0;
71+
event.user.data1 = NULL;
72+
event.user.data2 = NULL;
73+
SDL_PushEvent(&event);
74+
}
75+
76+
}
77+
#endif
78+

prefsAndroid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,6 @@ float getPreferencesCalibrationPatternSpacing(void *preferences)
310310

311311
char *getPreferenceCalibSaveDir(void *preferences)
312312
{
313-
return arUtilGetResourcesDirectoryPath(AR_UTIL_RESOURCES_DIRECTORY_BEHAVIOR_USE_USER_ROOT, NULL);
313+
return arUtilGetResourcesDirectoryPath(AR_UTIL_RESOURCES_DIRECTORY_BEHAVIOR_USE_APP_EXTERNAL_DATA_DIR, NULL);
314314
}
315315
#endif

0 commit comments

Comments
 (0)