0

这是我的代码:

package eye.sight.test;

import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class AndroidVideoCapture extends Activity implements SurfaceHolder.Callback{

    Button myButton;
    MediaRecorder mediaRecorder;
    SurfaceHolder surfaceHolder;
    boolean recording;
    Camera c;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        recording = false;
        setContentView(R.layout.main1);
        initMediaRecorder();

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

        myButton = (Button)findViewById(R.id.mybutton);
        myButton.setOnClickListener(myButtonOnClickListener);
    }

    private Button.OnClickListener myButtonOnClickListener
    = new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(recording){
                mediaRecorder.stop();
                mediaRecorder.release();
                finish();
            }else{
                mediaRecorder.start();
                recording = true;
                myButton.setText("STOP");
            }
        }};

        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }
        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
            prepareMediaRecorder();
        }
        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub

        }

        private void initMediaRecorder(){

            mediaRecorder = new MediaRecorder();
        //  c.unlock();
        //  c = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);

    //      mediaRecorder.setCamera(c);


            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
            mediaRecorder.setProfile(camcorderProfile_HQ);
            mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
            mediaRecorder.setMaxDuration(60000);
            //mediaRecorder.setMaxFileSize(5000000);
        }

        private void prepareMediaRecorder(){
            mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
            try {
                mediaRecorder.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}

我一直在谷歌上搜索如何使用前置摄像头,但没有任何运气。我在这里想念什么?默认情况下它工作得很好,但我不确定何时以及如何使用 Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); 和 Came.unlock();。

谢谢

4

1 回答 1

0

尝试这样做

private Camera FrontCamera() 
{
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" />
于 2012-11-03T13:07:24.163 回答