mirror of
https://github.com/LightningMcK/schafkopfzaehler.git
synced 2025-02-05 21:46:09 +00:00
- 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
This commit is contained in:
parent
d4b8fb8c52
commit
8340eccd0d
@ -1,5 +1,6 @@
|
|||||||
package project.schafkopfzaehler_2;
|
package project.schafkopfzaehler_2;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
@ -7,6 +8,7 @@ import android.net.Uri;
|
|||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.text.method.ScrollingMovementMethod;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
@ -18,6 +20,8 @@ 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 java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
import okhttp3.MediaType;
|
import okhttp3.MediaType;
|
||||||
import okhttp3.MultipartBody;
|
import okhttp3.MultipartBody;
|
||||||
@ -33,18 +37,36 @@ public class CardCaptureActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
private Camera mCamera;
|
private Camera mCamera;
|
||||||
private cameraPreview mPreview;
|
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
|
TextView captureLog; // TextView init
|
||||||
public static final int MEDIA_TYPE_IMAGE = 1;
|
public static final int MEDIA_TYPE_IMAGE = 1;
|
||||||
|
int points;
|
||||||
|
String playerNumber;
|
||||||
|
|
||||||
|
|
||||||
// Add a listener to the Capture button
|
// Add a listener to the Capture button
|
||||||
private View.OnClickListener startCaptureListener = new View.OnClickListener() {
|
private View.OnClickListener startCaptureListener = new View.OnClickListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick (View v) {
|
public void onClick (View v) {
|
||||||
|
Log.d("SHIGGY", "Some button pressed");
|
||||||
|
|
||||||
if (v == capture) {
|
if (v == capture) {
|
||||||
mCamera.takePicture(null, null, mPicture);
|
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);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.cardcapture);
|
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
|
// Create an instance of Camera
|
||||||
mCamera = getCameraInstance();
|
mCamera = getCameraInstance();
|
||||||
|
|
||||||
@ -62,7 +91,10 @@ public class CardCaptureActivity extends AppCompatActivity {
|
|||||||
FrameLayout preview = findViewById(R.id.cameraPreview);
|
FrameLayout preview = findViewById(R.id.cameraPreview);
|
||||||
preview.addView(mPreview);
|
preview.addView(mPreview);
|
||||||
|
|
||||||
|
// Start click listener
|
||||||
|
confirm = findViewById(R.id.capture_done);
|
||||||
capture = findViewById(R.id.capture);
|
capture = findViewById(R.id.capture);
|
||||||
|
confirm.setOnClickListener(startCaptureListener);
|
||||||
capture.setOnClickListener(startCaptureListener);
|
capture.setOnClickListener(startCaptureListener);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -102,32 +134,26 @@ public class CardCaptureActivity extends AppCompatActivity {
|
|||||||
super.onStart();
|
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() {
|
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);
|
final File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
|
||||||
|
|
||||||
if (pictureFile == null){
|
if (pictureFile == null){
|
||||||
Log.d("CAMERA", "Error creating media files");
|
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);
|
captureLog = findViewById(R.id.capture_log);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FileOutputStream fos = new FileOutputStream(pictureFile);
|
FileOutputStream fos = new FileOutputStream(pictureFile);
|
||||||
fos.write(bytes.toByteArray());
|
fos.write(data);
|
||||||
fos.close();
|
fos.close();
|
||||||
Log.d("CAMERA", "Saved...");
|
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());
|
captureLog.setText(captureLog.getText() + "\n" + "File not found: " + e.getMessage());
|
||||||
@ -137,7 +163,7 @@ public class CardCaptureActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------ Send file to server -------------------------
|
// ------------------------ 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();
|
Retrofit retrofit = builder.build();
|
||||||
|
|
||||||
@ -147,24 +173,25 @@ public class CardCaptureActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
Call<POJOClass> call = client.uploadAttachment(filePart);
|
Call<POJOClass> call = client.uploadAttachment(filePart);
|
||||||
|
|
||||||
try {
|
|
||||||
call.enqueue(new Callback<POJOClass>() {
|
call.enqueue(new Callback<POJOClass>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<POJOClass> call, Response<POJOClass> response) {
|
public void onResponse(Call<POJOClass> call, Response<POJOClass> response) {
|
||||||
String card = response.body().getKarte();
|
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
|
@Override
|
||||||
public void onFailure(Call<POJOClass> call, Throwable t) {
|
public void onFailure(Call<POJOClass> call, Throwable t) {
|
||||||
captureLog.setText(captureLog.getText() + "\n" + "Error..." + t.getMessage());
|
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
|
// Create a media file name
|
||||||
|
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
||||||
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 +
|
||||||
"Karte.jpg");
|
"Karte" + timeStamp + ".jpg");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
@ -215,6 +243,28 @@ public class CardCaptureActivity extends AppCompatActivity {
|
|||||||
return mediaFile;
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -89,10 +89,28 @@ public class MainGameActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
private void playerButtonClicked (View v) {
|
private void playerButtonClicked (View v) {
|
||||||
|
|
||||||
|
String playerNumber = "";
|
||||||
Log.d("MainGame", v + " clicked...");
|
Log.d("MainGame", v + " clicked...");
|
||||||
// Start card capture activity with the camera preview
|
// Start card capture activity with the camera preview
|
||||||
Intent cardCaptureIntent = new Intent(this, CardCaptureActivity.class);
|
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 () {
|
private void gameChoosing () {
|
||||||
@ -103,12 +121,39 @@ public class MainGameActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
if (requestCode == 0) {
|
if (requestCode == 0 && data != null) {
|
||||||
if (resultCode == RESULT_OK) {
|
if (resultCode == RESULT_OK) {
|
||||||
String announcement = data.getStringExtra("choice");
|
String announcement = data.getStringExtra("choice");
|
||||||
choice.setText(announcement);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,12 @@ package project.schafkopfzaehler_2;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.util.Size;
|
||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** A basic Camera preview class */
|
/** A basic Camera preview class */
|
||||||
public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback {
|
public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback {
|
||||||
@ -61,9 +63,24 @@ public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
|||||||
}
|
}
|
||||||
|
|
||||||
mCamera.setDisplayOrientation(90);
|
mCamera.setDisplayOrientation(90);
|
||||||
//Camera.Parameters setting_param = mCamera.getParameters();
|
Camera.Parameters setting_param = mCamera.getParameters();
|
||||||
//setting_param.setPreviewSize(320,480);
|
List<Camera.Size> sizes = setting_param.getSupportedPictureSizes();
|
||||||
//mCamera.setParameters(setting_param);
|
|
||||||
|
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
|
// start preview with new settings
|
||||||
try {
|
try {
|
||||||
|
@ -17,15 +17,32 @@
|
|||||||
android:layout_height="200dp"
|
android:layout_height="200dp"
|
||||||
android:layout_above="@id/capture"
|
android:layout_above="@id/capture"
|
||||||
android:background="@drawable/woodentable"
|
android:background="@drawable/woodentable"
|
||||||
|
android:scrollbars="vertical"
|
||||||
android:text="@string/app_name"
|
android:text="@string/app_name"
|
||||||
android:textColor="@android:color/black"
|
android:textColor="@android:color/black"
|
||||||
android:textSize="18sp" />
|
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
|
<Button
|
||||||
android:id="@+id/capture"
|
android:id="@+id/capture"
|
||||||
style="?android:borderlessButtonStyle"
|
style="?android:borderlessButtonStyle"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_above="@+id/cameraPreview"
|
||||||
android:layout_alignParentBottom="true"
|
android:layout_alignParentBottom="true"
|
||||||
android:layout_centerHorizontal="true"
|
android:layout_centerHorizontal="true"
|
||||||
android:background="@android:color/holo_green_dark"
|
android:background="@android:color/holo_green_dark"
|
||||||
|
@ -82,7 +82,7 @@
|
|||||||
android:text="@string/points"
|
android:text="@string/points"
|
||||||
android:textAlignment="viewStart"
|
android:textAlignment="viewStart"
|
||||||
android:textColor="@android:color/black"
|
android:textColor="@android:color/black"
|
||||||
android:textSize="14sp" />
|
android:textSize="24sp" />
|
||||||
|
|
||||||
</TableRow>
|
</TableRow>
|
||||||
|
|
||||||
@ -129,7 +129,7 @@
|
|||||||
android:text="@string/points"
|
android:text="@string/points"
|
||||||
android:textAlignment="viewStart"
|
android:textAlignment="viewStart"
|
||||||
android:textColor="@android:color/black"
|
android:textColor="@android:color/black"
|
||||||
android:textSize="14sp" />
|
android:textSize="24sp" />
|
||||||
|
|
||||||
<Space
|
<Space
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
@ -143,7 +143,7 @@
|
|||||||
android:text="@string/points"
|
android:text="@string/points"
|
||||||
android:textAlignment="viewStart"
|
android:textAlignment="viewStart"
|
||||||
android:textColor="@android:color/black"
|
android:textColor="@android:color/black"
|
||||||
android:textSize="14sp" />
|
android:textSize="24sp" />
|
||||||
|
|
||||||
</TableRow>
|
</TableRow>
|
||||||
|
|
||||||
@ -170,12 +170,12 @@
|
|||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/p3_points"
|
android:id="@+id/p3_points"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="match_parent"
|
||||||
android:fontFamily="@font/germania_one"
|
android:fontFamily="@font/germania_one"
|
||||||
android:text="@string/points"
|
android:text="@string/points"
|
||||||
android:textAlignment="viewStart"
|
android:textAlignment="viewStart"
|
||||||
android:textColor="@android:color/black"
|
android:textColor="@android:color/black"
|
||||||
android:textSize="14sp" />
|
android:textSize="24sp" />
|
||||||
</TableRow>
|
</TableRow>
|
||||||
|
|
||||||
</TableLayout>
|
</TableLayout>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user