CWAC CAMERA切换前后摄像头不工作

时间:2015-06-04 14:42:50

标签: android commonsware-cwac cwac-camera

我已经集成了[CWAC-CAMERA] [1]。我试图通过使用下面的代码点击按钮来切换后置摄像头和前置摄像头,但它无法工作。我只能看到默认的后置摄像头。我在哪里我错了?

     private boolean isBackCam=true;
     f = new CameraFragment();
        builder=new SimpleCameraHost.Builder(new DemoCameraHost(getApplicationContext()));
f.setHost(builder.useFullBleedPreview(true).build());
         handleSwitchCamera=(ImageButton)findViewById(R.id.handleSwitchCamera);
          handleSwitchCamera.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view) 
                {


                    if(isBackCam) 
                        {
                            isBackCam=false;
                            builder.useFrontFacingCamera(true);
                        }
                        else
                        {
                            isBackCam=true;
                            builder.useFrontFacingCamera(false);
                        }
                    }
                });

根据CommonsWare的答案更新 -

 f = new CameraFragment();
        getFragmentManager().beginTransaction()
                .add(R.id.preview_view, f, TAG_CAMERA_FRAGMENT)
                .commit();
        f2 = new CameraFragment();     
builder=new SimpleCameraHost.Builder(new DemoCameraHost(getApplicationContext()));
        builder2=new SimpleCameraHost.Builder(new DemoCameraHost(getApplicationContext()));
        builder.useFrontFacingCamera(false);
        builder2.useFrontFacingCamera(true);
        f.setHost(builder.useFullBleedPreview(true).build());
        f2.setHost(builder2.useFullBleedPreview(true).build());
handleSwitchCamera.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view) {
            if(isBackCam) {
                Log.e("bhuvnesh","backcam");
                isBackCam=false;


                getFragmentManager().beginTransaction()
                        .replace(R.id.preview_view, f2, TAG_CAMERA_FRAGMENT)
                        .commit();
            }
            else
            {
                Log.e("bhuvnesh","frontcam");
                isBackCam=true;


                getFragmentManager().beginTransaction()
                        .replace(R.id.preview_view, f, TAG_CAMERA_FRAGMENT)
                        .commit();
            }
        }
    });
  [1]: https://github.com/commonsguy/cwac-camera

1 个答案:

答案 0 :(得分:0)

在完全配置setHost()后,您需要致电Builder 。您不能稍后更改Builder并期望更改。

请注意,setHost()旨在为每个片段调用一次。如果你想在摄像机之间切换,你需要切换片段(每个摄像机一个),就像演示应用程序一样。