91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

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
健康| 民勤县| 上蔡县| 昌吉市| 达拉特旗| 资兴市| 驻马店市| 安庆市| 涿州市| 敦煌市| 芒康县| 会理县| 枝江市| 台湾省| 当雄县| 金川县| 古田县| 扶沟县| 宝山区| 天津市| 白山市| 潢川县| 陆河县| 平安县| 巴里| 灌南县| 普格县| 察雅县| 富平县| 大姚县| 天等县| 深圳市| 荥阳市| 海林市| 介休市| 买车| 永善县| 营山县| 新野县| 正宁县| 漳平市|