22

Am developing a simple camera app. I have code that takes screenshot of the whole activity and writes it to the SD card. The problem is that the SurfaceView returns a black screen.

I would like to know how to independently take a screenshot of the SurfaceView only. Here is the code that takes the screenshot of the whole activity.

findViewById(R.id.screen).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        final RelativeLayout layout = (RelativeLayout) findViewById(R.id.RelativeLayout1);
        layout.setVisibility(RelativeLayout.GONE);
        Bitmap bitmap = takeScreenshot();
        Toast.makeText(getApplicationContext(),"Please Wait", Toast.LENGTH_LONG).show();
        saveBitmap(bitmap);
    }
});

public Bitmap takeScreenshot() {
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}


public void saveBitmap(Bitmap bitmap) {
    final MediaPlayer cheer = MediaPlayer.create(PicShot.this, R.raw.shutter);
    cheer.start();
    
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".png";

    final RelativeLayout layout = (RelativeLayout) findViewById(R.id.RelativeLayout1);
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/" + fname);
    FileOutputStream fos;

    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
        layout.setVisibility(RelativeLayout.VISIBLE);
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/*");
        Uri uri = Uri.fromFile(imagePath);
        share.putExtra(Intent.EXTRA_STREAM,uri);
        startActivity(Intent.createChooser(share, "Share Image"));
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}
SerjantArbuz
  • 982
  • 1
  • 12
  • 16
Donnie Ibiyemi
  • 971
  • 3
  • 15
  • 26

6 Answers6

17

The SurfaceView's surface is independent of the surface on which View elements are drawn. So capturing the View contents won't include the SurfaceView.

You need to capture the SurfaceView contents separately and perform your own composition step. The easiest way to do the capture is probably to just re-render the contents, but use an off-screen bitmap as the target rather than the surface. If you're rendering with GLES to an off-screen pbuffer, you can use glReadPixels() before you swap buffers.

Update: Grafika's "texture from camera" activity demonstrates handling live video from the camera with OpenGL ES. EglSurfaceBase#saveFrame() shows how to capture GLES rendering to a Bitmap.

Update: See also this answer, which provides a bit more background.

Community
  • 1
  • 1
fadden
  • 51,356
  • 5
  • 116
  • 166
  • Am an android newbie. please kindly post sample code on how to do that.thanks – Donnie Ibiyemi Aug 01 '14 at 21:32
  • 1
    Assuming all of your rendering is done to a Canvas you get from the Surface, just create a Canvas backed by a Bitmap (see e.g. http://developer.android.com/guide/topics/graphics/2d-graphics.html). Depending on how the surfaces are composed, you can either blit one onto the other, or composite the pair onto a third. – fadden Aug 02 '14 at 04:19
  • Hi, the article you linked does not seem to cover the case of saving to bitmap a surfaceview showing a frame _from a Camera_ . Do you have some sample code for us? – ocramot Sep 10 '14 at 13:40
  • The pixel data source doesn't matter -- once it's on the surface, it's just pixels. – fadden Sep 10 '14 at 15:39
  • Added a link to Grafika. – fadden Oct 10 '14 at 15:32
  • 1
    ... code samples or "what is needed to do" would be interesting as well. +1 for the hint to grafika – user1767754 Apr 28 '15 at 01:57
  • how would you use glReadPixels with surface view. It is kinda confusing, can you provide code sample? – chia yongkang Dec 06 '19 at 07:55
  • Hi @chiayongkang did you get any solution for this case. If you have please post here – Karthikkumar Dec 18 '20 at 11:18
  • check this https://gptshubham595.medium.com/unveiling-the-world-of-ar-with-arcore-my-journey-of-exploration-practical-guide-86ac7f54b34 – Shubham Kumar Gupta Jul 05 '23 at 07:58
12

Just copy and past code

Note: only For API level >= 24

private void takePhoto() {

        // Create a bitmap the size of the scene view.
        final Bitmap bitmap = Bitmap.createBitmap(surfaceView.getWidth(), surfaceView.getHeight(),
                Bitmap.Config.ARGB_8888);



        // Create a handler thread to offload the processing of the image.
        final HandlerThread handlerThread = new HandlerThread("PixelCopier");
        handlerThread.start();
        // Make the request to copy.
        PixelCopy.request(holder.videoView, bitmap, (copyResult) -> {
            if (copyResult == PixelCopy.SUCCESS) {
                Log.e(TAG,bitmap.toString());
                String name = String.valueOf(System.currentTimeMillis() + ".jpg");
                imageFile = ScreenshotUtils.store(bitmap,name);

            } else {
                Toast toast = Toast.makeText(getViewActivity(),
                        "Failed to copyPixels: " + copyResult, Toast.LENGTH_LONG);
                toast.show();
            }
            handlerThread.quitSafely();
        }, new Handler(handlerThread.getLooper()));
    }
ND1010_
  • 3,743
  • 24
  • 41
  • Yup This function will work for 24 and greater version Please see this:-->https://developer.android.com/reference/android/view/PixelCopy – ND1010_ Jul 23 '18 at 09:49
  • Any ideas below API 24? Because none of the answers worked for me. – David Jul 23 '18 at 10:07
  • yes you can use MediaProjection class to capture the entire screen with a virtual display, but it can not provide any pacific area for capturing . only work for entire screen – ND1010_ Jul 23 '18 at 10:37
  • if you want to capture for a specified view you should have to use PixelCopy class – ND1010_ Jul 23 '18 at 10:39
  • why is the holder in holder.videoView an unresolved symbol? – chia yongkang Nov 05 '19 at 07:06
  • 1
    @chiayongkang You have just need to pass your view instead of my `holder.videoView` – ND1010_ Nov 05 '19 at 07:09
  • 1
    @ND1010_ is there the code for the Screenshot.utils class for the store function? Curious to see how you store the bitmap as well. – chia yongkang Nov 05 '19 at 10:32
  • sometimes the object results in the image to have white parts. can it be solved with glReadPixels? if not how can i solve that? – chia yongkang Dec 06 '19 at 07:54
4

My situation related with ExoPlayer, need get bitmap of current frame

Works on API >= 24:

private val copyFrameHandler = Handler()

fun getFrameBitmap(callback: FrameBitmapCallback) {
    when(val view = videoSurfaceView) {
        is TextureView -> callback.onResult(view.bitmap)
        is SurfaceView -> {
            val bitmap = Bitmap.createBitmap(
                    videoSurfaceView.getWidth(),
                    videoSurfaceView.getHeight(),
                    Bitmap.Config.ARGB_8888
            )
            
            copyFrameHandler.removeCallbacksAndMessages(null)
            
            PixelCopy.request(view, bitmap, { copyResult: Int ->
                if (copyResult == PixelCopy.SUCCESS) {
                    callback.onResult(bitmap)
                } else {
                    callback.onResult(null)
                }
            }, copyFrameHandler)
        }
        else -> callback.onResult(null)
    }
}

fun onDestroy() {
    copyFrameHandler.removeCallbacksAndMessages(null)
}

interface FrameBitmapCallback {
    fun onResult(bitmap: Bitmap?)
}
SerjantArbuz
  • 982
  • 1
  • 12
  • 16
3

If your situation allows it, using a TextureView instead of a SurfaceView will make this problem a lot easier to solve. It has a method getBitmap() that returns a Bitmap of the current frame on the TextureView.

MarkyDD
  • 272
  • 5
  • 16
1

This is how I do it. Put this method in some Util class:

/**
 * Pixel copy to copy SurfaceView/VideoView into BitMap
 */
fun usePixelCopy(videoView: SurfaceView,  callback: (Bitmap?) -> Unit) {
    val bitmap: Bitmap = Bitmap.createBitmap(
        videoView.width,
        videoView.height,
        Bitmap.Config.ARGB_8888
    );
    try {
        // Create a handler thread to offload the processing of the image.
        val handlerThread = HandlerThread("PixelCopier");
        handlerThread.start();
        PixelCopy.request(
            videoView, bitmap,
            PixelCopy.OnPixelCopyFinishedListener { copyResult ->
                if (copyResult == PixelCopy.SUCCESS) {
                    callback(bitmap)
                }
                handlerThread.quitSafely();
            },
            Handler(handlerThread.looper)
        )
    } catch (e: IllegalArgumentException) {
        callback(null)
        // PixelCopy may throw IllegalArgumentException, make sure to handle it
        e.printStackTrace()
    }
}

Usage:

usePixelCopy(videoView) { bitmap: Bitmap? ->
    processBitMp(bitmap)
}

Note: VideoView is a subclass of SurfaceView, so this method can take screenshot of video View as well.

SerjantArbuz
  • 982
  • 1
  • 12
  • 16
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
1

For those of us looking for a solution for API <= 24, this is what I did as a workaround. When user clicks button for capture, close the preview captureSession and create a new captureSession for still image capture.

mCaptureSession.stopRepeating();
mCaptureSession.abortCaptures();
mCaptureSession.close();
mCaptureSession = null;

mPreviewReader = ImageReader.newInstance(previewSize.getWidth(), previewSize.getHeight(), ImageFormat.JPEG, MAX_IMAGES);
mPreviewReader.setOnImageAvailableListener(mImageAvailableListener, mBackgroundHandler);

mCameraDevice.createCaptureSession(
    Arrays.asList(mPreviewReader.getSurface()),
    //Arrays.asList(mSurface),
    new CameraCaptureSession.StateCallback() {
        @Override
        public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
            mCaptureSession = cameraCaptureSession;
            try {
                final CaptureRequest.Builder captureBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
                captureBuilder.addTarget(mPreviewReader.getSurface());
                captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
                captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(mOrientation));
                Log.d(TAG, "Capture request created.");
                mCaptureSession.capture(captureBuilder.build(), mCaptureCallback, mBackgroundHandler);
            } catch (CameraAccessException cae) {
                Log.d(TAG, cae.toString());
            }
        }

        @Override
        public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {}
    },
    mBackgroundHandler
);

Then at onImageAvailableListener, you can get the image with imageReader.acquireNextImage() to get the still captured image.

SerjantArbuz
  • 982
  • 1
  • 12
  • 16
Ata
  • 31
  • 3