3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

カメラの切り替え

Last updated at Posted at 2015-03-24

SurfaceViewでカメラのプレビューができている前提です。

なにも指定せずに
camera.open();
とするとデフォルトのカメラ(たいていは背面だと思います)が有効になり、プレビューが開始されます。
自撮りしたいときなどに使う液晶面のフロントカメラに切り替えたい時に、SurfaceViewを継承したクラスで以下のようなメソッドを作っておいて外部から呼び出すようにしました。

public void switchCamera() {
    camera.stopPreview();
    camera.release();

    if(currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK){
        currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT;
    }else {
        currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
    }

    camera = Camera.open(currentCameraId);
    //プレビューを縦向きにしたいので回転。
    camera.setDisplayOrientation(90);

    try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        if(camera != null) {
            camera.release();
            camera = null;
        }
        Log.e(TAG,e.getMessage());
    }

    camera.startPreview();
}

一旦プレビューを止めてからカメラを再度オープンすればOKです。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?