1

I'm trying to create a switch camera button with my custom gui. In my CustomCam extends SherlockFragmentActivity I have a method called onSwitch() that gets called from the xml android:onClick="onSwitch"

Here is the method:

public void onClickSwitchButton(View view) {
        if (current == std) {
            ffc = CustomCamFragment.newInstance(true);
            current = ffc;
            getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, current).commit();
            isFrontCamera=true;

            return;

        }


        if (current == ffc) {
            std = CustomCamFragment.newInstance(false);
            current = std;
            isFrontCamera=false;
        }

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, current).commit();
    }

I know that the method triggers because of breakpoints and log statements, but my screen simply goes black, and then comes back as the same std view.

Note: I realize I have to detect if the phone has more than 1 camera, or no cameras at all. But for now, this app is not something I'll be publishing. Just want it for my own personal use.

My CustomCamFragment:

public class CustomCamFragment extends CameraFragment {

private static final String KEY_USE_FFC = "com.commonsware.cwac.camera.demo.USE_FFC";

public static CustomCamFragment newInstance(boolean useFFC) {
    CustomCamFragment f = new CustomCamFragment();
    Bundle args = new Bundle();

    args.putBoolean(KEY_USE_FFC, useFFC);
    f.setArguments(args);
    return (f);
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • "but my screen simply goes black, and then comes back as the same std view" -- I have no idea what you mean by this, sorry. – CommonsWare Feb 20 '14 at 19:22
  • It seems like it attempts to make the switch to ffc (temporarily goes black), but it comes right back with a video feed from the std camera. –  Feb 20 '14 at 19:24

3 Answers3

0

Well, I have no idea what CustomCamFragment is. Your code, though, does not seem to actually switch to another camera. You are creating the CustomCamFragment via its constructor and not telling it what camera to use. You will notice that is not how the demo app works, where I use a factory method and pass in what camera to use (DemoCameraFragment.newInstance(false) for the rear-facing camera, DemoCameraFragment.newInstance(true) for the front-facing camera).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I included my CustomCamFragment code above. It should be identical to yours. I'm not sure why it's not working. Could you take a look? I'm using the Demo-v9 as a example, but still don't understand what could be causing it not to switch. –  Feb 20 '14 at 19:36
  • @user2676468: One issue, probably unrelated, is that you are making new `CustomCamFragment` instances each time. Only create one per camera, reusing an existing instance if you have it. Beyond that, are you using the value from your arguments `Bundle`? In the demo app, that's in `useFrontFacingCamera()` on the `DemoCameraHost` inner class of `DemoCameraFragment`. – CommonsWare Feb 20 '14 at 19:43
  • Hmm... I'm using my own camera host class. `public class CustomCamHost extends SimpleCameraHost` and it's not using the `useFrontFacingCamera()` method. Now how should I pass that class a true or false value? –  Feb 20 '14 at 19:51
  • @user2676468: Read it out of that arguments `Bundle` (`getArguments().getBoolean(KEY_USE_FFC)`). – CommonsWare Feb 20 '14 at 19:53
  • I don't get a bundle in my CustomCamHost class since all it does is extend SimpleCameraHost. I guess maybe I should just ditch my CustomCamHost.java file and try to integrate it into my CustomCamFragment as an inner class? –  Feb 20 '14 at 19:58
  • @user2676468: Either that, or create a new `CustomCamHost` instance per fragment, passing in the FFC flag to the `CustomCamHost` instance (constructor, setter, whatever). – CommonsWare Feb 20 '14 at 20:08
  • Hmm... it seems I need to implement CameraHostProvider in activity. How do I specify that I want to use the one in my fragment? –  Feb 20 '14 at 20:27
0

To answer your last comment about embedding in fragments: I have disabled the throw new IllegalArgumentException(...) from the CameraView constructor in the library and are calling setHost myself from my custom CameraFragment in onCreateView. This way the CameraView's context doesn't have to be a CameraHostProvider and you can host the view inside a fragment.

mvandillen
  • 904
  • 10
  • 17
0

I want to switch the front/back camera without pushing a new fragment. After studying the code of CameraView and CameraHost (SimpleCameraHost), I think the best solution is to just remove the current CameraView instance and add a new one:

public class MyCameraFragment extends CameraFragment {

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    addCameraView(view);
}

private void addCameraView(View view) {
    FrameLayout frame = (FrameLayout)v.findViewById(R.id.cameraFrame);
    frame.removeAllViews();
    cameraView = new CameraView(getActivity());
    cameraView.setHost(cameraHost = new MyCameraHost(getActivity()));
    setCameraView(cameraView);
    frame.addView(cameraView);
}

private void doSwitchCamera() {
    // do some change to the settings.
    useFrontFacingCamera = !useFrontFacingCamera;
    if (null != cameraView) {
        cameraView.onPause();
    }
    addCameraView(getView());
 cameraView.onResume();
}

}

I hope this may helps someone who want to keep their fragment untouched / simple code.

John Pang
  • 2,403
  • 25
  • 25