Browse Source

- Cleaned up code

- Added comments for better documentation
master
Felix Kramer 2 years ago
parent
commit
84200b4fc8

+ 1
- 1
.idea/misc.xml View File

@@ -25,7 +25,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

+ 72
- 60
app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java View File

@@ -1,8 +1,6 @@
package project.schafkopfzaehler_2;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Environment;
@@ -14,8 +12,8 @@ import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
@@ -35,31 +33,37 @@ import retrofit2.converter.gson.GsonConverterFactory;

public class CardCaptureActivity extends AppCompatActivity {

private Camera mCamera;
private cameraPreview mPreview;
// Static base variables
private static final String serverURL = "http://192.168.1.6:8000/";
Button capture, confirm; // Button init
TextView captureLog; // TextView init
public static final int MEDIA_TYPE_IMAGE = 1;


// Camera variables
private Camera mCamera;
private cameraPreview mPreview;

// Variables from / for intent
int points;
String playerNumber;

Button capture, confirm; // Button init
TextView captureLog; // TextView init

// Add a listener to the Capture button
private View.OnClickListener startCaptureListener = new View.OnClickListener() {
// Add a listener to the capture / confirm button
private View.OnClickListener startClickListener = new View.OnClickListener() {

@Override
public void onClick (View v) {
Log.d("SHIGGY", "Some button pressed");

// Capture button was pressed --> Take picture
if (v == capture) {
mCamera.takePicture(null, null, mPicture);
}

// Confirm button was pressed --> Send data to previous activity
if ( v == confirm ) {
// Send choice to the previous activity
// and then finishes the current activity
Log.d("SHIGGY", "Confirm button pressed");
// Send points and player number to the previous
// activity and then finishes the current activity
Intent data = new Intent();
data.putExtra("points", points);
data.putExtra("playerNo", playerNumber);
@@ -86,7 +90,7 @@ public class CardCaptureActivity extends AppCompatActivity {
// Create an instance of Camera
mCamera = getCameraInstance();

// Create our Preview view and set it as the content of our activity.
// Create preview view and set it as the content of our activity.
mPreview = new cameraPreview(this, mCamera);
FrameLayout preview = findViewById(R.id.cameraPreview);
preview.addView(mPreview);
@@ -94,21 +98,21 @@ public class CardCaptureActivity extends AppCompatActivity {
// Start click listener
confirm = findViewById(R.id.capture_done);
capture = findViewById(R.id.capture);
confirm.setOnClickListener(startCaptureListener);
capture.setOnClickListener(startCaptureListener);
confirm.setOnClickListener(startClickListener);
capture.setOnClickListener(startClickListener);

}

/* Get an instance of the Camera object. */
// Get an instance of the Camera object
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
c = Camera.open(); // Attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
return c; // Returns null if camera is unavailable
}


@@ -134,7 +138,7 @@ public class CardCaptureActivity extends AppCompatActivity {
super.onStart();
}

/* Picture button was pressed. Send picture to server for card recognition. */
// Picture button was pressed. Send picture to server for card recognition
private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

@Override
@@ -153,47 +157,19 @@ public class CardCaptureActivity extends AppCompatActivity {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();

// Show successful save to the user
Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.pictureSaved), Toast.LENGTH_SHORT);
toast.show();

Log.d("CAMERA", "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(serverURL).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);

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!");
points += getCardPoints(card);
// Delete card for decreasing garbage
if ( pictureFile.delete() );
captureLog.setText(captureLog.getText() + "\n" + "Points: " + points);

}

@Override
public void onFailure(Call<POJOClass> call, Throwable t) {
captureLog.setText(captureLog.getText() + "\n" + "Error..." + t.getMessage());
// Delete card for decreasing garbage
if ( pictureFile.delete() );
}
});

// ----------------------------------------------------------------------
sendFileToServer(pictureFile);

mCamera.startPreview();
}
@@ -206,20 +182,16 @@ public class CardCaptureActivity extends AppCompatActivity {
}
}

/** Create a file Uri for saving an image or video */
// Create a file Uri for saving an image or video
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving the captured image */
// Create a File for saving the captured image
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
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()){
@@ -243,6 +215,7 @@ public class CardCaptureActivity extends AppCompatActivity {
return mediaFile;
}

// Returns the amount of points the player receives for the card
private int getCardPoints (String cardName) {

if (cardName.contains("Ass")) {
@@ -265,6 +238,45 @@ public class CardCaptureActivity extends AppCompatActivity {

}

private void sendFileToServer (final File picture) {

Retrofit.Builder builder = new Retrofit.Builder().baseUrl(serverURL).addConverterFactory(GsonConverterFactory.create());

Retrofit retrofit = builder.build();

api_server client = retrofit.create(api_server.class);

MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", picture.getName(), RequestBody.create(MediaType.parse("image/*"), picture));

Call<POJOClass> call = client.uploadAttachment(filePart);

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!");
points += getCardPoints(card);
// Delete picture to minimize memory usage
if ( picture.delete() );

// Show current points to user
Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.points) + points , Toast.LENGTH_SHORT);
toast.show();

}

@Override
public void onFailure(Call<POJOClass> call, Throwable t) {
captureLog.setText(captureLog.getText() + "\n" + "Error..." + t.getMessage());
// Delete picture to minimize memory usage
if ( picture.delete() );
}
});



}

}



+ 5
- 6
app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java View File

@@ -20,7 +20,6 @@ public class MainGameActivity extends AppCompatActivity {
private Button p1, p2, p3, p4, chooseGame; // Button init
private TextView choice;
private String playerNames[] = {"", "", "", ""}; // Player names init
static final int REQUEST_IMAGE_CAPTURE = 1;

private View.OnClickListener startClickListener = new View.OnClickListener() {

@@ -131,24 +130,24 @@ public class MainGameActivity extends AppCompatActivity {
if (resultCode == RESULT_OK) {
int points = data.getIntExtra("points", 0);
String player = data.getStringExtra("playerNo");
Log.d("MainGame", "ComeBack...: " + player + " " + points);
switch (player) {

case "p1":
TextView points_p1 = findViewById(R.id.p1_points);
points_p1.setText("Punkte: " + points);
points_p1.setText(getString(R.string.points) + points);
break;
case "p2":
TextView points_p2 = findViewById(R.id.p2_points);
points_p2.setText("Punkte: " + points);
points_p2.setText(getString(R.string.points) + points);
break;
case "p3":
TextView points_p3 = findViewById(R.id.p3_points);
points_p3.setText("Punkte: " + points);
points_p3.setText(getString(R.string.points) + points);
break;
case "p4":
TextView points_p4 = findViewById(R.id.p4_points);
points_p4.setText("Punkte: " + points);
points_p4.setText(getString(R.string.points) + points);
break;
}


+ 7
- 9
app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java View File

@@ -23,12 +23,11 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
// mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
// The Surface created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
@@ -55,7 +54,7 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
return;
}

// stop preview before making changes
// Stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
@@ -63,16 +62,16 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
}

mCamera.setDisplayOrientation(90);

// Get lowest possible resolution to minimize data traffic
Camera.Parameters setting_param = mCamera.getParameters();
List<Camera.Size> sizes = setting_param.getSupportedPictureSizes();

Camera.Size mSize;
int height;
int width;
for (Camera.Size size : sizes) {
width = size.width;
height = size.height;
//Log.d("SHIGGY", "Available resolution" + size.width + size.height);
if (width >= 600 && width <= 800) {
setting_param.setPictureSize(width, height);
break;
@@ -80,9 +79,8 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
}

mCamera.setParameters(setting_param);
// setting_param.setPreviewSize(320,480);

// start preview with new settings
// Start preview with new resolution
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();

+ 6
- 6
app/src/main/res/layout/cardcapture.xml View File

@@ -27,9 +27,9 @@
style="?android:borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/capture"
android:layout_above="@+id/cameraPreview"
android:layout_alignParentEnd="false"
android:layout_alignParentBottom="false"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_green_dark"
android:fontFamily="@font/germania_one"
@@ -39,13 +39,13 @@

<Button
android:id="@+id/capture"
style="?android:borderlessButtonStyle"
style="@style/Widget.AppCompat.Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/cameraPreview"
android:layout_alignParentBottom="true"
android:layout_above="@+id/capture_done"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_green_dark"
android:background="@color/colorPrimary"
android:fontFamily="@font/germania_one"
android:text="@string/capture"
android:textColor="@android:color/black"

+ 1
- 1
app/src/main/res/values/colors.xml View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimary">#3f51b5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#ff4081</color>
</resources>

+ 2
- 1
app/src/main/res/values/strings.xml View File

@@ -4,7 +4,7 @@
<string name="title">Schafkopf\nPunktezähler</string>
<string name="choosePlayers">Wer spielt mit?</string>
<string name="playStyle">Wer spielt was?</string>
<string name="points">Punkte\:</string>
<string name="points">Punkte: </string>
<string name="startMain">Los geht\'s</string>
<string name="next">Weiter</string>
<string name="Player1">Spieler 1</string>
@@ -27,6 +27,7 @@
<string name="noChoice">Es wurde noch nichts angesagt...</string>
<string name="soloPlayer">Wer hat das Solo gespielt?</string>
<string name="choice">-- Ihre Auswahl --</string>
<string name="pictureSaved">Bild gespeichert</string>
<string name="confirm">Bestätigen</string>
<string name="sauspiel1">Alte</string>
<string name="sauspiel2">Blaue</string>

Loading…
Cancel
Save