Browse Source

- 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
master
Felix Kramer 5 years ago
parent
commit
8340eccd0d

+ 70
- 20
app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java View File

@@ -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<POJOClass> call = client.uploadAttachment(filePart);

try {
call.enqueue(new Callback<POJOClass>() {
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 :)");
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() );
}
});
}

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;

}

}



+ 47
- 2
app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java View File

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

}
}
}

}

+ 20
- 3
app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java View File

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

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

// start preview with new settings
try {

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

@@ -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" />

<Button
android:id="@+id/capture_done"
style="?android:borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/capture"
android:layout_alignParentEnd="false"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_green_dark"
android:fontFamily="@font/germania_one"
android:text="@string/confirm"
android:textColor="@android:color/black"
android:textSize="24sp" />

<Button
android:id="@+id/capture"
style="?android:borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/cameraPreview"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_green_dark"

+ 5
- 5
app/src/main/res/layout/main_game_layout.xml View File

@@ -82,7 +82,7 @@
android:text="@string/points"
android:textAlignment="viewStart"
android:textColor="@android:color/black"
android:textSize="14sp" />
android:textSize="24sp" />

</TableRow>

@@ -129,7 +129,7 @@
android:text="@string/points"
android:textAlignment="viewStart"
android:textColor="@android:color/black"
android:textSize="14sp" />
android:textSize="24sp" />

<Space
android:layout_width="wrap_content"
@@ -143,7 +143,7 @@
android:text="@string/points"
android:textAlignment="viewStart"
android:textColor="@android:color/black"
android:textSize="14sp" />
android:textSize="24sp" />

</TableRow>

@@ -170,12 +170,12 @@
<TextView
android:id="@+id/p3_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:fontFamily="@font/germania_one"
android:text="@string/points"
android:textAlignment="viewStart"
android:textColor="@android:color/black"
android:textSize="14sp" />
android:textSize="24sp" />
</TableRow>

</TableLayout>

Loading…
Cancel
Save