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 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.