- 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:
Felix Kramer 2019-01-06 12:58:57 +01:00
parent 3bca29767a
commit d4b8fb8c52
14 changed files with 166 additions and 52 deletions

Binary file not shown.

1
.idea/gradle.xml generated
View File

@ -9,7 +9,6 @@
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
<option value="$PROJECT_DIR$/openCVLibrary343" />
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />

View File

@ -5,7 +5,7 @@ android {
defaultConfig {
applicationId "project.schafkopfzaehler_2"
minSdkVersion 19
targetSdkVersion 27
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@ -19,10 +19,11 @@ android {
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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.espresso:espresso-core:3.0.2'
}

View File

@ -19,6 +19,8 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

View File

@ -1,5 +1,7 @@
package project.schafkopfzaehler_2;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Environment;
@ -11,11 +13,22 @@ import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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 {
private Camera mCamera;
@ -89,30 +102,73 @@ public class CardCaptureActivity extends AppCompatActivity {
super.onStart();
}
/** Picture button was pressed. Send picture to server for card recognition. */
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null){
Log.d("CAMERA", "Error creating media file, check storage permissions");
Log.d("CAMERA", "Error creating media files");
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("CAMERA", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("CAMERA", "Error accessing file: " + e.getMessage());
}
// Compress image to lower site
Bitmap bmp = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 20, bytes);
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.
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
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
Log.d("Schafkopfzaehler", "failed to create directory");
return null;
}
}
// Create a media file name
Long tsLong = System.currentTimeMillis()/1000;
String timeStamp = tsLong.toString();
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
"Karte.jpg");
} else {
return null;
}
@ -162,3 +217,5 @@ public class CardCaptureActivity extends AppCompatActivity {
}

View File

@ -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;
}
}

View 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);
}

View File

@ -22,7 +22,7 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
mHolder = getHolder();
mHolder.addCallback(this);
// 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) {
@ -48,6 +48,11 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
return;
}
if (mHolder == null){
// holder does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
@ -56,6 +61,9 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
}
mCamera.setDisplayOrientation(90);
//Camera.Parameters setting_param = mCamera.getParameters();
//setting_param.setPreviewSize(320,480);
//mCamera.setParameters(setting_param);
// start preview with new settings
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

View File

@ -6,37 +6,32 @@
<FrameLayout
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true">
</FrameLayout>
<RelativeLayout
<TextView
android:id="@+id/capture_log"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_above="@id/capture"
android:background="@drawable/woodentable"
android:text="@string/app_name"
android:textColor="@android:color/black"
android:textSize="18sp" />
<Button
android:id="@+id/capture"
style="?android:borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/woodentable"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/capture"
style="?android:borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_green_dark"
android:fontFamily="@font/germania_one"
android:text="@string/capture"
android:textColor="@android:color/black"
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>
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_green_dark"
android:fontFamily="@font/germania_one"
android:text="@string/capture"
android:textColor="@android:color/black"
android:textSize="24sp" />
</RelativeLayout>

View File

@ -7,7 +7,7 @@ buildscript {
jcenter()
}
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

View File

@ -1,6 +1,6 @@
#Thu Sep 06 17:43:00 CEST 2018
#Sun Dec 09 12:42:16 CET 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
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

View File

@ -1,2 +1 @@
include ':app'
include ':openCVLibrary343'