在允许用户使用按钮拍摄照片的同时,如何定期拍摄隐藏的照片?

时间:2018-11-09 11:24:43

标签: android image android-camera photo hidden

阅读了多个与我的问题相似的问题后,我认为该问题适用于专业人士!

我正在使用Android Studio。

我正在尝试允许系统定期“隐藏”图片。同时允许用户使用按钮拍照。

尝试解释应该发生的情况-假设您正在打开相机并尝试拍摄完美的照片,那么找到正确的角度需要花费一些时间。显然,一直以来,您都可以预览相机所看到的内容,并且预览一直持续到您按下“拍照” 按钮。我要创建的是在按下“拍摄图片” 按钮之前的时间内,系统正在后台拍摄隐藏的图片。稍后,我将使用用户拍摄的照片以及系统拍摄的照片,并将它们保存在我的设备上以供以后编辑。

[很显然,我无法使用android手机的内置摄像头应用程序]

到目前为止,我一直依靠: https://developer.android.com/guide/topics/media/camera#custom-camera 为我的代码的框架。

注意1:任何建议都将受到欢迎。

注意2:欢迎对我的代码进行任何改进。

注意3:我已经在一些论坛上阅读了可以通过某种方式使用SurfaceTexture的内容,但是我发现这些示例很难理解

注意4:我已经在一些论坛上阅读到可以以某种方式使用Camera2的示例,但是我发现示例难以理解,因此,我目前正在使用已弃用的示例Camera类。

下面提供了我的代码:

public class CameraActivity extends AppCompatActivity {

    private Camera mCamera;
    private CameraPreview mPreview; 
    // variable to break the photo taking loop
    boolean wasButtonPressed = false;
    static int numOfPicturesTakenUpTo25PPS = 0;
    boolean isItSafeToTakePicture = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        // Create an instance of Camera
        mCamera = getCameraInstance();
        // Create our Preview view and set it as the content of our activity.
        try {
            mPreview = new CameraPreview(this, mCamera);
            FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
            preview.addView(mPreview);
        } catch (Exception e) {
            Log.d(TAG, "error: in the preview creation: " + e.getMessage());
            e.printStackTrace();
        }
        //double check
        numOfPicturesTakenUpTo25PPS = 0;
        // Add a listener to the Capture button
        Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //
                        wasButtonPressed = true;

                        if (isItSafeToTakePicture == true) {
                            mCamera.takePicture(null, null, mPicture);
                            isItSafeToTakePicture = false;  //will be changed later to true by "onPictureTaken"
                        }

                    }
                }
        );
    }

    @Override
    protected void onStart(){
        super.onStart();
        Toast.makeText(getApplicationContext(), "user - please wait 2 seconds before taking pictures. (should be 2 secs)",Toast.LENGTH_LONG).show();
        // note user 2 secs have passed and its ok to use:
        Toast.makeText(getApplicationContext(), "You can start taking pictures",Toast.LENGTH_SHORT).show();
        // start taking pictures in a loop.       
        while (wasButtonPressed == false) {
            if (isItSafeToTakePicture == true) {
                mCamera.takePicture(null, null, mPicture);
                isItSafeToTakePicture = false;  //will be changed later to true by "onPictureTaken"
            }
        }

        //TODO: do I need to ensure that this [code above] won't be an infinite loop?


    }

    @Override
    protected void onResume(){
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseCamera();              // release the camera immediately on pause event
    }

    @Override
    protected void onStop() {
        super.onStop();
        releaseCamera();              // release the camera immediately
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseCamera();              // release the camera immediately
    }

    //----------------------------------------------------------------------------------------------------------------//
    //-------------------------end of system functions. beginning of helping functions -------------------------------//
    //----------------------------------------------------------------------------------------------------------------//

    private void releaseCamera(){
        if (mCamera != null){
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }
    }

    public static Camera getCameraInstance(){                           
        Camera cam1 = null;
        try {
            int num_of_cameras = Camera.getNumberOfCameras();
            for (int i=0; i<num_of_cameras; i++) {
                Log.d(TAG, "[My Debugging message] Camera loop: i is " + i + " properties are: ");
            }
            cam1 = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
            Log.d(TAG, "[My Debugging message] Error Opening the camera !! error message: " + e.getMessage());
            e.printStackTrace();
        }
        //addition: use "camera parameters" or "camera info" to validate information about the camera
        // end addition
        if (cam1 == null) {
            Log.d(TAG, "[My Debugging message] received a NULL camera !!! ");
        }
        return cam1; // returns null if camera is unavailable
    }



    private static final String TAG = "CameraActivity";


    Context context = this;
    private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            if (numOfPicturesTakenUpTo25PPS >= 25) {            ///only 25 pictures per second. so if we want to capture 2 seconds before button pressed - than //TODO: we need to make it 50 PPS
                numOfPicturesTakenUpTo25PPS = 0;
            }

            File pictureFile = new File(getFilesDir() + File.separator + "IMG_" + numOfPicturesTakenUpTo25PPS + ".bmp");
            String pictureFilename = "IMG_" + numOfPicturesTakenUpTo25PPS + ".bmp";  //used later for the log.

            numOfPicturesTakenUpTo25PPS++;

            if (pictureFile == null) {
                Log.d(TAG, "[My Debugging message] Error creating media file, check storage permissions");
                //no path to picture, return
                isItSafeToTakePicture = true;
                return;
            }

            try {

                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();

            } catch (FileNotFoundException e) {
                Log.d(TAG, "[My Debugging message] File not found: " + e.getMessage());
                e.printStackTrace();
            } catch (IOException e) {
                Log.d(TAG, "[My Debugging message] Error accessing file: " + e.getMessage());
                e.printStackTrace();
            } catch (Exception e) {
                Log.d(TAG, "[My Debugging message] other error:  " + e.getMessage());
                e.printStackTrace();
            }

            //if we got here with no exceptions:
            Log.d(TAG, "[My Debugging message] File saved [supposedly]: " + pictureFilename + " at " + getFilesDir()); 
            //finished saving picture, it is now safe to take a new picture
            isItSafeToTakePicture = true;

            //WARNING - these next few lines are a test
            camera.release();
            camera = null;
        }
    };



    //----------------------------------------------------------------------------------------------------------------//
    //------------------------------------------ Camera Preview Section ----------------------------------------------//
    //----------------------------------------------------------------------------------------------------------------//

    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {              ///TODO: how to fix this shit right here ?:
        private SurfaceHolder mHolder;
        private Camera mCamera;

        // CTOR ?
        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();
                isItSafeToTakePicture = true;
            } catch (IOException e) {
                Log.d(TAG, "[My Debugging message] Error starting camera preview: " + e.getMessage());
                e.printStackTrace();
            } catch (Exception e) {
                Log.d(TAG, "[My Debugging message] Error starting camera preview: " + e.getMessage());
                e.printStackTrace();
            }

        }

        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
                Log.d(TAG, "[My Debugging message] preview surface does not exist [CameraPreview.java]");
                return;
            }
            // stop preview before making changes
            try {
                mCamera.stopPreview();
            } catch (Exception e) {
                // ignore: tried to stop a non-existent preview
                Log.d(TAG, "[My Debugging message] Error STOPPING camera preview: " + e.getMessage());
                e.printStackTrace();
            }
            // set preview size and make any resize, rotate or
            // reformatting changes here


            // start preview with new settings
            try {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
                isItSafeToTakePicture = true;
            } catch (Exception e) {
                Log.d(TAG, "[My Debugging message] Error starting camera preview: " + e.getMessage());
                e.printStackTrace();
            }
        }

        ///-----------------------------------------------------------------------------------------------------///
    }
    }

谢谢。

0 个答案:

没有答案