Search code examples
androidcallbackandroid-cameraresolution

Camera takePicture does not always do jpeg callback depending on picture size


I am with stuck problem where camera.takePicture() jpeg callback gets less frequently called where desired picture size is higher. E.g. it always succeedes with 640x480, but only works on 5% to 40% of cases with minor parameter tweaks with 3264x2448 resolution. Relevant code:

public void foo(View view) {
    Camera.PictureCallback jpegCallback = new Camera.PictureCallback(){
        @Override
        public void onPictureTaken(byte[] bytes, Camera camera) {
            camera.release();
            Toast.makeText(SurvCamActivity.getInstance(), "jpeg callback", Toast.LENGTH_SHORT).show();
        }
    };
    //Gets desired resolution
    SpinnerResolution res = getSelectedSpinnerResolution();
    Camera camera = getCameraInstance();
    try {
        Camera.Parameters params = camera.getParameters();
        params.setPictureSize(res.width, res.height);
        camera.setParameters(params);
        camera.setPreviewTexture(new SurfaceTexture(R.layout.fragment_preview));
        camera.startPreview();
        camera.takePicture(null, null, jpegCallback);
    } catch (IOException e) {
        e.printStackTrace();
    } 
}

Now odd thing is, if I add breakpoint to camera.takePicture() and simply resume execution once thread reaches there it always calls the jpeg callback. When I added Thread.sleep(700) after the camera.takePicture() it also started consistently doing the callbacks. Any ideas? Tested on Nexus 4.


Solution

  • EDIT: Turns out the camera object might have got garbage collected. I created static reference to it and now it works without problems.