0

这里 externaltexture 为空。我正在尝试在 sceneform 场景视图中的 3D 对象上添加相机预览。请帮我在对象(立方体)上渲染相机预览。很抱歉这个问题,因为我是 Sceneform 的初学者。在此先感谢。谁能帮我解决这个问题。我需要在场景视图中的 3D 对象上渲染相机预览

private static final String TAG = ChromaKeyVideoActivity.class.getSimpleName();
  private static final double MIN_OPENGL_VERSION = 3.0;
  private static final Color CHROMA_KEY_COLOR = new Color(0.1843f, 1.0f, 0.098f);
  private static final float VIDEO_HEIGHT_METERS = 0.85f;
  ExternalTexture texture, camTexture;
  SceneView sceneView;
  @Nullable private ModelRenderable videoRenderable;
  private MediaPlayer mediaPlayer;

    private TextureView myTexture;
    private Camera mCamera;

  @Override
  @SuppressWarnings({"AndroidApiChecker", "FutureReturnValueIgnored"})
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!checkIsSupportedDeviceOrFinish(this)) {
      return;
    }

    setContentView(R.layout.activity_video);
    sceneView = findViewById(R.id.sceneView);
    //playVideo();
    camPreview();


  }

    private void camPreview() {
        myTexture = new TextureView(this);
        myTexture.setSurfaceTextureListener(this);
        camTexture = new ExternalTexture();

        myTexture.setSurfaceTexture(camTexture.getSurfaceTexture());
        ModelRenderable.builder()
                .setSource(this, R.raw.chroma_key_video)
                .build()
                .thenAccept(
                        renderable -> {
                            videoRenderable = renderable;
                            videoRenderable.getMaterial().setExternalTexture("cameraTexture", camTexture);
                        })
                .exceptionally(
                        throwable -> {
                            Toast toast =
                                    Toast.makeText(this, "Unable to load video renderable", Toast.LENGTH_LONG);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();
                            return null;
                        });


        Node cakeNode = new Node();
        cakeNode.setParent(sceneView.getScene());
        cakeNode.setLocalPosition(new Vector3(0f, 0f, -1f));


        camTexture.getSurfaceTexture().setOnFrameAvailableListener(surfaceTexture -> {
            cakeNode.setRenderable(videoRenderable);
            camTexture.getSurfaceTexture().setOnFrameAvailableListener(null);
        });



        cakeNode.setLocalScale(
                new Vector3(
                        VIDEO_HEIGHT_METERS * (VIDEO_HEIGHT_METERS / VIDEO_HEIGHT_METERS), VIDEO_HEIGHT_METERS, 1.0f));
        sceneView.getScene().addChild(cakeNode);

    }
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        return true;
    }

    @SuppressLint("NewApi")
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1, int arg2) {
        mCamera = Camera.open();
        Camera.Size previewSize = mCamera.getParameters().getPreviewSize();

        myTexture.setLayoutParams(new FrameLayout.LayoutParams(
                previewSize.width, previewSize.height, Gravity.CENTER));

        try {
            mCamera.setPreviewTexture(arg0);
        } catch (IOException t) {
        }

        mCamera.startPreview();
        myTexture.setAlpha(1.0f);
        myTexture.setRotation(90.0f);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
                                            int arg2) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
        // TODO Auto-generated method stub
    }
}
4

0 回答 0