diff --git a/.idea/misc.xml b/.idea/misc.xml index 99202cc..c0f68ed 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -25,7 +25,7 @@ - + diff --git a/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java b/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java index d71413a..0347b5e 100644 --- a/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java +++ b/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java @@ -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 { + // Static base variables + private static final String serverURL = "http://192.168.1.6:8000/"; + public static final int MEDIA_TYPE_IMAGE = 1; + + + // Camera variables private Camera mCamera; private cameraPreview mPreview; - 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; + + // 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 call = client.uploadAttachment(filePart); - - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response 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 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 call = client.uploadAttachment(filePart); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response 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 call, Throwable t) { + captureLog.setText(captureLog.getText() + "\n" + "Error..." + t.getMessage()); + // Delete picture to minimize memory usage + if ( picture.delete() ); + } + }); + + + + } + } diff --git a/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java b/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java index 18913c1..389a9f7 100644 --- a/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java +++ b/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java @@ -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; } diff --git a/app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java b/app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java index 9a67229..81b877f 100644 --- a/app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java +++ b/app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java @@ -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 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(); diff --git a/app/src/main/res/layout/cardcapture.xml b/app/src/main/res/layout/cardcapture.xml index c78e5b6..dbb44a8 100644 --- a/app/src/main/res/layout/cardcapture.xml +++ b/app/src/main/res/layout/cardcapture.xml @@ -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 @@