mirror of
https://github.com/LightningMcK/schafkopfzaehler.git
synced 2025-02-05 13:36:09 +00:00
- Picture will be compressed, saved and send to server
- Realized with retrofit: * added api interface * Created POJO-Class to convert JSON Answer - Connection and sending to server works - TODO: * Wait for server to answer (lab set up takes way too long) * Get response and show it to user
This commit is contained in:
parent
3bca29767a
commit
d4b8fb8c52
BIN
.idea/caches/build_file_checksums.ser
generated
BIN
.idea/caches/build_file_checksums.ser
generated
Binary file not shown.
1
.idea/gradle.xml
generated
1
.idea/gradle.xml
generated
@ -9,7 +9,6 @@
|
|||||||
<set>
|
<set>
|
||||||
<option value="$PROJECT_DIR$" />
|
<option value="$PROJECT_DIR$" />
|
||||||
<option value="$PROJECT_DIR$/app" />
|
<option value="$PROJECT_DIR$/app" />
|
||||||
<option value="$PROJECT_DIR$/openCVLibrary343" />
|
|
||||||
</set>
|
</set>
|
||||||
</option>
|
</option>
|
||||||
<option name="resolveModulePerSourceSet" value="false" />
|
<option name="resolveModulePerSourceSet" value="false" />
|
||||||
|
@ -5,7 +5,7 @@ android {
|
|||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "project.schafkopfzaehler_2"
|
applicationId "project.schafkopfzaehler_2"
|
||||||
minSdkVersion 19
|
minSdkVersion 19
|
||||||
targetSdkVersion 27
|
targetSdkVersion 26
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "1.0"
|
versionName "1.0"
|
||||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||||
@ -19,10 +19,11 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation fileTree(include: ['*.jar'], dir: 'libs')
|
|
||||||
implementation 'com.android.support:appcompat-v7:27.1.1'
|
implementation 'com.android.support:appcompat-v7:27.1.1'
|
||||||
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
|
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
|
||||||
implementation 'com.android.support:design:27.1.1'
|
implementation 'com.android.support:design:27.1.1'
|
||||||
|
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
|
||||||
|
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
|
||||||
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
||||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package project.schafkopfzaehler_2;
|
package project.schafkopfzaehler_2;
|
||||||
|
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
@ -11,11 +13,22 @@ import android.widget.Button;
|
|||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.MultipartBody;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import project.schafkopfzaehler_2.POJO.POJOClass;
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Callback;
|
||||||
|
import retrofit2.Response;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
|
|
||||||
public class CardCaptureActivity extends AppCompatActivity {
|
public class CardCaptureActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private Camera mCamera;
|
private Camera mCamera;
|
||||||
@ -89,30 +102,73 @@ public class CardCaptureActivity extends AppCompatActivity {
|
|||||||
super.onStart();
|
super.onStart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Picture button was pressed. Send picture to server for card recognition. */
|
||||||
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
|
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPictureTaken(byte[] data, Camera camera) {
|
public void onPictureTaken(byte[] data, Camera camera) {
|
||||||
|
|
||||||
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
|
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
|
||||||
|
|
||||||
if (pictureFile == null){
|
if (pictureFile == null){
|
||||||
Log.d("CAMERA", "Error creating media file, check storage permissions");
|
Log.d("CAMERA", "Error creating media files");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// Compress image to lower site
|
||||||
FileOutputStream fos = new FileOutputStream(pictureFile);
|
Bitmap bmp = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
|
||||||
fos.write(data);
|
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||||
fos.close();
|
bmp.compress(Bitmap.CompressFormat.JPEG, 20, bytes);
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
Log.d("CAMERA", "File not found: " + e.getMessage());
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.d("CAMERA", "Error accessing file: " + e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
captureLog = findViewById(R.id.capture_log);
|
captureLog = findViewById(R.id.capture_log);
|
||||||
captureLog.setText("Karte scannen --> an Server senden");
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
FileOutputStream fos = new FileOutputStream(pictureFile);
|
||||||
|
fos.write(bytes.toByteArray());
|
||||||
|
fos.close();
|
||||||
|
Log.d("CAMERA", "Saved...");
|
||||||
|
captureLog.setText(captureLog.getText() + "\n" + "Saved...");
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
Log.d("CAMERA", "File not found: " + e.getMessage());
|
||||||
|
captureLog.setText(captureLog.getText() + "\n" + "File not found: " + e.getMessage());
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.d("CAMERA", "Error accessing file: " + e.getMessage());
|
||||||
|
captureLog.setText(captureLog.getText() + "\n" + "Error accessing file: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------ Send file to server -------------------------
|
||||||
|
Retrofit.Builder builder = new Retrofit.Builder().baseUrl("http://192.168.0.195:8000/").addConverterFactory(GsonConverterFactory.create());
|
||||||
|
|
||||||
|
Retrofit retrofit = builder.build();
|
||||||
|
|
||||||
|
api_server client = retrofit.create(api_server.class);
|
||||||
|
|
||||||
|
MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", pictureFile.getName(), RequestBody.create(MediaType.parse("image/*"), pictureFile));
|
||||||
|
|
||||||
|
Call<POJOClass> call = client.uploadAttachment(filePart);
|
||||||
|
|
||||||
|
try {
|
||||||
|
call.enqueue(new Callback<POJOClass>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<POJOClass> call, Response<POJOClass> response) {
|
||||||
|
String card = response.body().getKarte();
|
||||||
|
captureLog.setText(captureLog.getText() + "\n" + card + " erkannt :)");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<POJOClass> call, Throwable t) {
|
||||||
|
captureLog.setText(captureLog.getText() + "\n" + "Error..." + t.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (Exception e){
|
||||||
|
Log.d("Schafkopfzaehler", "EXCEPTION: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -134,25 +190,24 @@ public class CardCaptureActivity extends AppCompatActivity {
|
|||||||
// using Environment.getExternalStorageState() before doing this.
|
// using Environment.getExternalStorageState() before doing this.
|
||||||
|
|
||||||
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
|
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
|
||||||
Environment.DIRECTORY_PICTURES), "MyCameraApp");
|
Environment.DIRECTORY_PICTURES), "Schafkopfzaehler");
|
||||||
// This location works best if you want the created images to be shared
|
// This location works best if you want the created images to be shared
|
||||||
// between applications and persist after your app has been uninstalled.
|
// between applications and persist after your app has been uninstalled.
|
||||||
|
|
||||||
// Create the storage directory if it does not exist
|
// Create the storage directory if it does not exist
|
||||||
if (! mediaStorageDir.exists()){
|
if (! mediaStorageDir.exists()){
|
||||||
if (! mediaStorageDir.mkdirs()){
|
if (! mediaStorageDir.mkdirs()){
|
||||||
Log.d("MyCameraApp", "failed to create directory");
|
Log.d("Schafkopfzaehler", "failed to create directory");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a media file name
|
// Create a media file name
|
||||||
Long tsLong = System.currentTimeMillis()/1000;
|
|
||||||
String timeStamp = tsLong.toString();
|
|
||||||
File mediaFile;
|
File mediaFile;
|
||||||
if (type == MEDIA_TYPE_IMAGE){
|
if (type == MEDIA_TYPE_IMAGE){
|
||||||
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
|
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
|
||||||
"IMG_"+ timeStamp + ".jpg");
|
"Karte.jpg");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -162,3 +217,5 @@ public class CardCaptureActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
|
||||||
|
package project.schafkopfzaehler_2.POJO;
|
||||||
|
|
||||||
|
import com.google.gson.annotations.Expose;
|
||||||
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
|
||||||
|
public class POJOClass {
|
||||||
|
|
||||||
|
@SerializedName("safely executed")
|
||||||
|
@Expose
|
||||||
|
private Boolean safelyExecuted;
|
||||||
|
@SerializedName("Karte")
|
||||||
|
@Expose
|
||||||
|
private String karte;
|
||||||
|
|
||||||
|
public Boolean getSafelyExecuted() {
|
||||||
|
return safelyExecuted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSafelyExecuted(Boolean safelyExecuted) {
|
||||||
|
this.safelyExecuted = safelyExecuted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKarte() {
|
||||||
|
return karte;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKarte(String karte) {
|
||||||
|
this.karte = karte;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
21
app/src/main/java/project/schafkopfzaehler_2/api_server.java
Normal file
21
app/src/main/java/project/schafkopfzaehler_2/api_server.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package project.schafkopfzaehler_2;
|
||||||
|
|
||||||
|
import okhttp3.MultipartBody;
|
||||||
|
import project.schafkopfzaehler_2.POJO.POJOClass;
|
||||||
|
|
||||||
|
import retrofit2.Call;
|
||||||
|
import retrofit2.Callback;
|
||||||
|
import retrofit2.http.Multipart;
|
||||||
|
import retrofit2.http.POST;
|
||||||
|
import retrofit2.http.Part;
|
||||||
|
|
||||||
|
public interface api_server{
|
||||||
|
|
||||||
|
@Multipart
|
||||||
|
@POST("/face_detection/detect/")
|
||||||
|
|
||||||
|
Call<POJOClass> uploadAttachment(@Part MultipartBody.Part filePart);
|
||||||
|
|
||||||
|
void getData(Callback<POJOClass> response);
|
||||||
|
|
||||||
|
}
|
@ -22,7 +22,7 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
|||||||
mHolder = getHolder();
|
mHolder = getHolder();
|
||||||
mHolder.addCallback(this);
|
mHolder.addCallback(this);
|
||||||
// deprecated setting, but required on Android versions prior to 3.0
|
// deprecated setting, but required on Android versions prior to 3.0
|
||||||
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
// mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
@ -48,6 +48,11 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (mHolder == null){
|
||||||
|
// holder does not exist
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// stop preview before making changes
|
// stop preview before making changes
|
||||||
try {
|
try {
|
||||||
mCamera.stopPreview();
|
mCamera.stopPreview();
|
||||||
@ -56,6 +61,9 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
|||||||
}
|
}
|
||||||
|
|
||||||
mCamera.setDisplayOrientation(90);
|
mCamera.setDisplayOrientation(90);
|
||||||
|
//Camera.Parameters setting_param = mCamera.getParameters();
|
||||||
|
//setting_param.setPreviewSize(320,480);
|
||||||
|
//mCamera.setParameters(setting_param);
|
||||||
|
|
||||||
// start preview with new settings
|
// start preview with new settings
|
||||||
try {
|
try {
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 93 KiB After Width: | Height: | Size: 107 KiB |
Binary file not shown.
Before Width: | Height: | Size: 5.2 MiB After Width: | Height: | Size: 346 KiB |
@ -6,15 +6,20 @@
|
|||||||
|
|
||||||
<FrameLayout
|
<FrameLayout
|
||||||
android:id="@+id/cameraPreview"
|
android:id="@+id/cameraPreview"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="fill_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="fill_parent"
|
||||||
|
android:layout_alignParentTop="true">
|
||||||
</FrameLayout>
|
</FrameLayout>
|
||||||
|
|
||||||
<RelativeLayout
|
<TextView
|
||||||
|
android:id="@+id/capture_log"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="200dp"
|
||||||
|
android:layout_above="@id/capture"
|
||||||
android:background="@drawable/woodentable"
|
android:background="@drawable/woodentable"
|
||||||
android:layout_alignParentBottom="true">
|
android:text="@string/app_name"
|
||||||
|
android:textColor="@android:color/black"
|
||||||
|
android:textSize="18sp" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/capture"
|
android:id="@+id/capture"
|
||||||
@ -29,14 +34,4 @@
|
|||||||
android:textColor="@android:color/black"
|
android:textColor="@android:color/black"
|
||||||
android:textSize="24sp" />
|
android:textSize="24sp" />
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/capture_log"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="@string/app_name"
|
|
||||||
android:layout_alignParentBottom="false"
|
|
||||||
android:layout_below="@+id/capture" />
|
|
||||||
|
|
||||||
</RelativeLayout>
|
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
@ -7,7 +7,7 @@ buildscript {
|
|||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:3.1.4'
|
classpath 'com.android.tools.build:gradle:3.2.1'
|
||||||
|
|
||||||
|
|
||||||
// NOTE: Do not place your application dependencies here; they belong
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
|
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,6 +1,6 @@
|
|||||||
#Thu Sep 06 17:43:00 CEST 2018
|
#Sun Dec 09 12:42:16 CET 2018
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
|
||||||
|
@ -1,2 +1 @@
|
|||||||
include ':app'
|
include ':app'
|
||||||
include ':openCVLibrary343'
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user