diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index bca8fdd..016cdc8 100644 Binary files a/.idea/caches/build_file_checksums.ser and b/.idea/caches/build_file_checksums.ser differ diff --git a/.idea/misc.xml b/.idea/misc.xml index c0f68ed..99202cc 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -25,7 +25,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 95cae58..d28ac8b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -23,8 +23,6 @@ dependencies { implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:27.1.1' - testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' - implementation project(':openCVLibrary343') } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e36114f..5e9d128 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,12 +1,24 @@ - - - - - + + + + + + + + + + android:theme="@style/AppTheme.NoActionBar" /> + android:theme="@style/AppTheme.NoActionBar" /> + \ No newline at end of file diff --git a/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java b/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java new file mode 100644 index 0000000..81a93d4 --- /dev/null +++ b/app/src/main/java/project/schafkopfzaehler_2/CardCaptureActivity.java @@ -0,0 +1,164 @@ +package project.schafkopfzaehler_2; + +import android.hardware.Camera; +import android.net.Uri; +import android.os.Environment; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.FrameLayout; +import android.widget.TextView; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +public class CardCaptureActivity extends AppCompatActivity { + + private Camera mCamera; + private cameraPreview mPreview; + Button capture; // Button init + TextView captureLog; // TextView init + public static final int MEDIA_TYPE_IMAGE = 1; + + // Add a listener to the Capture button + private View.OnClickListener startCaptureListener = new View.OnClickListener() { + + @Override + public void onClick (View v) { + if (v == capture) { + mCamera.takePicture(null, null, mPicture); + } + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + + super.onCreate(savedInstanceState); + setContentView(R.layout.cardcapture); + + // Create an instance of Camera + mCamera = getCameraInstance(); + + // Create our Preview view and set it as the content of our activity. + mPreview = new cameraPreview(this, mCamera); + FrameLayout preview = findViewById(R.id.cameraPreview); + preview.addView(mPreview); + + capture = findViewById(R.id.capture); + capture.setOnClickListener(startCaptureListener); + + } + + /* Get an instance of the Camera object. */ + public static Camera getCameraInstance(){ + Camera c = null; + try { + c = Camera.open(); // attempt to get a Camera instance + } + catch (Exception e){ + // Camera is not available (in use or does not exist) + } + return c; // returns null if camera is unavailable + } + + + @Override + protected void onPause() { + + super.onPause(); + releaseCamera(); + } + + @Override + protected void onResume() { + super.onResume(); + } + + @Override + protected void onRestart() { + super.onRestart(); + } + + @Override + protected void onStart() { + super.onStart(); + } + + private Camera.PictureCallback mPicture = new Camera.PictureCallback() { + + @Override + public void onPictureTaken(byte[] data, Camera camera) { + + File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); + if (pictureFile == null){ + Log.d("CAMERA", "Error creating media file, check storage permissions"); + return; + } + + try { + FileOutputStream fos = new FileOutputStream(pictureFile); + fos.write(data); + fos.close(); + } catch (FileNotFoundException e) { + Log.d("CAMERA", "File not found: " + e.getMessage()); + } catch (IOException e) { + Log.d("CAMERA", "Error accessing file: " + e.getMessage()); + } + + captureLog = findViewById(R.id.capture_log); + captureLog.setText("Karte scannen --> an Server senden"); + + } + }; + + private void releaseCamera(){ + if (mCamera != null){ + mCamera.release(); // release the camera for other applications + mCamera = null; + } + } + + /** Create a file Uri for saving an image or video */ + private static Uri getOutputMediaFileUri(int type){ + return Uri.fromFile(getOutputMediaFile(type)); + } + + /** Create a File for saving the captured image */ + private static File getOutputMediaFile(int type){ + // To be safe, you should check that the SDCard is mounted + // using Environment.getExternalStorageState() before doing this. + + File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_PICTURES), "MyCameraApp"); + // This location works best if you want the created images to be shared + // between applications and persist after your app has been uninstalled. + + // Create the storage directory if it does not exist + if (! mediaStorageDir.exists()){ + if (! mediaStorageDir.mkdirs()){ + Log.d("MyCameraApp", "failed to create directory"); + return null; + } + } + + // Create a media file name + Long tsLong = System.currentTimeMillis()/1000; + String timeStamp = tsLong.toString(); + File mediaFile; + if (type == MEDIA_TYPE_IMAGE){ + mediaFile = new File(mediaStorageDir.getPath() + File.separator + + "IMG_"+ timeStamp + ".jpg"); + } else { + return null; + } + + return mediaFile; + } + +} + diff --git a/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java b/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java index d35b56a..7c9dc9d 100644 --- a/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java +++ b/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java @@ -1,6 +1,8 @@ package project.schafkopfzaehler_2; import android.content.Intent; +import android.hardware.camera2.CameraMetadata; +import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.design.widget.FloatingActionButton; @@ -11,13 +13,14 @@ import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; +import android.widget.VideoView; public class MainGameActivity extends AppCompatActivity { private Button p1, p2, p3, p4, chooseGame; // Button init private TextView choice; private String playerNames[] = {"", "", "", ""}; // Player names init - static final int REQUEST_VIDEO_CAPTURE = 1; + static final int REQUEST_IMAGE_CAPTURE = 1; private View.OnClickListener startClickListener = new View.OnClickListener() { @@ -87,12 +90,9 @@ public class MainGameActivity extends AppCompatActivity { private void playerButtonClicked (View v) { Log.d("MainGame", v + " clicked..."); - Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); - if (takeVideoIntent.resolveActivity(getPackageManager()) != null) { - startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE); - } - - + // Start card capture activity with the camera preview + Intent cardCaptureIntent = new Intent(this, CardCaptureActivity.class); + startActivity(cardCaptureIntent); } private void gameChoosing () { @@ -109,10 +109,6 @@ public class MainGameActivity extends AppCompatActivity { choice.setText(announcement); } } - - if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) { - // Do nothing... - } } } diff --git a/app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java b/app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java new file mode 100644 index 0000000..b4892a9 --- /dev/null +++ b/app/src/main/java/project/schafkopfzaehler_2/cameraPreview.java @@ -0,0 +1,69 @@ +package project.schafkopfzaehler_2; + +import android.content.Context; +import android.hardware.Camera; +import android.util.Log; +import android.view.SurfaceHolder; +import android.view.SurfaceView; + +import java.io.IOException; + +/** A basic Camera preview class */ +public class cameraPreview extends SurfaceView implements SurfaceHolder.Callback { + private SurfaceHolder mHolder; + private Camera mCamera; + + public cameraPreview(Context context, Camera camera) { + super(context); + mCamera = camera; + + // Install a SurfaceHolder.Callback so we get notified when the + // underlying surface is created and destroyed. + mHolder = getHolder(); + mHolder.addCallback(this); + // deprecated setting, but required on Android versions prior to 3.0 + mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); + } + + public void surfaceCreated(SurfaceHolder holder) { + // The Surface has been created, now tell the camera where to draw the preview. + try { + mCamera.setPreviewDisplay(holder); + mCamera.startPreview(); + } catch (IOException e) { + Log.d("CAMERA", "Error setting camera preview: " + e.getMessage()); + } + } + + public void surfaceDestroyed(SurfaceHolder holder) { + // empty. Take care of releasing the Camera preview in your activity. + } + + public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { + // If your preview can change or rotate, take care of those events here. + // Make sure to stop the preview before resizing or reformatting it. + + if (mHolder.getSurface() == null){ + // preview surface does not exist + return; + } + + // stop preview before making changes + try { + mCamera.stopPreview(); + } catch (Exception e){ + // ignore: tried to stop a non-existent preview + } + + mCamera.setDisplayOrientation(90); + + // start preview with new settings + try { + mCamera.setPreviewDisplay(mHolder); + mCamera.startPreview(); + + } catch (Exception e){ + Log.d("CAMERA", "Error starting camera preview: " + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/cardcapture.xml b/app/src/main/res/layout/cardcapture.xml index a939e27..768e916 100644 --- a/app/src/main/res/layout/cardcapture.xml +++ b/app/src/main/res/layout/cardcapture.xml @@ -1,36 +1,42 @@ + android:layout_height="match_parent"> - + android:layout_height="match_parent"> -