Categories
Android Development

Camera capture custom view Android

I’m learning about Surfaceview and Camera together.

I can’t seem to capture my CustomSurfaceView, below is the code of my OnCreate and how I perform the capture of the CustomSurfaceView.

Below are the links I’ve researched:
https://inneka.com/programming/android/how-does-androids-setdrawingcacheenabled-work/

https://arpitonline.com/2012/07/17/capturing-bitmaps-of-views-in-android/

This is what I understand is needed; I tried it but it still does not work

//customSurfaceView.setVisibility(View.VISIBLE)
//customSurfaceView.setDrawingCacheEnabled(true)
//customSurfaceView.buildDrawingCache()


private CustomSurfaceView customSurfaceView; 

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = findViewById(R.id.iv_screenshot);
    surfaceView = findViewById(R.id.surfaceView);
    if (surfaceView != null) {
        boolean result = checkPermission();
        if (result) {
            setupSurfaceHolder();
        }
    }
    if (canvasLayout == null) {
        canvasLayout = (LinearLayout) findViewById(R.id.customViewLayout);
    }

    // Create custom surfaceview object.
    customSurfaceView = new CustomSurfaceView(getApplicationContext());
//        customSurfaceView.set

    // Set this as the onTouchListener to process custom surfaceview ontouch event.
    customSurfaceView.setOnTouchListener(this);

    // Add the custom surfaceview object to the layout.
    canvasLayout.addView(customSurfaceView);
}

    private void saveSecondSurfaceVice() {
    View content = customSurfaceView;
//        View content = getWindow().getDecorView().getRootView();
//        customSurfaceView.setVisibility(View.VISIBLE);
        content.setDrawingCacheEnabled(true);
//        content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        content.buildDrawingCache(true);
        Bitmap bitmap = content.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
        String fileName = "CanvasSurface" + System.currentTimeMillis() + ".jpg";
        Log.i("TAGS", "content.getDrawingCache() = " + content.getDrawingCache() + " | bitmap = " + bitmap);
        File file = new File(Environment.getExternalStorageDirectory(), fileName);
        FileOutputStream ostream;
        content.setDrawingCacheEnabled(false);
//        Bitmap bitmap1 = bitmap.copy(Bitmap.Config.ARGB_8888, false);
//        imageView.setImageBitmap(bitmap1);
//        //            check empty bitmap testing only
//        Bitmap emptyBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//
//        if (bitmap.sameAs(emptyBitmap)) {
//            imageView.setVisibility(View.GONE);
//            Log.i("TAGS", "bitmap is empty");
//        } else {
//            imageView.setVisibility(View.VISIBLE);
//            imageView.setImageBitmap(bitmap);
//            Log.i("TAGS", "bitmap is not empty");
//        }

//        content.destroyDrawingCache();

//        imageView.setImageResource(R.drawable.ic_launcher_background);
        try {
            file.createNewFile();
            ostream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
            ostream.flush();
            ostream.close();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
        }
    }

Guidance is very much appreciated 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *