package com.camera_test0; import java.io.File; import java.io.IOException; import java.util.List; import android.os.Environment; import android.app.Activity; import android.hardware.Camera; import android.hardware.Camera.Size; import android.media.MediaRecorder; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup.LayoutParams; public class CameraTest0Activity extends Activity { /** Called when the activity is first created. */ private Camera myCamera; private SurfaceView mySurfaceView; private boolean isRecording; private MediaRecorder mediaRecorder; private SurfaceHolder v_holder; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mySurfaceView = (SurfaceView) findViewById(R.id.surfaceView1); SurfaceHolder holder = mySurfaceView.getHolder(); holder.addCallback(mSurfaceListener); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); myCamera = getCameraInstance();// } private SurfaceHolder.Callback mSurfaceListener = new SurfaceHolder.Callback() { public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub try { myCamera.setPreviewDisplay(holder); } catch (Exception e) { e.printStackTrace(); } } public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub //myCamera.release(); //myCamera = null; myCamera.setPreviewCallback(null); myCamera.stopPreview(); myCamera.release(); myCamera = null; } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub v_holder = holder; // SurfaceHolderを保存 myCamera.stopPreview(); Camera.Parameters parameters = myCamera.getParameters(); List<Size> asizeSupport = parameters.getSupportedPreviewSizes(); //一番小さいプレビューサイズを利用 Size size = asizeSupport.get(asizeSupport.size() - 1); parameters.setPreviewSize(size.width,size.height); Log.d("size1", "w=" + String.valueOf(width) + "h=" + String.valueOf(height));// Log.d("size2", "w=" + String.valueOf(size.width) + "h=" + String.valueOf(size.height));// LayoutParams paramLayout; paramLayout = mySurfaceView.getLayoutParams(); paramLayout.width = size.width; paramLayout.height = size.height; mySurfaceView.setLayoutParams(paramLayout); // camera.setDisplayOrientation(90);// カメラを回転 //List<Camera.Size> size = parameters.getSupportedPreviewSizes(); //Log.d("カメラのサイズ", "w=" + String.valueOf(size.get(0).width) + "h=" + String.valueOf(size.get(0).height));// //parameters.setPreviewSize(size.get(0).width, size.get(0).height); // myCamera.setParameters(parameters); myCamera.startPreview();// } }; public void click(View v) { // 録画中でなければ録画を開始 if (!isRecording) { initializeVideoSettings(); // MediaRecorderの設定 // myRecorder.start(); // 録画開始 isRecording = true; // 録画中のフラグを立てる // 録画中であれば録画を停止 } else { mediaRecorder.stop(); // 録画停止 mediaRecorder.reset(); // 無いとmediarecorder went away with unhandled // events が発生 mediaRecorder.release();// mediaRecorder = null; //myCamera.lock(); //myCamera.release(); // release the camera for other applications //myCamera = null; isRecording = false; // 録画中のフラグを外す } } private void initializeVideoSettings() { // TODO 自動生成されたメソッド・スタブ try { //myCamera = getCameraInstance(); mediaRecorder = new MediaRecorder(); //mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); myCamera.unlock(); mediaRecorder.setCamera( myCamera ); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); // 録画の入力ソースを指定 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // ファイルフォーマットを指定 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); // ビデオエンコーダを指定 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); //CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); //mediaRecorder.setProfile(profile); mediaRecorder.setVideoFrameRate(15); // 動画のフレームレートを指定 mediaRecorder.setVideoSize(320, 240); // 動画のサイズを指定 File folder = Environment.getExternalStorageDirectory(); File file; file = File.createTempFile("sample20110603", ".mp4", folder);// //mFile = Environment.getExternalStorageDirectory().getAbsolutePath(); mediaRecorder.setOutputFile(file.getAbsolutePath()); mediaRecorder.setPreviewDisplay(v_holder.getSurface());// mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } private Camera getCameraInstance() { // TODO 自動生成されたメソッド・スタブ Camera c = null; try { c = Camera.open( 1 ); // attempt to get a Camera instance } catch (Exception e){ // Camera is not available (in use or does not exist) } return c; // returns null if camera is unavailable } }
参考サイト:
Androidで簡単なカメラアプリを作る
ソフトウェア技術ドキュメントを勝手に翻訳 c.3 カメラ
0 件のコメント:
コメントを投稿