From 8340eccd0d15a8b992b7af5c5836cf29f198d924 Mon Sep 17 00:00:00 2001 From: Felix Kramer Date: Sun, 13 Jan 2019 14:17:29 +0100 Subject: [PATCH] - Picture will be taken in a low resolution - Added confirm button to end card capturing - Added scrollbar to capture log if many cards were captured - Delete picture after sending them to the server to avoid excessive memory usage - Get points for each player after card capture and sending it to the main game activity --- .../CardCaptureActivity.java | 90 ++++++++++++++----- .../schafkopfzaehler_2/MainGameActivity.java | 49 +++++++++- .../schafkopfzaehler_2/cameraPreview.java | 23 ++++- app/src/main/res/layout/cardcapture.xml | 17 ++++ app/src/main/res/layout/main_game_layout.xml | 10 +-- 5 files changed, 159 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java b/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java index 1978716..d71413a 100644 --- a/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java +++ b/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java @@ -1,5 +1,6 @@ package project.schafkopfzaehler_2; +import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.hardware.Camera; @@ -7,6 +8,7 @@ import android.net.Uri; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.text.method.ScrollingMovementMethod; import android.util.Log; import android.view.View; import android.widget.Button; @@ -18,6 +20,8 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; import okhttp3.MediaType; import okhttp3.MultipartBody; @@ -33,18 +37,36 @@ public class CardCaptureActivity extends AppCompatActivity { private Camera mCamera; private cameraPreview mPreview; - Button capture; // Button init + 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; + int points; + String playerNumber; + // Add a listener to the Capture button private View.OnClickListener startCaptureListener = new View.OnClickListener() { @Override public void onClick (View v) { + Log.d("SHIGGY", "Some button pressed"); + if (v == capture) { mCamera.takePicture(null, null, mPicture); } + + if ( v == confirm ) { + // Send choice to the previous activity + // and then finishes the current activity + Log.d("SHIGGY", "Confirm button pressed"); + Intent data = new Intent(); + data.putExtra("points", points); + data.putExtra("playerNo", playerNumber); + setResult(RESULT_OK, data); + finish(); + } + } }; @@ -54,6 +76,13 @@ public class CardCaptureActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.cardcapture); + TextView captureLog = findViewById(R.id.capture_log); + captureLog.setMovementMethod(new ScrollingMovementMethod()); + + // Get player names from the activity that started this one + Intent intent = getIntent(); + playerNumber = intent.getStringExtra("playerName"); + // Create an instance of Camera mCamera = getCameraInstance(); @@ -62,7 +91,10 @@ public class CardCaptureActivity extends AppCompatActivity { FrameLayout preview = findViewById(R.id.cameraPreview); preview.addView(mPreview); + // Start click listener + confirm = findViewById(R.id.capture_done); capture = findViewById(R.id.capture); + confirm.setOnClickListener(startCaptureListener); capture.setOnClickListener(startCaptureListener); } @@ -102,32 +134,26 @@ 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 public void onPictureTaken(byte[] data, Camera camera) { - File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); + final File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); if (pictureFile == null){ Log.d("CAMERA", "Error creating media files"); 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 { FileOutputStream fos = new FileOutputStream(pictureFile); - fos.write(bytes.toByteArray()); + fos.write(data); 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()); @@ -137,7 +163,7 @@ public class CardCaptureActivity extends AppCompatActivity { } // ------------------------ Send file to server ------------------------- - Retrofit.Builder builder = new Retrofit.Builder().baseUrl("http://192.168.0.195:8000/").addConverterFactory(GsonConverterFactory.create()); + Retrofit.Builder builder = new Retrofit.Builder().baseUrl(serverURL).addConverterFactory(GsonConverterFactory.create()); Retrofit retrofit = builder.build(); @@ -147,24 +173,25 @@ public class CardCaptureActivity extends AppCompatActivity { Call call = client.uploadAttachment(filePart); - try { - call.enqueue(new Callback() { + call.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { String card = response.body().getKarte(); - captureLog.setText(captureLog.getText() + "\n" + card + " erkannt :)"); + 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() ); } }); - } - - catch (Exception e){ - Log.d("Schafkopfzaehler", "EXCEPTION: " + e.getMessage()); - } // ---------------------------------------------------------------------- @@ -203,10 +230,11 @@ public class CardCaptureActivity extends AppCompatActivity { } // Create a media file name + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); File mediaFile; if (type == MEDIA_TYPE_IMAGE){ mediaFile = new File(mediaStorageDir.getPath() + File.separator + - "Karte.jpg"); + "Karte" + timeStamp + ".jpg"); } else { return null; @@ -215,6 +243,28 @@ public class CardCaptureActivity extends AppCompatActivity { return mediaFile; } + private int getCardPoints (String cardName) { + + if (cardName.contains("Ass")) { + return 11; + } + if (cardName.contains("Koenig")) { + return 4; + } + if (cardName.contains("Ober")) { + return 3; + } + if (cardName.contains("Unter")) { + return 2; + } + if (cardName.contains("10")) { + return 10; + } + + return 0; + + } + } diff --git a/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java b/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java index 7c9dc9d..18913c1 100644 --- a/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java +++ b/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java @@ -89,10 +89,28 @@ public class MainGameActivity extends AppCompatActivity { private void playerButtonClicked (View v) { + String playerNumber = ""; Log.d("MainGame", v + " clicked..."); // Start card capture activity with the camera preview Intent cardCaptureIntent = new Intent(this, CardCaptureActivity.class); - startActivity(cardCaptureIntent); + + // Get which player button was pressed + if ( findViewById(R.id.p1_game) == v ) { + playerNumber = "p1"; + } + if ( findViewById(R.id.p2_game) == v ) { + playerNumber = "p2"; + } + if ( findViewById(R.id.p3_game) == v ) { + playerNumber = "p3"; + } + if ( findViewById(R.id.p4_game) == v ) { + playerNumber = "p4"; + } + + cardCaptureIntent.putExtra("playerName", playerNumber ); + startActivityForResult(cardCaptureIntent, 1); + // startActivity(cardCaptureIntent); } private void gameChoosing () { @@ -103,12 +121,39 @@ public class MainGameActivity extends AppCompatActivity { } public void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == 0) { + if (requestCode == 0 && data != null) { if (resultCode == RESULT_OK) { String announcement = data.getStringExtra("choice"); choice.setText(announcement); } } + if (requestCode == 1 && data != null) { + 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); + break; + case "p2": + TextView points_p2 = findViewById(R.id.p2_points); + points_p2.setText("Punkte: " + points); + break; + case "p3": + TextView points_p3 = findViewById(R.id.p3_points); + points_p3.setText("Punkte: " + points); + break; + case "p4": + TextView points_p4 = findViewById(R.id.p4_points); + points_p4.setText("Punkte: " + 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 07b3105..9a67229 100644 --- a/app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java +++ b/app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java @@ -3,10 +3,12 @@ package project.schafkopfzaehler_2; import android.content.Context; import android.hardware.Camera; import android.util.Log; +import android.util.Size; import android.view.SurfaceHolder; import android.view.SurfaceView; import java.io.IOException; +import java.util.List; /** A basic Camera preview class */ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback { @@ -61,9 +63,24 @@ 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); + 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; + } + } + + mCamera.setParameters(setting_param); + // setting_param.setPreviewSize(320,480); // start preview with new settings try { diff --git a/app/src/main/res/layout/cardcapture.xml b/app/src/main/res/layout/cardcapture.xml index a6e8b32..c78e5b6 100644 --- a/app/src/main/res/layout/cardcapture.xml +++ b/app/src/main/res/layout/cardcapture.xml @@ -17,15 +17,32 @@ android:layout_height="200dp" android:layout_above="@id/capture" android:background="@drawable/woodentable" + android:scrollbars="vertical" android:text="@string/app_name" android:textColor="@android:color/black" android:textSize="18sp" /> +