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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package project.schafkopfzaehler_2;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.hardware.Camera;
  5. import android.net.Uri;
  6. import android.os.Environment;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  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 java.io.ByteArrayOutputStream;
  15. import java.io.File;
  16. import java.io.FileNotFoundException;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import okhttp3.MediaType;
  20. import okhttp3.MultipartBody;
  21. import okhttp3.RequestBody;
  22. import project.schafkopfzaehler_2.POJO.POJOClass;
  23. import retrofit2.Call;
  24. import retrofit2.Callback;
  25. import retrofit2.Response;
  26. import retrofit2.Retrofit;
  27. import retrofit2.converter.gson.GsonConverterFactory;
  28. public class CardCaptureActivity extends AppCompatActivity {
  29. private Camera mCamera;
  30. private cameraPreview mPreview;
  31. Button capture; // Button init
  32. TextView captureLog; // TextView init
  33. public static final int MEDIA_TYPE_IMAGE = 1;
  34. // Add a listener to the Capture button
  35. private View.OnClickListener startCaptureListener = new View.OnClickListener() {
  36. @Override
  37. public void onClick (View v) {
  38. if (v == capture) {
  39. mCamera.takePicture(null, null, mPicture);
  40. }
  41. }
  42. };
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(R.layout.cardcapture);
  47. // Create an instance of Camera
  48. mCamera = getCameraInstance();
  49. // Create our Preview view and set it as the content of our activity.
  50. mPreview = new cameraPreview(this, mCamera);
  51. FrameLayout preview = findViewById(R.id.cameraPreview);
  52. preview.addView(mPreview);
  53. capture = findViewById(R.id.capture);
  54. capture.setOnClickListener(startCaptureListener);
  55. }
  56. /* Get an instance of the Camera object. */
  57. public static Camera getCameraInstance(){
  58. Camera c = null;
  59. try {
  60. c = Camera.open(); // attempt to get a Camera instance
  61. }
  62. catch (Exception e){
  63. // Camera is not available (in use or does not exist)
  64. }
  65. return c; // returns null if camera is unavailable
  66. }
  67. @Override
  68. protected void onPause() {
  69. super.onPause();
  70. releaseCamera();
  71. }
  72. @Override
  73. protected void onResume() {
  74. super.onResume();
  75. }
  76. @Override
  77. protected void onRestart() {
  78. super.onRestart();
  79. }
  80. @Override
  81. protected void onStart() {
  82. super.onStart();
  83. }
  84. /** Picture button was pressed. Send picture to server for card recognition. */
  85. private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
  86. @Override
  87. public void onPictureTaken(byte[] data, Camera camera) {
  88. File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
  89. if (pictureFile == null){
  90. Log.d("CAMERA", "Error creating media files");
  91. return;
  92. }
  93. // Compress image to lower site
  94. Bitmap bmp = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
  95. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  96. bmp.compress(Bitmap.CompressFormat.JPEG, 20, bytes);
  97. captureLog = findViewById(R.id.capture_log);
  98. try {
  99. FileOutputStream fos = new FileOutputStream(pictureFile);
  100. fos.write(bytes.toByteArray());
  101. fos.close();
  102. Log.d("CAMERA", "Saved...");
  103. captureLog.setText(captureLog.getText() + "\n" + "Saved...");
  104. } catch (FileNotFoundException e) {
  105. Log.d("CAMERA", "File not found: " + e.getMessage());
  106. captureLog.setText(captureLog.getText() + "\n" + "File not found: " + e.getMessage());
  107. } catch (IOException e) {
  108. Log.d("CAMERA", "Error accessing file: " + e.getMessage());
  109. captureLog.setText(captureLog.getText() + "\n" + "Error accessing file: " + e.getMessage());
  110. }
  111. // ------------------------ Send file to server -------------------------
  112. Retrofit.Builder builder = new Retrofit.Builder().baseUrl("http://192.168.0.195:8000/").addConverterFactory(GsonConverterFactory.create());
  113. Retrofit retrofit = builder.build();
  114. api_server client = retrofit.create(api_server.class);
  115. MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", pictureFile.getName(), RequestBody.create(MediaType.parse("image/*"), pictureFile));
  116. Call<POJOClass> call = client.uploadAttachment(filePart);
  117. try {
  118. call.enqueue(new Callback<POJOClass>() {
  119. @Override
  120. public void onResponse(Call<POJOClass> call, Response<POJOClass> response) {
  121. String card = response.body().getKarte();
  122. captureLog.setText(captureLog.getText() + "\n" + card + " erkannt :)");
  123. }
  124. @Override
  125. public void onFailure(Call<POJOClass> call, Throwable t) {
  126. captureLog.setText(captureLog.getText() + "\n" + "Error..." + t.getMessage());
  127. }
  128. });
  129. }
  130. catch (Exception e){
  131. Log.d("Schafkopfzaehler", "EXCEPTION: " + e.getMessage());
  132. }
  133. // ----------------------------------------------------------------------
  134. mCamera.startPreview();
  135. }
  136. };
  137. private void releaseCamera(){
  138. if (mCamera != null){
  139. mCamera.release(); // release the camera for other applications
  140. mCamera = null;
  141. }
  142. }
  143. /** Create a file Uri for saving an image or video */
  144. private static Uri getOutputMediaFileUri(int type){
  145. return Uri.fromFile(getOutputMediaFile(type));
  146. }
  147. /** Create a File for saving the captured image */
  148. private static File getOutputMediaFile(int type){
  149. // To be safe, you should check that the SDCard is mounted
  150. // using Environment.getExternalStorageState() before doing this.
  151. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
  152. Environment.DIRECTORY_PICTURES), "Schafkopfzaehler");
  153. // This location works best if you want the created images to be shared
  154. // between applications and persist after your app has been uninstalled.
  155. // Create the storage directory if it does not exist
  156. if (! mediaStorageDir.exists()){
  157. if (! mediaStorageDir.mkdirs()){
  158. Log.d("Schafkopfzaehler", "failed to create directory");
  159. return null;
  160. }
  161. }
  162. // Create a media file name
  163. File mediaFile;
  164. if (type == MEDIA_TYPE_IMAGE){
  165. mediaFile = new File(mediaStorageDir.getPath() + File.separator +
  166. "Karte.jpg");
  167. } else {
  168. return null;
  169. }
  170. return mediaFile;
  171. }
  172. }