Android端末のカメラのプレビューを表示するの続きです。
サンプルプログラム
カメラのプレビューの前面に、半透明の緑色の四角を表示します。FlameLayoutにカメラのプレビュー(cameraView)と四角のプレビュー(rectView)を追加し、それをsetContentViewで表示します。
package sgl.example.camera; import java.io.IOException; import android.app.Activity; import android.graphics.Canvas; import android.graphics.Paint; import android.hardware.Camera; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.widget.FrameLayout; public class CameraSample extends Activity{ private final String TAG = "CameraSample"; private SurfaceView cameraView; private FrameLayout frameLayout; private View rectView; SurfaceHolder holder; Camera camera; private SurfaceHolder.Callback surfaceHolderCallback = new SurfaceHolder.Callback() { public void surfaceCreated(SurfaceHolder holder) { camera = Camera.open(); try { camera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { camera.startPreview(); } public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release(); camera = null; } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); requestWindowFeature(Window.FEATURE_NO_TITLE); frameLayout = new FrameLayout(this); cameraView = new SurfaceView(this); rectView = new View(this) { @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(0x9900ff00); float widthQuarter = cameraView.getWidth() / 4; float heightQuarter = cameraView.getHeight() / 4; canvas.drawRect(widthQuarter , heightQuarter, widthQuarter * 3, heightQuarter * 3, paint); } }; SurfaceHolder holder = cameraView.getHolder(); holder.addCallback(surfaceHolderCallback); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); frameLayout.addView(cameraView); frameLayout.addView(rectView); setContentView(frameLayout); } }
丸、三角、直線等の図形のビューを重ね合わせていけば、それだけで面白そうなアプリが作れそうです。
0 件のコメント:
コメントを投稿