Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
摄像头捕获自定义视图Android_Android_Camera_Android Custom View_Surfaceview - Fatal编程技术网

摄像头捕获自定义视图Android

摄像头捕获自定义视图Android,android,camera,android-custom-view,surfaceview,Android,Camera,Android Custom View,Surfaceview,我正在一起学习Surfaceview和Camera 我似乎无法捕获我的CustomSurfaceView,下面是我的OnCreate代码以及我如何执行CustomSurfaceView的捕获 以下是我研究过的链接: 这就是我所理解的需要;我试过了,但还是不行 //customSurfaceView.setVisibility(View.VISIBLE) //customSurfaceView.setDrawingCacheEnabled(true) //customSurfaceView.b

我正在一起学习Surfaceview和Camera

我似乎无法捕获我的CustomSurfaceView,下面是我的OnCreate代码以及我如何执行CustomSurfaceView的捕获

以下是我研究过的链接:

这就是我所理解的需要;我试过了,但还是不行

//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();
        }
    }

非常感谢您的指导:)

如果您的最低API级别至少为24,您可以使用像素保持器。根据,这是屏幕截图的推荐方法。实际上,buildDrawingCache方法在API级别28时就已经被弃用了

例如:

    SurfaceHolder holder = customSurfaceView.getHolder();

    Bitmap bitmap = Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888);
    PixelCopy.request(holder.getSurface(), bitmap, (copyResult -> {
        //success/failure handler
    }), new Handler());

如果最低API级别至少为24,则可以使用像素保持器。根据,这是屏幕截图的推荐方法。实际上,buildDrawingCache方法在API级别28时就已经被弃用了

例如:

    SurfaceHolder holder = customSurfaceView.getHolder();

    Bitmap bitmap = Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888);
    PixelCopy.request(holder.getSurface(), bitmap, (copyResult -> {
        //success/failure handler
    }), new Handler());