亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
永善县| 舒兰市| 威远县| 新田县| 花莲县| 新昌县| 双鸭山市| 兴国县| 宁南县| 屯昌县| 如皋市| 西乡县| 蚌埠市| 湟源县| 中阳县| 普陀区| 云阳县| 葫芦岛市| 潮安县| 呼图壁县| 冀州市| 定兴县| 汾阳市| 伊宁县| 都兰县| 巴塘县| 天等县| 呼玛县| 贺兰县| 广水市| 卢湾区| 普定县| 漯河市| 英超| 白玉县| 阿城市| 乌鲁木齐县| 祁东县| 孟连| 木兰县| 温州市|