摄像头

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation   ="vertical" android:layout_width="fill_parent"
	android:layout_height ="fill_parent">
	<SurfaceView
		android:id            = "@+id/videoView"
		android:layout_width  = "320px"
		android:layout_height = "240px"
		android:gravity       = "center_horizontal"
	/>
SurfaceView用于显示录制视频时摄像头的画面,尺寸为320*240,样式居中
	<LinearLayout 
		android:orientation      = "horizontal"
		android:layout_width     = "fill_parent" 
		android:layout_height    = "wrap_content"
		android:gravity          = "center_horizontal" 
		android:layout_marginTop = "20dp">
		<Button 
			android:id           ="@+id/recordBtn"
			android:layout_width ="wrap_content" 
			android:layout_height="wrap_content"
			android:text         ="开始录制" />
		<Button 
			android:id           ="@+id/stopBtn" 
			android:layout_width ="wrap_content"
			android:layout_height="wrap_content" 
			android:text         ="停止录制" />
		<Button 
			android:id           ="@+id/deleteBtn" 
			android:layout_width ="wrap_content"
			android:layout_height="wrap_content" 
			android:text         ="删除录像" />
	</LinearLayout>
</LinearLayout>

package com.creative.CameraDemo;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CameraDemoActivity extends Activity 
{
	private File 		  _recordVideoFile;
	private MediaRecorder _vedioRecorder;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        SurfaceView videoView = (SurfaceView)findViewById(R.id.videoView);
        final SurfaceHolder surfaceHolder = videoView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        Button recordBtn = (Button)findViewById(R.id.recordBtn);
		Button stopBtn   = (Button)findViewById(R.id.stopBtn);
		Button deleteBtn = (Button)findViewById(R.id.deleteBtn);
		
		// ��ʼ¼��
		recordBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v)
			{
				if(_vedioRecorder == null)
				{
					_vedioRecorder = new MediaRecorder();
				}
				
				try
				{
					_recordVideoFile = File.createTempFile("video", ".3gp");//在SD卡目录下创建一个视频文件
				} catch (IOException e1)
				{
					e1.printStackTrace();
				}
				
				_vedioRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//设置视频源为摄像头
				_vedioRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//设置输出格式为MPEG4
				
				_vedioRecorder.setVideoSize(320, 240);
				_vedioRecorder.setVideoFrameRate(30);///设置视频帧为每秒30帧
				_vedioRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//设置编码格式为H264
				_vedioRecorder.setOutputFile(_recordVideoFile.getAbsolutePath());//设置输出文件路径
				_vedioRecorder.setPreviewDisplay(surfaceHolder.getSurface());//设置视频显示在SurfaceView上
				
				try
				{
					_vedioRecorder.prepare();
				} catch (IllegalStateException e)
				{
					e.printStackTrace();
				} catch (IOException e)
				{
					e.printStackTrace();
				}
				_vedioRecorder.start();
			}});
		
		// ֹͣ¼��
		stopBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v)
			{
				if(_vedioRecorder != null)
				{
					_vedioRecorder.stop();
					_vedioRecorder.release();
					_vedioRecorder = null;
				}
			}});

		// ɾ��¼��
		deleteBtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v)
			{
				_recordVideoFile.delete();
			}});
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值