Android Camcorder

Manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.jeanlee.camc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Camc"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape"
            android:label="@string/title_activity_camc" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <SurfaceView 
	        android:id="@+id/id_surface_view"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"/>
	    <Button
	        android:id="@+id/id_btn_open_camera"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_below="@id/id_surface_view"
	        android:text="OpenCam" />	    
	    
	    <Button
	        android:id="@+id/id_btn_start_preview"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_below="@id/id_btn_open_camera"
	        android:text="StartPrv" />	    
	    <Button
	        android:id="@+id/id_btn_stop_preview"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_below="@id/id_btn_open_camera"
	        android:layout_toRightOf="@id/id_btn_start_preview"
	        android:text="StopPrv" />
	    <Button
	        android:id="@+id/id_btn_start_rec"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_below="@id/id_btn_open_camera"
	        android:layout_toRightOf="@id/id_btn_stop_preview"
	        android:text="StartRec" />
	    
	    <Button
	        android:id="@+id/id_btn_stop_rec"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_below="@id/id_btn_open_camera"
	        android:layout_toRightOf="@id/id_btn_start_rec"
	        android:text="StopRec" />

</RelativeLayout>

Jave Code :

package com.android.jeanlee.camc;

import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.hardware.Camera;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class Camc extends Activity {
	
	private static final String TAG = "Camc";
	
	private SurfaceView mSurfaceView;
	private SurfaceHolder mSurfaceHolder;
	private final int LAYOUT_WIDTH = 480;
	private final int LAYOUT_HEIGHT = 320;
	
	private Button mBtnOpenCamera;
	private Button mBtnStartPreview;
	private Button mBtnStopPreview;
	private Button mBtnStartRecord;
	private Button mBtnStopRecord;
	
	private Camera mCamera = null;
	private MediaRecorder mMediaRecorder;
	
	private boolean mBlRecording = false;
	private boolean mBlPreviewing = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //Activity Full screen and no title.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);        

        setContentView(R.layout.activity_camc);  
        
        Log.e(TAG, "onCreate X");
    }

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		 //Implements surface view
        mSurfaceView = (SurfaceView)findViewById(R.id.id_surface_view);
        ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();
        lp.width = LAYOUT_WIDTH;
        lp.height = LAYOUT_HEIGHT;
        mSurfaceView.setLayoutParams(lp);
        mSurfaceView.getHolder().setFixedSize(LAYOUT_WIDTH, LAYOUT_HEIGHT);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        
        mBtnOpenCamera = (Button)findViewById(R.id.id_btn_open_camera);
        mBtnStartPreview = (Button)findViewById(R.id.id_btn_start_preview);
        mBtnStopPreview = (Button)findViewById(R.id.id_btn_stop_preview);
        mBtnStartRecord = (Button)findViewById(R.id.id_btn_start_rec);
        mBtnStopRecord = (Button)findViewById(R.id.id_btn_stop_rec);
        
        mBtnOpenCamera.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.e(TAG, "Camera.open E");

				if(mCamera != null) {
					Log.e(TAG, "Camera already opened, return");
					return;
				}

				mCamera = Camera.open();
				if(mCamera != null){
					Toast.makeText(Camc.this, "Camera opened", Toast.LENGTH_SHORT).show();
				}
				Log.e(TAG, "Camera.open X");
			}
        	
        });
        
        mBtnStartPreview.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mBlPreviewing || mBlRecording) {
					Log.e(TAG, "preview already started or recording, return");
					return;
				}
				
				if(mCamera != null){
					Log.e(TAG, "start preview E");
					try {
						mCamera.setPreviewDisplay(mSurfaceHolder);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					//mCamera.setDisplayOrientation(90);
					mCamera.startPreview();
					mBlPreviewing = true;
					Log.e(TAG, "start preview X");
				}
			}
        	
        });
        
        mBtnStopPreview.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if( !mBlPreviewing || mBlRecording) {
					Log.e(TAG, "preview already stoped or recording, return");
					return;
				}
				
				if(mCamera != null) {
					mCamera.stopPreview();
					mBlPreviewing = false;
				}
			}
        	
        });
        
        mBtnStartRecord.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.e(TAG, "start record E");
				
				if(mBlRecording == true){
					Log.e(TAG, "Aleady recording, return");
					return;
				}
				
				mMediaRecorder = new MediaRecorder();
				
				if(mCamera != null) {
					mCamera.unlock();
					mMediaRecorder.setCamera(mCamera);
				}
				
				mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
				mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
				mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
				//mMediaRecorder.setVideoSize(176, 144);
				mMediaRecorder.setVideoSize(640, 480);
				mMediaRecorder.setVideoFrameRate(30);
				mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
				mMediaRecorder.setOutputFile("/mnt/sdcard/luck.mp4");
				
				try {
					mMediaRecorder.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				mMediaRecorder.start();
				mBlRecording = true;
				
				Log.e(TAG, "start record X");
			}
        	
        });
        
        mBtnStopRecord.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.e(TAG, "stop record E");
				if(mBlRecording == false || mMediaRecorder == null) {
					Log.e(TAG, "Not recording, return");
					return;
				}
				
				mMediaRecorder.stop();
				mMediaRecorder.release();
				mMediaRecorder = null;
				mBlRecording = false;
				mBlPreviewing = true;
				
				Log.e(TAG, "stop record X");
			}
        	
        });
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.e(TAG, "onDestroy");
		if(mCamera != null){
			mCamera.release();
			mCamera = null;
		}
		
		if(mBlRecording == true && mMediaRecorder != null) {
			mMediaRecorder.stop();
		}
		
		if(mMediaRecorder != null){
			mMediaRecorder.release();
			mMediaRecorder=null;
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.e(TAG, "onPause");
		if(mCamera != null){
			mCamera.release();
			mCamera = null;
		}
		
		if(mBlRecording == true && mMediaRecorder != null) {
			mMediaRecorder.stop();
		}
		
		if(mMediaRecorder != null){
			mMediaRecorder.release();
			mMediaRecorder=null;
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值