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