- 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 usermaster
<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" /> |
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" | ||||
} | } | ||||
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' | ||||
} | } |
<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" |
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; | ||||
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; | ||||
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; | ||||
} | } | ||||
// 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); | |||||
try { | try { | ||||
FileOutputStream fos = new FileOutputStream(pictureFile); | FileOutputStream fos = new FileOutputStream(pictureFile); | ||||
fos.write(data); | |||||
fos.write(bytes.toByteArray()); | |||||
fos.close(); | fos.close(); | ||||
Log.d("CAMERA", "Saved..."); | |||||
captureLog.setText(captureLog.getText() + "\n" + "Saved..."); | |||||
} catch (FileNotFoundException e) { | } catch (FileNotFoundException e) { | ||||
Log.d("CAMERA", "File not found: " + e.getMessage()); | Log.d("CAMERA", "File not found: " + e.getMessage()); | ||||
captureLog.setText(captureLog.getText() + "\n" + "File not found: " + e.getMessage()); | |||||
} catch (IOException e) { | } catch (IOException e) { | ||||
Log.d("CAMERA", "Error accessing file: " + e.getMessage()); | Log.d("CAMERA", "Error accessing file: " + e.getMessage()); | ||||
captureLog.setText(captureLog.getText() + "\n" + "Error accessing file: " + e.getMessage()); | |||||
} | } | ||||
captureLog = findViewById(R.id.capture_log); | |||||
captureLog.setText("Karte scannen --> an Server senden"); | |||||
// ------------------------ 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(); | |||||
} | } | ||||
}; | }; | ||||
// 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; | ||||
} | } | ||||
} | } | ||||
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; | |||||
} | |||||
} |
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); | |||||
} |
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) { | ||||
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(); | ||||
} | } | ||||
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 { |
<FrameLayout | <FrameLayout | ||||
android:id="@+id/cameraPreview" | 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> | </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"> | |||||
<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" /> | |||||
android:text="@string/app_name" | |||||
android:textColor="@android:color/black" | |||||
android:textSize="18sp" /> | |||||
<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> | |||||
<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" /> | |||||
</RelativeLayout> | </RelativeLayout> |
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 |
#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 |
include ':app' | include ':app' | ||||
include ':openCVLibrary343' |