Search code examples
javaandroidscreenshotarcoresceneform

How can I take Screenshots using Arcore?


I am trying to take a screenshot of my Augmented Reality Screen and pass it as a bitmap to another activity.

This is the code that I am using to take the screenshot:

Function to take screen shot

public static void tmpScreenshot(Bitmap bmp, Context context){
        try {
            //Write file
            String filename = "bitmap.png";
            FileOutputStream stream = context.openFileOutput(filename, Context.MODE_PRIVATE);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

            //Cleanup
            stream.close();
            bmp.recycle();

            //Pop intent
            Intent in1 = new Intent(context, CostActivity.class);
            in1.putExtra("image", filename);
            context.startActivity(in1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Function to receive screenshot

private void loadTmpBitmap() {
        Bitmap bmp = null;
        String filename = getIntent().getStringExtra("image");
        try {
            FileInputStream is = this.openFileInput(filename);
            bmp = BitmapFactory.decodeStream(is);
            ImageView imageView = findViewById(R.id.test);
            imageView.setImageBitmap(Bitmap.createScaledBitmap(bmp, 120, 120, false));
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Even though the Screenshot was taken, it was black when it is passed to another activity. In addition, the Screenshot only appeared after I pressed the back button

Can anyone help me with the code to take a screenshot with ARCore? Or what am I doing wrong?


Solution

  • It is not possible to take a screenshot of a SurfaceView using your method. If you do then the screenshot will be black, as it only works for regular views.

    What you need to use is pixelcopy.

        private void takePhoto() {
        ArSceneView view = arFragment.getArSceneView();
    
        // Create a bitmap the size of the scene view.
        final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.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(view, bitmap, (copyResult) -> {
            if (copyResult == PixelCopy.SUCCESS) {
                try {
                    saveBitmapToDisk(bitmap);
                } catch (IOException e) {
                    Toast toast = Toast.makeText(VisualizerActivity.this, e.toString(),
                            Toast.LENGTH_LONG);
                    toast.show();
                    return;
                }
                SnackbarUtility.showSnackbarTypeLong(settingsButton, "Screenshot saved in /Pictures/Screenshots");
    
    
    
    
            } else {
    
                SnackbarUtility.showSnackbarTypeLong(settingsButton, "Failed to take screenshot");
    
            }
            handlerThread.quitSafely();
        }, new Handler(handlerThread.getLooper()));
    }
    
    
    public void saveBitmapToDisk(Bitmap bitmap) throws IOException {
    
      //  String path = Environment.getExternalStorageDirectory().toString() +  "/Pictures/Screenshots/";
    
        if (videoDirectory == null) {
            videoDirectory =
                    new File(
                            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                                    + "/Screenshots");
        }
    
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
        String formattedDate = df.format(c.getTime());
    
        File mediaFile = new File(videoDirectory, "FieldVisualizer"+formattedDate+".jpeg");
    
        FileOutputStream fileOutputStream = new FileOutputStream(mediaFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
    }