カメラのビュークラス 2

クラスの記述

	/*
	 * (non-Javadoc) サーフェイス生成イベントの処理
	 * 
	 * @see android.view.SurfaceHolder.Callback#surfaceCreated(android.view.SurfaceHolder)
	 */
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		CameraInformation info = new CameraInformation();
		try {
			// カメラの初期化
			if (null == camera)
				camera = info.openCamera(cameraFacing);
			// カメラにサーフェイスホルダーを設定
			if (null != camera)
				camera.setPreviewDisplay(holder);
		} catch (Exception e) {
			Log.e(LOG_TAG, "Camera not open. " + e.getMessage());
		}
	}

	/*
	 * (non-Javadoc) サーフェイス変更イベントの処理
	 * 
	 * @see android.view.SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder, int, int, int)
	 */
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		CameraInformation info = new CameraInformation();
		Size previewSize = null;
		Size pictureSize = null;
		Log.d(LOG_TAG, "DefaultDisplay Rotation=" + getDefaultDisplayRotation());
		Log.d(LOG_TAG, "format=" + format + " width=" + width + " height=" + height);
		try {
			if (null != camera) {
				// プレビューの停止
				camera.stopPreview();
				// ローテーションを設定
				info.setRotation(getDefaultDisplayRotation());
				// カメラの設定値を取得
				Camera.Parameters params = camera.getParameters();
				// プレビューサイズを選択
				previewSize = info.getPreviewSize(camera, width, height);
				// プレビューのサイズを設定
				params.setPreviewSize(previewSize.width, previewSize.height);
				// 画像サイズを取得
				pictureSize = info.getPictureSize(camera, this.pictureSize);
				// 画像のサイズを設定
				params.setPictureSize(pictureSize.width, pictureSize.height);
				// カメラへ設定値を適用
				camera.setParameters(params);
				// プレビューの開始
				camera.startPreview();

				Log.d(LOG_TAG, "preview width=" + previewSize.width + " height=" + previewSize.height);
				Log.d(LOG_TAG, "picture width=" + pictureSize.width + " height=" + pictureSize.height);
			}
		} catch (Exception e) {
			Log.e(LOG_TAG, "Can not preview. " + e.getMessage());
		}
	}

	/*
	 * (non-Javadoc) サーフェイス解放イベントの処理
	 * 
	 * @see android.view.SurfaceHolder.Callback#surfaceDestroyed(android.view.SurfaceHolder)
	 */
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if (null != camera) {
			// プレビュー終了
			camera.setPreviewCallback(null);
			camera.stopPreview();
			camera.release();
		}
		camera = null;
	}

今回は、ビューを生成、表示、終了までです。

1、surfaceCreated(SurfaceHolder holder)
カメラを初期化して、カメラにサーフェイスホルダーをぶっこみます。

2、surfaceChanged(SurfaceHolder holder, int format, int width, int height)
ビューが生成されたもしくは変更されたときに、カメラのプレビュー機能を使用してビューに表示します。

3、surfaceDestroyed(SurfaceHolder holder)
ビューが削除されるときに呼び出されるので、カメラを使用していたら解放してあげます。
camera.release() これを忘れるとメモリリークするらしい、検証していないので定かではないが、まぁ画像処理ではメモリを大量に使って高速に処理しなければならないからな。

さて、機種に依存するため、詳細な機能を実装するためにはそれらをラップしていきます。
手始めに、CameraInformation こいつでカメラの設定情報を取得したり変更したりします。
それと外部からの要件を満たすために、cameraFacing と pictureSize を実装します。

プレビューサイズは? と疑問をもつ?
surfaceChanged() で表示するサーフェイスホルダーとフォーマット、幅と高さをわたされるでしょ!
そこで処理するので外部からの要件は満たしています。

前回に、「おちているソースや参考書などが読みにくいと感じた」と書いたのことがなんとなくわかった気がする。
要件と機能が明確に記述されていないからだ!
また、おいおいそれはそのクラスに必要ない機能だろうと思われるのに、そのクラスで実装している。
オブジェクト指向の前に、要件と機能を明確にしろよと、遠いむかしに部下へよく言ったもんだ・・・

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>