Search code examples
androidcamera2

Screenshot and camera preview not working at same time


enter image description here

This picture on the left shows the app before any buttons are clicked. I have used the camera2 google github example to make the backround a camera preview. The picture on the right is when I have clicked the takePicture button. The picture isnt showing! Does the screenshot and camera not work at the same time? Also how should I get rid of the black bar that appears at the top? Any help would be appreciated. Maybe lock the focus of the camera before i take the screenshot??

    private void takePicture() {
        if (picturePresent == false) {
            edit_button.setVisibility(View.INVISIBLE);

            pictureBitmap = getBitmapFromView();

            edit_button.setVisibility(View.VISIBLE);

            BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), pictureBitmap);
            outer_backround.setBackground(bitmapDrawable);
            picturePresent = true;

        } else {
        }
    }



    public Bitmap getBitmapFromView() {
        View v1 = getActivity().getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);
        Bitmap resizedBitmap  = Bitmap.createBitmap(bitmap,(int) outer_backround.getX(), (int) outer_backround.getY(), outer_backround.getWidth(), outer_backround.getHeight());
        return bitmap;
    }

Solution

  • You cannot getDrawingCache from camera preview surface.

    You must read pixels from glsurface, or get frame.

    Look at this example: https://stackoverflow.com/a/26303163/5690332