Building my own camera application + Automatically capture the image

后端 未结 3 791
一整个雨季
一整个雨季 2021-02-01 00:06

I have created my own camera application. And when I click the button it takes the picture and saves it in the galary. What I want to do is to take the picture without a preview

相关标签:
3条回答
  • 2021-02-01 00:13

    its simple create another method with a timer variable with 10000 delay which finishes the activity of previewing then you will be done. The logic is that these method will run 5secs after timer 1 activity is done.

    0 讨论(0)
  • 2021-02-01 00:23

    Ok I found a answer to take the picture automatically. Adding as a comment for the use of others.

    final Timer t = new Timer();
    t.schedule(new TimerTask() { 
    @Override 
    public void run() { 
    mCamera.takePicture(null, null, mPicture); t.cancel(); 
    } 
    },5000);
    
    0 讨论(0)
  • 2021-02-01 00:24

    the trick is to give the Camera Class a SurfaceView which is not part of the view hierarchy. The following code is from one of my apps where i used this technique to display my own

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            if (mCamera != null) {
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            mCamera = Camera.open();
            startPreview();
        }
    
        private void startPreview() {
                if (mCamera != null) {
                    mCamera.stopPreview();
                    try {
                        mCamera.setPreviewDisplay(new SurfaceView(this).getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mCamera.setPreviewCallbackWithBuffer(this);
    
                    Camera.Parameters parameters = mCamera.getParameters();    
                    parameters.setPreviewSize(IMAGE_W, IMAGE_H);
                    mCamera.setParameters(parameters);
                    PixelFormat p = new PixelFormat();
                    PixelFormat.getPixelFormatInfo(parameters.getPreviewFormat(), p);
                    int bufSize = (IMAGE_W * IMAGE_H * p.bitsPerPixel) / 8;
                    mCamera.addCallbackBuffer(new byte[bufSize]);
                    mCamera.startPreview();
                }
            }
        public void onPreviewFrame(final byte[] data, Camera camera) {
            if (mCamera == null) {
                return;
            }
            mCamera.addCallbackBuffer(data);
        }
    }
    
    0 讨论(0)
提交回复
热议问题