0

这是我的整个活动代码: 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);我不确定。请帮我。谢谢。

4

2 回答 2

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()方法中包含这一行。

于 2013-11-25T03:03:09.427 回答
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并循环通过相机,只需更改您的逻辑..

于 2013-11-20T07:07:53.403 回答