我的所有代码都在这里,我有另一个问题。 link
现在我的问题是我无法从后置摄像头切换到前置摄像头。 有明确的答案吗?也许给我代码并告诉我在哪里写它?
我的按钮是这样的:
Button switchbtn = (Button)findViewById(R.id.switch_camera);
switchbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
}
我该怎么办?
毋庸置疑,我看到了一百万个答案,但没有解决它。
答案 0 :(得分:0)
试试这个(基于https://stackoverflow.com/a/17117023 mjosh回答)
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
boolean isPreviewRunning = true;
public void switchCamera(int currentCameraId)
{
//here write code for switch the camera
if (isPreviewRunning) {
camera.stopPreview();
}
//NB: if you don't release the current camera before switching, you app will crash
camera.release();
//swap the id of the camera to be used
if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
}
else {
currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
}
camera = Camera.open(currentCameraId);
setCameraDisplayOrientation(CameraActivity.this, currentCameraId, camera);
try {
camera.setPreviewDisplay(previewHolder);
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mHolder.setFixedSize(100, 100);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the
// preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException e) {
Log.d("DG_DEBUG", "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height) {
if (isPreviewRunning){
return;
}
isPreviewRunning = true;
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null) {
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
// make any resize, rotate or reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d("DG_DEBUG", "Error starting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
}
这是您的活动
public class YOURACTIVITY extends Activity
{
int currentCameraId; // it will be intialized while you opening a camera and pass it to switch camera
Button switchbtn;
CameraPreview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourxml);
createCamera();
switchbtn = (Button)findViewById(R.id.switch_camera);
switchbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
mPreview.switchCamera(currentCameraId);
}
}
}
private void createCamera() {
// Create an instance of Camera
mCamera = getCameraInstance(); //here you have open the camera. here intialize the currentcamerid
// Setting the right parameters in the camera
Camera.Parameters params = mCamera.getParameters();
params.setRotation(90);
mCamera.setParameters(params);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview, 0);
}
}