中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

android cameraview如何實現拍照和錄像功能

小樊
82
2024-08-17 02:08:44
欄目: 編程語言

要實現拍照和錄像功能,可以使用Android的Camera類和MediaRecorder類來實現。

  1. 首先,在AndroidManifest.xml文件中添加相機和錄像權限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  1. 在布局文件中添加CameraView:
<FrameLayout
    android:id="@+id/camera_preview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</FrameLayout>
  1. 在Activity中初始化Camera和MediaRecorder對象,并在拍照和錄像按鈕的點擊事件中調用相應的方法:
public class CameraActivity extends Activity {

    private Camera mCamera;
    private CameraPreview mPreview;
    private MediaRecorder mMediaRecorder;

    @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);
        
        Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCamera.takePicture(null, null, mPicture);
                }
            }
        );

        Button recordButton = (Button) findViewById(R.id.button_record);
        recordButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isRecording) {
                        stopRecording();
                    } else {
                        startRecording();
                    }
                }
            }
        );
    }

    private Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open();
        }
        catch (Exception e){
            Log.e(TAG, "Failed to open camera: " + e.getMessage());
        }
        return c;
    }

    private void startRecording(){
        mCamera.unlock();

        mMediaRecorder = new MediaRecorder();

        mMediaRecorder.setCamera(mCamera);
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

        mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

        try {
            mMediaRecorder.prepare();
            mMediaRecorder.start();
        } catch (IOException e) {
            Log.e(TAG, "Failed to start recording: " + e.getMessage());
        }
    }

    private void stopRecording(){
        mMediaRecorder.stop();
        mMediaRecorder.reset();
        mMediaRecorder.release();
        mMediaRecorder = null;
        mCamera.lock();
    }

    private PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            //保存拍照圖片的邏輯
        }
    };
}

在以上代碼中,需要自定義CameraPreview類來顯示相機預覽,具體實現可以參考Android官方文檔。同時,需要實現獲取輸出文件路徑的方法getOutputMediaFile()來保存拍照和錄像的文件。

0
竹山县| 瑞金市| 江陵县| 祁阳县| 仁寿县| 永昌县| 土默特右旗| 门源| 大渡口区| 石嘴山市| 香格里拉县| 虞城县| 依兰县| 教育| 农安县| 青神县| 安西县| 扎兰屯市| 竹山县| 陇南市| 和林格尔县| 伊吾县| 祁东县| 永州市| 临武县| 枣阳市| 岱山县| 宁国市| 汪清县| 满城县| 乌兰县| 乐都县| 定结县| 宁德市| 天祝| 莱芜市| 阿巴嘎旗| 永靖县| 怀远县| 沙湾县| 望奎县|