相机捕获自定义视图Android

问题描述 投票:0回答:1

我正在一起学习Surfaceview和Camera。

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

以下是我研究过的链接:https://inneka.com/programming/android/how-does-androids-setdrawingcacheenabled-work/

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

这是我所需要的;我尝试过,但仍然无法正常工作

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

非常感谢指导:)

android camera android-custom-view surfaceview
1个回答
0
投票

如果您的最低API级别至少为24,则可以使用PixelHolder。根据Android documentation,这是建议的截屏方法。实际上,从API级别28开始,不赞成使用buildDrawingCache方法。

例如:

    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());
© www.soinside.com 2019 - 2024. All rights reserved.