简体   繁体   中英

Cant make button to switch camera (Back/Front)

I have all my code here where I had another problem. link

Now my problem is that I cant switch the from back to front camera. Is there a clear answer? Maybe give me the code and tell me where to write it?

My button is this:

    Button switchbtn = (Button)findViewById(R.id.switch_camera);
    switchbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    }

What should I do?

Needless to say that I saw like a million answers but nothing solved it.

Try this (based on https://stackoverflow.com/a/17117023 mjosh answer)

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



}

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


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM