如何在Android 2.3(Ginger)上使用API​​前置摄像头?

时间:2011-11-04 10:37:31

标签: android camera android-camera

我在这个网站上看到了如何使用API​​,但我不知道代码的位置在哪里。请帮帮我......

代码:

package com.androidhuman.example.CameraPreview;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class CameraPreview extends Activity {    
    private Preview mPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Create our Preview view and set it as the content of our activity.
        mPreview = new Preview(this);
        setContentView(mPreview);
    }

}

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;

    Preview(Context context) {
        super(context);

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



    public void surfaceCreated(SurfaceHolder holder) {  **//The code that I've changed**
        int cameraCount = 0; 
        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 { 
                    mCamera = Camera.open( camIdx ); 
                } catch (RuntimeException e) { 
                    Log.i("Camera failed to open: ",e.getLocalizedMessage()); 
                } 
            } 
        } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

}

我该如何解决这个问题?

我见过的问题

How to capture photo automatically in android phone?

1 个答案:

答案 0 :(得分:2)

private Camera openFrontFacingCameraGingerbread() 
{
    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;
}

确保在清单中声明以下内容:

<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" />