CWAC CAMERA-切换前后摄像头不工作
CWAC CAMERA-switching Front-Back camera not working
我已经集成了 [CWAC-CAMERA][1]。我正在尝试通过使用以下代码单击按钮在后置和前置摄像头之间切换,但它 working.I 只能看到默认后置camera.Where我错了吗?
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
您需要在 完全配置 Builder
之后调用 setHost()
。您不能稍后更改 Builder
并期待更改。
请注意,setHost()
设计为每个片段调用一次。如果你想在相机之间切换,你需要像演示应用程序那样切换片段(每个相机一个)。
我已经集成了 [CWAC-CAMERA][1]。我正在尝试通过使用以下代码单击按钮在后置和前置摄像头之间切换,但它 working.I 只能看到默认后置camera.Where我错了吗?
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
您需要在 完全配置 Builder
之后调用 setHost()
。您不能稍后更改 Builder
并期待更改。
请注意,setHost()
设计为每个片段调用一次。如果你想在相机之间切换,你需要像演示应用程序那样切换片段(每个相机一个)。