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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package project.schafkopfzaehler_2;
  2. import android.hardware.Camera;
  3. import android.net.Uri;
  4. import android.os.Environment;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.FrameLayout;
  11. import android.widget.TextView;
  12. import java.io.File;
  13. import java.io.FileNotFoundException;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. public class CardCaptureActivity extends AppCompatActivity {
  17. private Camera mCamera;
  18. private cameraPreview mPreview;
  19. Button capture; // Button init
  20. TextView captureLog; // TextView init
  21. public static final int MEDIA_TYPE_IMAGE = 1;
  22. // Add a listener to the Capture button
  23. private View.OnClickListener startCaptureListener = new View.OnClickListener() {
  24. @Override
  25. public void onClick (View v) {
  26. if (v == capture) {
  27. mCamera.takePicture(null, null, mPicture);
  28. }
  29. }
  30. };
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.cardcapture);
  35. // Create an instance of Camera
  36. mCamera = getCameraInstance();
  37. // Create our Preview view and set it as the content of our activity.
  38. mPreview = new cameraPreview(this, mCamera);
  39. FrameLayout preview = findViewById(R.id.cameraPreview);
  40. preview.addView(mPreview);
  41. capture = findViewById(R.id.capture);
  42. capture.setOnClickListener(startCaptureListener);
  43. }
  44. /* Get an instance of the Camera object. */
  45. public static Camera getCameraInstance(){
  46. Camera c = null;
  47. try {
  48. c = Camera.open(); // attempt to get a Camera instance
  49. }
  50. catch (Exception e){
  51. // Camera is not available (in use or does not exist)
  52. }
  53. return c; // returns null if camera is unavailable
  54. }
  55. @Override
  56. protected void onPause() {
  57. super.onPause();
  58. releaseCamera();
  59. }
  60. @Override
  61. protected void onResume() {
  62. super.onResume();
  63. }
  64. @Override
  65. protected void onRestart() {
  66. super.onRestart();
  67. }
  68. @Override
  69. protected void onStart() {
  70. super.onStart();
  71. }
  72. private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
  73. @Override
  74. public void onPictureTaken(byte[] data, Camera camera) {
  75. File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
  76. if (pictureFile == null){
  77. Log.d("CAMERA", "Error creating media file, check storage permissions");
  78. return;
  79. }
  80. try {
  81. FileOutputStream fos = new FileOutputStream(pictureFile);
  82. fos.write(data);
  83. fos.close();
  84. } catch (FileNotFoundException e) {
  85. Log.d("CAMERA", "File not found: " + e.getMessage());
  86. } catch (IOException e) {
  87. Log.d("CAMERA", "Error accessing file: " + e.getMessage());
  88. }
  89. captureLog = findViewById(R.id.capture_log);
  90. captureLog.setText("Karte scannen --> an Server senden");
  91. }
  92. };
  93. private void releaseCamera(){
  94. if (mCamera != null){
  95. mCamera.release(); // release the camera for other applications
  96. mCamera = null;
  97. }
  98. }
  99. /** Create a file Uri for saving an image or video */
  100. private static Uri getOutputMediaFileUri(int type){
  101. return Uri.fromFile(getOutputMediaFile(type));
  102. }
  103. /** Create a File for saving the captured image */
  104. private static File getOutputMediaFile(int type){
  105. // To be safe, you should check that the SDCard is mounted
  106. // using Environment.getExternalStorageState() before doing this.
  107. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
  108. Environment.DIRECTORY_PICTURES), "MyCameraApp");
  109. // This location works best if you want the created images to be shared
  110. // between applications and persist after your app has been uninstalled.
  111. // Create the storage directory if it does not exist
  112. if (! mediaStorageDir.exists()){
  113. if (! mediaStorageDir.mkdirs()){
  114. Log.d("MyCameraApp", "failed to create directory");
  115. return null;
  116. }
  117. }
  118. // Create a media file name
  119. Long tsLong = System.currentTimeMillis()/1000;
  120. String timeStamp = tsLong.toString();
  121. File mediaFile;
  122. if (type == MEDIA_TYPE_IMAGE){
  123. mediaFile = new File(mediaStorageDir.getPath() + File.separator +
  124. "IMG_"+ timeStamp + ".jpg");
  125. } else {
  126. return null;
  127. }
  128. return mediaFile;
  129. }
  130. }