Projekt Schafkopfpunktezähler: Erster Prototyp einer App, die dem Benutzer die Notwendigkeit des Punktezählens beim Schafkopfspiel abnimmt.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CardCaptureActivity.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package project.schafkopfzaehler_2;
  2. import android.content.Intent;
  3. import android.hardware.Camera;
  4. import android.net.Uri;
  5. import android.os.Environment;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.text.method.ScrollingMovementMethod;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.FrameLayout;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. import java.io.File;
  16. import java.io.FileNotFoundException;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. import okhttp3.MediaType;
  22. import okhttp3.MultipartBody;
  23. import okhttp3.RequestBody;
  24. import project.schafkopfzaehler_2.POJO.POJOClass;
  25. import retrofit2.Call;
  26. import retrofit2.Callback;
  27. import retrofit2.Response;
  28. import retrofit2.Retrofit;
  29. import retrofit2.converter.gson.GsonConverterFactory;
  30. public class CardCaptureActivity extends AppCompatActivity {
  31. // Static base variables
  32. private static final String serverURL = "http://192.168.1.6:8000/";
  33. public static final int MEDIA_TYPE_IMAGE = 1;
  34. // Camera variables
  35. private Camera mCamera;
  36. private cameraPreview mPreview;
  37. // Variables from / for intent
  38. int points;
  39. String playerNumber;
  40. Button capture, confirm; // Button init
  41. TextView captureLog; // TextView init
  42. // Add a listener to the capture / confirm button
  43. private View.OnClickListener startClickListener = new View.OnClickListener() {
  44. @Override
  45. public void onClick (View v) {
  46. // Capture button was pressed --> Take picture
  47. if (v == capture) {
  48. mCamera.takePicture(null, null, mPicture);
  49. }
  50. // Confirm button was pressed --> Send data to previous activity
  51. if ( v == confirm ) {
  52. // Send points and player number to the previous
  53. // activity and then finishes the current activity
  54. Intent data = new Intent();
  55. data.putExtra("points", points);
  56. data.putExtra("playerNo", playerNumber);
  57. setResult(RESULT_OK, data);
  58. finish();
  59. }
  60. }
  61. };
  62. @Override
  63. protected void onCreate(Bundle savedInstanceState) {
  64. super.onCreate(savedInstanceState);
  65. setContentView(R.layout.cardcapture);
  66. TextView captureLog = findViewById(R.id.capture_log);
  67. captureLog.setMovementMethod(new ScrollingMovementMethod());
  68. // Get player names from the activity that started this one
  69. Intent intent = getIntent();
  70. playerNumber = intent.getStringExtra("playerName");
  71. // Create an instance of Camera
  72. mCamera = getCameraInstance();
  73. // Create preview view and set it as the content of our activity.
  74. mPreview = new cameraPreview(this, mCamera);
  75. FrameLayout preview = findViewById(R.id.cameraPreview);
  76. preview.addView(mPreview);
  77. // Start click listener
  78. confirm = findViewById(R.id.capture_done);
  79. capture = findViewById(R.id.capture);
  80. confirm.setOnClickListener(startClickListener);
  81. capture.setOnClickListener(startClickListener);
  82. }
  83. // Get an instance of the Camera object
  84. public static Camera getCameraInstance(){
  85. Camera c = null;
  86. try {
  87. c = Camera.open(); // Attempt to get a Camera instance
  88. }
  89. catch (Exception e){
  90. // Camera is not available (in use or does not exist)
  91. }
  92. return c; // Returns null if camera is unavailable
  93. }
  94. @Override
  95. protected void onPause() {
  96. super.onPause();
  97. releaseCamera();
  98. }
  99. @Override
  100. protected void onResume() {
  101. super.onResume();
  102. }
  103. @Override
  104. protected void onRestart() {
  105. super.onRestart();
  106. }
  107. @Override
  108. protected void onStart() {
  109. super.onStart();
  110. }
  111. // Picture button was pressed. Send picture to server for card recognition
  112. private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
  113. @Override
  114. public void onPictureTaken(byte[] data, Camera camera) {
  115. final File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
  116. if (pictureFile == null){
  117. Log.d("CAMERA", "Error creating media files");
  118. return;
  119. }
  120. captureLog = findViewById(R.id.capture_log);
  121. try {
  122. FileOutputStream fos = new FileOutputStream(pictureFile);
  123. fos.write(data);
  124. fos.close();
  125. // Show successful save to the user
  126. Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.pictureSaved), Toast.LENGTH_SHORT);
  127. toast.show();
  128. Log.d("CAMERA", "Saved...");
  129. } catch (FileNotFoundException e) {
  130. Log.d("CAMERA", "File not found: " + e.getMessage());
  131. } catch (IOException e) {
  132. Log.d("CAMERA", "Error accessing file: " + e.getMessage());
  133. }
  134. sendFileToServer(pictureFile);
  135. mCamera.startPreview();
  136. }
  137. };
  138. private void releaseCamera(){
  139. if (mCamera != null){
  140. mCamera.release(); // release the camera for other applications
  141. mCamera = null;
  142. }
  143. }
  144. // Create a file Uri for saving an image or video
  145. private static Uri getOutputMediaFileUri(int type){
  146. return Uri.fromFile(getOutputMediaFile(type));
  147. }
  148. // Create a File for saving the captured image
  149. private static File getOutputMediaFile(int type){
  150. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
  151. Environment.DIRECTORY_PICTURES), "Schafkopfzaehler");
  152. // Create the storage directory if it does not exist
  153. if (! mediaStorageDir.exists()){
  154. if (! mediaStorageDir.mkdirs()){
  155. Log.d("Schafkopfzaehler", "failed to create directory");
  156. return null;
  157. }
  158. }
  159. // Create a media file name
  160. String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  161. File mediaFile;
  162. if (type == MEDIA_TYPE_IMAGE){
  163. mediaFile = new File(mediaStorageDir.getPath() + File.separator +
  164. "Karte" + timeStamp + ".jpg");
  165. } else {
  166. return null;
  167. }
  168. return mediaFile;
  169. }
  170. // Returns the amount of points the player receives for the card
  171. private int getCardPoints (String cardName) {
  172. if (cardName.contains("Ass")) {
  173. return 11;
  174. }
  175. if (cardName.contains("Koenig")) {
  176. return 4;
  177. }
  178. if (cardName.contains("Ober")) {
  179. return 3;
  180. }
  181. if (cardName.contains("Unter")) {
  182. return 2;
  183. }
  184. if (cardName.contains("10")) {
  185. return 10;
  186. }
  187. return 0;
  188. }
  189. private void sendFileToServer (final File picture) {
  190. Retrofit.Builder builder = new Retrofit.Builder().baseUrl(serverURL).addConverterFactory(GsonConverterFactory.create());
  191. Retrofit retrofit = builder.build();
  192. api_server client = retrofit.create(api_server.class);
  193. MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", picture.getName(), RequestBody.create(MediaType.parse("image/*"), picture));
  194. Call<POJOClass> call = client.uploadAttachment(filePart);
  195. call.enqueue(new Callback<POJOClass>() {
  196. @Override
  197. public void onResponse(Call<POJOClass> call, Response<POJOClass> response) {
  198. String card = response.body().getKarte();
  199. captureLog.setText(captureLog.getText() + "\n" + card + " erkannt!");
  200. points += getCardPoints(card);
  201. // Delete picture to minimize memory usage
  202. if ( picture.delete() );
  203. // Show current points to user
  204. Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.points) + points , Toast.LENGTH_SHORT);
  205. toast.show();
  206. }
  207. @Override
  208. public void onFailure(Call<POJOClass> call, Throwable t) {
  209. captureLog.setText(captureLog.getText() + "\n" + "Error..." + t.getMessage());
  210. // Delete picture to minimize memory usage
  211. if ( picture.delete() );
  212. }
  213. });
  214. }
  215. }