0

I have integrated [CWAC-CAMERA][1].I am trying to switch between back and front camera by clicking on the button using the below code but its not working.I can see only the default back camera.Where am I going wrong?

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

Updated according to CommonsWare answer-

 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
  • You need to do something with the `Builder` after you configure it, and there is no code in the above listing that does anything with the `Builder` other than call `useFrontFacingCamera()`. – CommonsWare Jun 04 '15 at 14:47
  • @CommonsWare checkk now the code in question..i have added few lines which i was using..what exactly you want me to do wid the builder? –  Jun 04 '15 at 14:50

1 Answers1

0

You need to call setHost() after fully configuring the Builder. You cannot just change the Builder later on and expect a change.

Note that setHost() is designed to be called once per fragment. If you want to switch between cameras, you need to switch fragments (one per camera), as the demo app does.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • see in updated question above which i updated after your comment..i am using `f.setHost(builder.useFullBleedPreview(true).build());` –  Jun 04 '15 at 14:53
  • @Bhuvi: Yes, but you have not called `useFrontFacingCamera()` yet. You are calling `useFrontFacingCamera()` sometime after `setHost()`, when the view is clicked. You have to call `useFrontFacingCamera()` **before** `setHost()`. – CommonsWare Jun 04 '15 at 14:55
  • u mean after `builder.useFrontFacingCamera(true);` i should call `f.setHost(builder.useFullBleedPreview(true).build());` –  Jun 04 '15 at 14:56
  • @Bhuvi: No, because you cannot change hosts. As I wrote in my answer, if you want to switch between cameras, you need to switch fragments (one per camera), as the demo app does. – CommonsWare Jun 04 '15 at 14:59
  • i am doing this in activity –  Jun 04 '15 at 15:00
  • @Bhuvi: I do not know what "this" is. But you have `f = new CameraFragment();`. `CameraFragment` is a fragment. If you want to switch between cameras, you need to switch fragments (one per camera), as the demo app does. – CommonsWare Jun 04 '15 at 15:02
  • @Bhuvi: The demo app is available in the GitHub repo: https://github.com/commonsguy/cwac-camera, in a directory named `demo/`. – CommonsWare Jun 04 '15 at 15:04
  • please check the above question code updated according to ur answer..i am still seeing the back camera only..thanks.. –  Jun 04 '15 at 15:11
  • @Bhuvi: As I have written several times, **you have to call `useFrontFacingCamera()` before `setHost()`**. As part of setting up `f` and `f2` originally, call `useFrontFacingCamera()` on each fragment's `Builder`. Then, on the button click, just do the `replace()` transaction. – CommonsWare Jun 04 '15 at 15:17
  • i updated according to ur comment..i still seeing back camera only..please check updated code in question..thanks.. –  Jun 04 '15 at 15:24
  • in that demo code i don't see any where coding for switching front-back camera..please provide me link..thanks.. –  Jun 04 '15 at 15:36
  • @Bhuvi: https://github.com/commonsguy/cwac-camera/blob/master/demo/src/com/commonsware/cwac/camera/demo/MainActivity.java#L102-L130 – CommonsWare Jun 04 '15 at 15:45
  • okay ..i checked that activity.in that u r creating three instance of DemoCameraFragment std,ffc,current and assigning the fragments std or ffc to current depening upon requirement.But i don't see u have set host or created a builder..i willl be grateful to u if u can check in question and let me know where am I going wrong.. –  Jun 05 '15 at 04:33
  • @Bhuvi: `setHost()` is called [in `onCreate()` of `DemoCameraFragment`](https://github.com/commonsguy/cwac-camera/blob/master/demo/src/com/commonsware/cwac/camera/demo/DemoCameraFragment.java#L76). Feel free to learn how to use tools like **`grep`**, or the "find" function in your IDE, or the search field in GitHub, to search code for certain strings like "setHost" in a set of source files. – CommonsWare Jun 05 '15 at 11:10