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.7KB

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