123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- 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;
- }
-
- }
|