diff --git a/software/.gitignore b/software/.gitignore index 35578c5..58b1042 100644 --- a/software/.gitignore +++ b/software/.gitignore @@ -2,3 +2,4 @@ ENV *.pyc __pycache__ .pytest_cache +OpenBookScannerApp_ diff --git a/software/OpenBookScannerApp/.gitignore b/software/OpenBookScannerApp/.gitignore new file mode 100644 index 0000000..4201aac --- /dev/null +++ b/software/OpenBookScannerApp/.gitignore @@ -0,0 +1,78 @@ +*.iml +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +/.idea/caches +.DS_Store +/build +/captures +.externalNativeBuild + +# from https://github.com/github/gitignore/blob/master/Android.gitignore + +# Built application files +*.apk +*.ap_ + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +*.iml +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries +.idea/libraries +.idea/caches + +# Keystore files +# Uncomment the following line if you do not want to check your keystore files in. +#*.jks + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md diff --git a/software/OpenBookScannerApp/.idea/codeStyles/Project.xml b/software/OpenBookScannerApp/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/software/OpenBookScannerApp/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/software/OpenBookScannerApp/.idea/gradle.xml b/software/OpenBookScannerApp/.idea/gradle.xml new file mode 100644 index 0000000..7ac24c7 --- /dev/null +++ b/software/OpenBookScannerApp/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/software/OpenBookScannerApp/.idea/misc.xml b/software/OpenBookScannerApp/.idea/misc.xml new file mode 100644 index 0000000..99202cc --- /dev/null +++ b/software/OpenBookScannerApp/.idea/misc.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/software/OpenBookScannerApp/.idea/modules.xml b/software/OpenBookScannerApp/.idea/modules.xml new file mode 100644 index 0000000..f852883 --- /dev/null +++ b/software/OpenBookScannerApp/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/software/OpenBookScannerApp/.idea/runConfigurations.xml b/software/OpenBookScannerApp/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/software/OpenBookScannerApp/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/software/OpenBookScannerApp/.idea/vcs.xml b/software/OpenBookScannerApp/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/software/OpenBookScannerApp/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/software/OpenBookScannerApp/README.md b/software/OpenBookScannerApp/README.md new file mode 100644 index 0000000..cc79dbb --- /dev/null +++ b/software/OpenBookScannerApp/README.md @@ -0,0 +1,55 @@ +# OpenBookScannerApp + +This Android App takes the pictures and registers as a scanner. +You can develop this app with Android Studio 3.1.3. +This app is compatible with old phones until Android 2.3. + +## API + +The OpenBookScannerApp requires this API endpoint: + +- `POST /scanner` with data + Headers: + - `Content-Type: application/json` + ``` + { + "type": "scanner", + "name": "", + "id": "random id for this run of the app" + } + ``` + The attributes have the following meaning: + - `type` is `"scanner"` because this is what the app is going to do. + - `name` is the name of the device which should be displayed. + - `id` is the id of the scanner as there might be multiple devices of the same name. + This results in `200`. + Headers: + - `Content-Type: application/json` + - if no picture shall be taken: + ``` + { + "status": "ok", + "refresh": 0.5 + } + ``` + - if a picture shall be taken: + ``` + { + "status": "ok", + "refresh": SECONDS, + "picture": "" + ``` + These are the values explained: + - `status` should be `"ok"`. Otherwise, there was an error. + - `refresh` is the seconds as float + when to re-post to keep the scanner active. + - `picture` is the full url including `http://hostname:port/path` where + to post the picture once taken. + Method: `POST` + Headers: + - `Content-Type: image/jpeg` + +## Thanks + +- Big thanks to [isnotmenow](https://github.com/isnotmenow/AndroidProjectForAPI9) + for the [APIv9 app](https://github.com/isnotmenow/AndroidProjectForAPI9). diff --git a/software/OpenBookScannerApp/app/.gitignore b/software/OpenBookScannerApp/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/software/OpenBookScannerApp/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/software/OpenBookScannerApp/app/build.gradle b/software/OpenBookScannerApp/app/build.gradle new file mode 100644 index 0000000..f61d4bb --- /dev/null +++ b/software/OpenBookScannerApp/app/build.gradle @@ -0,0 +1,33 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 25 + defaultConfig { + applicationId "com.github.fablabcb.openbookscannerapp" + minSdkVersion 9 + targetSdkVersion 25 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'com.android.support:support-v4:25.4.0' + implementation 'com.android.support:appcompat-v7:25.4.0' + implementation 'com.android.support.constraint:constraint-layout:1.0.2' + // implementation 'com.parse:parse-android:1.17.3' // http://docs.parseplatform.org/android/guide/ needs version 14+ + testImplementation 'junit:junit:4.12' + + // this is the last support test for api >= 9 + androidTestImplementation 'com.android.support.test:runner:1.0.0' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' + implementation 'com.android.support:design:25.4.0' +} diff --git a/software/OpenBookScannerApp/app/proguard-rules.pro b/software/OpenBookScannerApp/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/software/OpenBookScannerApp/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/software/OpenBookScannerApp/app/src/androidTest/java/com/github/fablabcb/openbookscannerapp/ExampleInstrumentedTest.java b/software/OpenBookScannerApp/app/src/androidTest/java/com/github/fablabcb/openbookscannerapp/ExampleInstrumentedTest.java new file mode 100644 index 0000000..81f399a --- /dev/null +++ b/software/OpenBookScannerApp/app/src/androidTest/java/com/github/fablabcb/openbookscannerapp/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.github.fablabcb.openbookscannerapp; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.github.fablabcb.openbookscannerapp", appContext.getPackageName()); + } +} diff --git a/software/OpenBookScannerApp/app/src/main/AndroidManifest.xml b/software/OpenBookScannerApp/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..3351fa1 --- /dev/null +++ b/software/OpenBookScannerApp/app/src/main/AndroidManifest.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/software/OpenBookScannerApp/app/src/main/java/com/github/fablabcb/openbookscannerapp/MainActivity.java b/software/OpenBookScannerApp/app/src/main/java/com/github/fablabcb/openbookscannerapp/MainActivity.java new file mode 100644 index 0000000..647a2e1 --- /dev/null +++ b/software/OpenBookScannerApp/app/src/main/java/com/github/fablabcb/openbookscannerapp/MainActivity.java @@ -0,0 +1,287 @@ +package com.github.fablabcb.openbookscannerapp; + +import android.Manifest; +import android.content.pm.PackageManager; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.hardware.Camera; +import android.hardware.Camera.CameraInfo; +import android.os.Build; +import android.os.Bundle; +import android.support.v4.app.ActivityCompat; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; +import android.widget.TextView; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.net.URLEncoder; +import java.util.UUID; + + +public class MainActivity extends AppCompatActivity { + + private final static String DEBUG_TAG = "MainActivity"; + private final static int DEFAULT_PORT = 8001; + + private ImageView imageView; + private TextView statusText; + private Button button; + private boolean cameraIsAvailable; + private boolean hasCameraFeature; + private boolean cameraIsDisabled; + private Camera camera; + private EditText addressIP; + private EditText addressPort; + private TextView serverStatus; + private UUID app_id; + + + + protected void setAttributesOnCreate() { + setContentView(R.layout.activity_main); + imageView = (ImageView) findViewById(R.id.imageView); + statusText = (TextView) findViewById(R.id.statusText); + button = (Button) findViewById(R.id.takePictureButton); + PackageManager pm = getApplicationContext().getPackageManager(); + addressIP = (EditText) findViewById(R.id.addressIP); + addressPort = (EditText) findViewById(R.id.addressPort); + serverStatus = (TextView) findViewById(R.id.serverStatus); + hasCameraFeature = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA); + // from https://stackoverflow.com/questions/2982748/create-a-guid-in-java#2982751 + app_id = java.util.UUID.randomUUID();; + } + + private int findFrontFacingCamera() { + // Search for the front facing camera + // from http://www.vogella.com/tutorials/AndroidCamera/article.html + int numberOfCameras = Camera.getNumberOfCameras(); + for (int cameraId = 0; cameraId < numberOfCameras; cameraId++) { + CameraInfo info = new CameraInfo(); + Camera.getCameraInfo(cameraId, info); + if (info.facing == CameraInfo.CAMERA_FACING_BACK) { + Log.d(DEBUG_TAG, "Camera found"); + return cameraId; + } + } + return -1; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Log.d(DEBUG_TAG, "onCreate"); + setAttributesOnCreate(); + + + + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + tryToTakeAPicture(); + } + }); + } + + private void tryToTakeAPicture() { + if (cameraIsAvailable) { + setCameraParameters(); + camera.startPreview(); + camera.takePicture(null, null, + new Camera.PictureCallback() { + @Override + public void onPictureTaken(byte[] data, Camera camera) { + Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); + imageView.setImageBitmap(bm); + camera.stopPreview(); + } + }); + } + notifyServer(); + } + + protected void setCameraParameters() { + // https://developer.android.com/reference/android/hardware/Camera.Parameters + // setJpegQuality(int quality) + // setPictureSize(int width, int height) + // getSupportedPictureSizes() + Camera.Parameters params = camera.getParameters(); + Camera.Size maxSize = params.getSupportedPictureSizes().get(0); + for (Camera.Size size : params.getSupportedPictureSizes()) { + if (size.width >= maxSize.width && size.height > maxSize.height) { + maxSize = size; + } + } + params.setPictureSize(maxSize.width, maxSize.height); + Log.d(DEBUG_TAG, "Camera resolution: " + maxSize.width + "x" + maxSize.height + "px"); + params.setJpegQuality(100); // no compression + camera.setParameters(params); + } + + @Override + protected void onPause() { + // from http://www.vogella.com/tutorials/AndroidCamera/article.html + // see also https://developer.android.com/reference/android/hardware/Camera (10) + if (cameraIsAvailable) { + camera.release(); + camera = null; + } + super.onPause(); + } + + @Override + protected void onResume() { + // from https://developer.android.com/reference/android/hardware/Camera (10) + + openCamera(); + statusText.setText(cameraIsAvailable ? R.string.camera_status_available : + cameraIsDisabled ? R.string.camera_status_disabled : + !hasCameraFeature ? R.string.camera_status_no_feature : R.string.TODO); + super.onResume(); + } + + private void openCamera() { + if (hasCameraFeature) { + int cameraId = findFrontFacingCamera(); + try { + camera = Camera.open(cameraId); + cameraIsDisabled = false; + } catch (RuntimeException e) { + // this occurs if no permissions are given, see thedocumentation of Camera.open. + cameraIsDisabled = true; + } + } + cameraIsAvailable = hasCameraFeature && !cameraIsDisabled; + if (cameraIsDisabled) { + // from https://androidkennel.org/android-camera-access-tutorial/ + // imageView.setImageResource(); // TODO: change picture + ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, 0); + } + } + + public int getPort() { + // from https://stackoverflow.com/a/25804635/1320237 + try { + return Integer.parseInt(addressPort.getText().toString()); + } catch (NumberFormatException e) { + return DEFAULT_PORT; + } + } + + public String getHostname() { + return addressIP.getText().toString(); + } + + public URL getServerUrl() throws MalformedURLException { + return new URL("http", getHostname(), getPort(), "/scanner"); + } + + protected void notifyServer() { + URL url; + setServerStatus(R.string.server_status_starting_request); + try { + url = getServerUrl(); + } catch (MalformedURLException e) { + e.printStackTrace(); + setServerStatus(R.string.server_status_malformed_url); + return; + } + Log.d(DEBUG_TAG, "URL: " + url.toString()); + // see https://www.wikihow.com/Execute-HTTP-POST-Requests-in-Android + HttpURLConnection client; + try { + client = (HttpURLConnection) url.openConnection(); + } catch (IOException e) { + setServerStatus(R.string.server_status_connection_error); + e.printStackTrace(); + return; + } + try { + client.setRequestMethod("POST"); + } catch (ProtocolException e) { + setServerStatus(R.string.server_status_protocol_exception); + e.printStackTrace(); + return; + } + // from https://stackoverflow.com/a/34555324/1320237 + client.addRequestProperty("Accept", "application/json"); + client.addRequestProperty("Content-Type", "application/json"); + client.setDoOutput(true); + client.setChunkedStreamingMode(0); + // write data to the server + DataOutputStream os; + // from https://stackoverflow.com/q/42767249/1320237 + JSONObject notification = new JSONObject(); + try { + notification.put("type", "scanner"); + notification.put("name", getDeviceName()); + notification.put("id", app_id.toString()); + } catch (JSONException e) { + setServerStatus(R.string.server_status_create_notification_exception); + e.printStackTrace(); + return; + } + try { + os = new DataOutputStream(client.getOutputStream()); + os.writeBytes(notification.toString()); + os.flush(); + os.close(); + } catch (IOException e) { + setServerStatus(R.string.server_status_send_notification_exception); + e.printStackTrace(); + return; + } + Log.d(DEBUG_TAG, "POST request sent."); + try { + Log.i("STATUS", String.valueOf(client.getResponseCode())); + Log.i("MSG" , client.getResponseMessage()); + } catch (IOException e) { + setServerStatus(R.string.server_status_get_server_response_error); + e.printStackTrace(); + return; + } + setServerStatus(R.string.server_status_get_server_response); + + } + + private void setServerStatus(int resourceId) { + serverStatus.setText(resourceId); + } + + public String getDeviceName() { + // from https://stackoverflow.com/a/12707479/1320237 + String manufacturer = Build.MANUFACTURER; + String model = Build.MODEL; + if (model.toLowerCase().startsWith(manufacturer.toLowerCase())) { + return capitalize(model); + } else { + return capitalize(manufacturer) + " " + model; + } + } + + + private String capitalize(String s) { + // form https://stackoverflow.com/a/12707479/1320237 + if (s == null || s.length() == 0) { + return ""; + } + char first = s.charAt(0); + if (Character.isUpperCase(first)) { + return s; + } else { + return Character.toUpperCase(first) + s.substring(1); + } + } + +} diff --git a/software/OpenBookScannerApp/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/software/OpenBookScannerApp/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..7daee77 --- /dev/null +++ b/software/OpenBookScannerApp/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/software/OpenBookScannerApp/app/src/main/res/drawable/ic_launcher_background.xml b/software/OpenBookScannerApp/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..4ea54ca --- /dev/null +++ b/software/OpenBookScannerApp/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,171 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/software/OpenBookScannerApp/app/src/main/res/layout/activity_main.xml b/software/OpenBookScannerApp/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..2925aab --- /dev/null +++ b/software/OpenBookScannerApp/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,109 @@ + + + + + + + + + + + + + + + + + + + +