切换到前置摄像头不起作用

时间:2013-11-20 06:53:46

标签: android camera front-camera

这是我的整个活动代码:     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;
    }

}

每当我调用openFrontFacingCamera()方法时,每个条件似乎都能正常工作,我可以在Toast上看到它,但相机本身没有切换。我认为这条线camera = Camera.open(camId);我不确定是否有遗漏或错误。请帮我。感谢。

2 个答案:

答案 0 :(得分:1)

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) {}
        }
    }

我得到了解决方案,我忘了在camera.release();方法中添加此行openFrontFacingCamera()

答案 1 :(得分:0)

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

你必须使用相机信息获取相机ID并通过相机循环,只需改变你的逻辑..