Browse Source

- Cleaned up code

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

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

</value> </value>
</option> </option>
</component> </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" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

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

package project.schafkopfzaehler_2; package project.schafkopfzaehler_2;


import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera; import android.hardware.Camera;
import android.net.Uri; import android.net.Uri;
import android.os.Environment; import android.os.Environment;
import android.widget.Button; import android.widget.Button;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;


import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;


public class CardCaptureActivity extends AppCompatActivity { 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/"; 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; public static final int MEDIA_TYPE_IMAGE = 1;


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

// Variables from / for intent
int points; int points;
String playerNumber; 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 @Override
public void onClick (View v) { public void onClick (View v) {
Log.d("SHIGGY", "Some button pressed");


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


// Confirm button was pressed --> Send data to previous activity
if ( v == confirm ) { 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(); Intent data = new Intent();
data.putExtra("points", points); data.putExtra("points", points);
data.putExtra("playerNo", playerNumber); data.putExtra("playerNo", playerNumber);
// Create an instance of Camera // Create an instance of Camera
mCamera = getCameraInstance(); 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); mPreview = new cameraPreview(this, mCamera);
FrameLayout preview = findViewById(R.id.cameraPreview); FrameLayout preview = findViewById(R.id.cameraPreview);
preview.addView(mPreview); preview.addView(mPreview);
// Start click listener // Start click listener
confirm = findViewById(R.id.capture_done); confirm = findViewById(R.id.capture_done);
capture = findViewById(R.id.capture); 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(){ public static Camera getCameraInstance(){
Camera c = null; Camera c = null;
try { try {
c = Camera.open(); // attempt to get a Camera instance
c = Camera.open(); // Attempt to get a Camera instance
} }
catch (Exception e){ catch (Exception e){
// Camera is not available (in use or does not exist) // 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
} }




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
FileOutputStream fos = new FileOutputStream(pictureFile); FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data); fos.write(data);
fos.close(); 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..."); Log.d("CAMERA", "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());
} catch (IOException e) { } catch (IOException e) {
Log.d("CAMERA", "Error accessing file: " + e.getMessage()); 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(); mCamera.startPreview();
} }
} }
} }


/** 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){ private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(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){ 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( File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "Schafkopfzaehler"); 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 // Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){ if (! mediaStorageDir.exists()){
return mediaFile; return mediaFile;
} }


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


if (cardName.contains("Ass")) { if (cardName.contains("Ass")) {


} }


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

private Button p1, p2, p3, p4, chooseGame; // Button init private Button p1, p2, p3, p4, chooseGame; // Button init
private TextView choice; private TextView choice;
private String playerNames[] = {"", "", "", ""}; // Player names init private String playerNames[] = {"", "", "", ""}; // Player names init
static final int REQUEST_IMAGE_CAPTURE = 1;


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


if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
int points = data.getIntExtra("points", 0); int points = data.getIntExtra("points", 0);
String player = data.getStringExtra("playerNo"); String player = data.getStringExtra("playerNo");
Log.d("MainGame", "ComeBack...: " + player + " " + points);
switch (player) { switch (player) {


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



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

// underlying surface is created and destroyed. // underlying surface is created and destroyed.
mHolder = getHolder(); mHolder = getHolder();
mHolder.addCallback(this); 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) { 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 { try {
mCamera.setPreviewDisplay(holder); mCamera.setPreviewDisplay(holder);
mCamera.startPreview(); mCamera.startPreview();
return; return;
} }


// stop preview before making changes
// Stop preview before making changes
try { try {
mCamera.stopPreview(); mCamera.stopPreview();
} catch (Exception e){ } catch (Exception e){
} }


mCamera.setDisplayOrientation(90); mCamera.setDisplayOrientation(90);

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

Camera.Size mSize;
int height; int height;
int width; int width;
for (Camera.Size size : sizes) { for (Camera.Size size : sizes) {
width = size.width; width = size.width;
height = size.height; height = size.height;
//Log.d("SHIGGY", "Available resolution" + size.width + size.height);
if (width >= 600 && width <= 800) { if (width >= 600 && width <= 800) {
setting_param.setPictureSize(width, height); setting_param.setPictureSize(width, height);
break; break;
} }


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


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

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

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/capture"
android:layout_above="@+id/cameraPreview"
android:layout_alignParentEnd="false" android:layout_alignParentEnd="false"
android:layout_alignParentBottom="false"
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"
android:fontFamily="@font/germania_one" android:fontFamily="@font/germania_one"


<Button <Button
android:id="@+id/capture" android:id="@+id/capture"
style="?android:borderlessButtonStyle"
style="@style/Widget.AppCompat.Button"
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_above="@+id/capture_done"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:background="@android:color/holo_green_dark"
android:background="@color/colorPrimary"
android:fontFamily="@font/germania_one" android:fontFamily="@font/germania_one"
android:text="@string/capture" android:text="@string/capture"
android:textColor="@android:color/black" android:textColor="@android:color/black"

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

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

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

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

Loading…
Cancel
Save