5

我得到了调用另一个具有相机预览的类的活动。问题是它打开了后置摄像头,我需要前置摄像头。我怎么能做到这一点在默认情况下它会打开前置摄像头(我在谷歌中查看但我尝试的每件事都在应用程序打开时崩溃了)。这是活动:

package com.elichai.tfillin;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.FrameLayout;

public class CameraActivity extends Activity {

   private Camera mCamera;
   private CameraPreview mPreview;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       mCamera = getCameraInstance();

       mPreview = new CameraPreview(this, mCamera);
       FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
       preview.addView(mPreview);
   }
   public static Camera getCameraInstance(){
       Camera c = null;
       try {
           c = Camera.open(); 
       }
       catch (Exception e){
       }
       return c; 
   }

这是另一个类:

package com.elichai.tfillin;

import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;
    int cameraId=0;

   @SuppressWarnings("deprecation")
   public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
       mCamera.release();
    }

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    mCamera.setDisplayOrientation(90);
   if (mHolder.getSurface() == null){
      return;
    }

    try {
        mCamera.stopPreview();
    } catch (Exception e){
    }

    try {
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startPreview();

    } catch (Exception e){
    }
}

}

4

2 回答 2

10

您的当前Camera.open()没有参数,这意味着它将打开默认摄像头,该摄像头几乎总是后置摄像头。

您应该遍历可用的摄像头并找出前置摄像头的 ID 并使用它来打开它。就像是:

private Camera openFrontFacingCamera() 
{
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}

然后在您的应用程序中使用它,如下所示:

public static Camera getCameraInstance() {
   Camera c = null;
   try {
       c = openFrontFacingCamera();
   }
   catch (Exception e){
   }
   return c; 
}

看到这两种方法所做的都是打开并返回 Camera 实例,您可以通过直接调用openFrontFacingCamera()而不是getCameraInstance().

于 2013-01-18T12:24:13.980 回答
0

你可以试试这个:

private Camera openFrontFacingCameraGingerbread() 
{
    int Count = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < Count; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}

而且您还需要在清单文件中为相机添加权限。

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
于 2013-01-18T12:25:41.757 回答