Question

Here is my whole activity code : import java.io.IOException;

import pickld.android.intents.R;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class CameraActivity extends Activity implements SurfaceHolder.Callback, OnClickListener {

    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean previewing = false;

    int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
    int numberOfCamera;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.camera);

        surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        Button cancel = (Button)findViewById(R.id.cancel);
        Button openGallery = (Button)findViewById(R.id.openGallery);
        Button takePhoto = (Button)findViewById(R.id.capture);
        Button switchCam = (Button)findViewById(R.id.flip);
        Button toggleFlash = (Button)findViewById(R.id.toggleFlash);

        cancel.setOnClickListener(this);
        openGallery.setOnClickListener(this);
        takePhoto.setOnClickListener(this);
        switchCam.setOnClickListener(this);
        toggleFlash.setOnClickListener(this);

    }

    public void openFrontFacingCamera() {
        numberOfCamera = Camera.getNumberOfCameras();
        if(camId == Camera.CameraInfo.CAMERA_FACING_BACK){
            camId = Camera.CameraInfo.CAMERA_FACING_FRONT;
            Toast.makeText(getApplicationContext(), "BACK TO FRONT" ,
1000).show();
                try {
                    camera = Camera.open(camId);
                    camera.setPreviewDisplay(surfaceHolder);
                    camera.startPreview();
                    previewing = true;
                } catch (RuntimeException e) {

            } catch (IOException e) {}
        }else if(camId == Camera.CameraInfo.CAMERA_FACING_FRONT){
            camId = Camera.CameraInfo.CAMERA_FACING_BACK;
            Toast.makeText(getApplicationContext(), "FRONT TO BACK" , 



 1000).show();
                    try {
                        camera = Camera.open(camId);
                        camera.setPreviewDisplay(surfaceHolder);
                        camera.startPreview();
                    } catch (RuntimeException e) {

            } catch (IOException e) {}
        }
    }

    @Override
    public void onClick(View click) {
        if(click.getId() == R.id.cancel){
            this.finish();
        }else if(click.getId() == R.id.flip){
            openFrontFacingCamera();
        }
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if(previewing){
            camera.stopPreview();
            previewing = false;
        }
        if (camera != null){
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open(camId);
        camera.setDisplayOrientation(90);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }

}

Everytime i call the openFrontFacingCamera() method, every conditions seems to be working fine i can see it on the Toast i put there, but the camera itself is not switching. I think there must something missing or wrong with this line camera = Camera.open(camId); im not sure. Please help me. Thanks.

Was it helpful?

Solution

public void openFrontFacingCamera() {
        numberOfCamera = Camera.getNumberOfCameras();
        if(camId == Camera.CameraInfo.CAMERA_FACING_BACK){
            camId = Camera.CameraInfo.CAMERA_FACING_FRONT;
            Toast.makeText(getApplicationContext(), "BACK TO FRONT" ,
1000).show();
                try {
                    camera.release();
                    camera = Camera.open(camId);
                    camera.setPreviewDisplay(surfaceHolder);
                    camera.startPreview();
                    previewing = true;
                } catch (RuntimeException e) {

            } catch (IOException e) {}
        }else if(camId == Camera.CameraInfo.CAMERA_FACING_FRONT){
            camId = Camera.CameraInfo.CAMERA_FACING_BACK;
            Toast.makeText(getApplicationContext(), "FRONT TO BACK" , 



 1000).show();
                    try {
                        camera.release();
                        camera = Camera.open(camId);
                        camera.setPreviewDisplay(surfaceHolder);
                        camera.startPreview();
                    } catch (RuntimeException e) {

            } catch (IOException e) {}
        }
    }

I got the solution, i forgot to include this line camera.release(); in the openFrontFacingCamera() method.

OTHER TIPS

private int findFirstFrontFacingCamera() {
        int foundId = -1;
        // find the first front facing camera
        int numCams = Camera.getNumberOfCameras();
        for (int camId = 0; camId < numCams; camId++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(camId, info);
            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                Log.d(DEBUG_TAG, "Found front facing camera");
                foundId = camId;
                break;
            }else{
                foundId = 0;
              }
        }
        return foundId;
    }

you have to get camera id using camera info and loop through camera's , just change your logic..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top