音视频采集

第一步:在功能清单文件AndroidManifest.xml中添加音频刻录和照相机权限:

<uses-permission android:name="android.permission.RECORD_AUDIO"/>

 <uses-permission android:name="android.permission.CAMERA"/>

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

第二步:编写音频刻录代码:

recorder.reset();

recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

recorder.setVideoSize(320, 240);

recorder.setVideoFrameRate(3); //每秒3帧

recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置视频编码方式

recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

recorder.setOutputFile("/mnt/sdcard/itcast.3gp");

recorder.setPreviewDisplay(surfaceView.getHolder().getSurface());

recorder.prepare();//预期准备

recorder.start();//开始刻录

...

recorder.stop();//停止刻录

recorder.release(); //刻录完成一定要释放资源

strings.xml
AndroidManifest.xml
main.xml
VideoRecordActivity
posted @ 2011-06-08 00:30 飞鹰小谭 阅读(68) 评论(0) 编辑

在main.xml布局文件添加用于显示取景画面的SurfaceView 控件:

<SurfaceView android:layout_width="fill_parent" android:layout_height="240dip" android:id="@+id/surfaceView" />

SurfaceView surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);

surfaceView.getHolder().setFixedSize(176, 144);  //设置分辨率

/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/

surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

Camera camera = Camera.open();

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

Display display = wm.getDefaultDisplay();

Camera.Parameters parameters = camera.getParameters();

parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小

parameters.setPreviewFrameRate(3);//每秒3帧

parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式

parameters.set("jpeg-quality", 85);//照片质量

parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小

camera.setParameters(parameters);

camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面

camera.startPreview();//开始预览

camera.autoFocus(null);//自动对焦

camera.takePicture(null, null, null, jpegCallback);//拍照片

camera.stopPreview();//停止预览

camera.release();//释放摄像头

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package
="com.takepic"
android:versionCode
="1"
android:versionName
="1.0">
<uses-sdk android:minSdkVersion="7"/>

<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- 手机屏幕横向显示 -->
<activity android:name=".TakePicActivity"
android:label
="@string/app_name" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

</application>
<!-- 增加摄像头的访问权限 -->
<uses-permission android:name="android.permission.CAMERA"/>
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
main.xml
TakePicActivity
package com.takepic;

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

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;

publicclass TakePicActivity extends Activity {
privatefinalstatic String TAG="TakePicActivity";
private SurfaceView surfaceView;
private Camera camera;
privateboolean preview=false;
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window
= getWindow();
requestWindowFeature(Window.FEATURE_NO_TITLE);
//没有标题
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//高亮
setContentView(R.layout.main);
surfaceView
=(SurfaceView)this.findViewById(R.id.surfaceview);
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(
176, 144); //设置分辨率

}

@Override
publicboolean onKeyDown(int keyCode, KeyEvent event) {
if(camera!=null&&event.getRepeatCount()==0){//按单次
switch (keyCode) {
case KeyEvent.KEYCODE_SEARCH:
camera.autoFocus(
null);//自动对焦
break;
case KeyEvent.KEYCODE_CAMERA://拍照键
case KeyEvent.KEYCODE_DPAD_CENTER://确认键
camera.takePicture(null, null, new TackPicCallBack());
break;

default:
break;
}
}
returntrue;
}

privatefinalclass TackPicCallBack implements PictureCallback{

@Override
publicvoid onPictureTaken(byte[] data, Camera camera) {
FileOutputStream stream
=null;
try {
Bitmap bitmap
=BitmapFactory.decodeByteArray(data, 0, data.length);
File file
=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
stream
=new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG,
100, stream);
//处理完照片继续预览
camera.stopPreview();
camera.startPreview();
}
catch (Exception e) {
Log.i(TAG, e.getMessage());
}
finally{
if(stream!=null){
try {
stream.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}

privatefinalclass SurfaceViewCallBack implements Callback{

@Override
publicvoid surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}

@Override
publicvoid surfaceCreated(SurfaceHolder holder) {
try {
camera
=Camera.open();
Camera.Parameters parameters
= camera.getParameters();
WindowManager wm
= (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display
= wm.getDefaultDisplay();
parameters.setPreviewSize(display.getWidth(), display.getHeight());
//设置预览照片的大小
parameters.setPreviewFrameRate(3);//每秒3帧
parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式
parameters.set("jpeg-quality", 85);//照片质量 最高质量可以设置为100
parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小 如果要拍的大就设置大一些
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceView.getHolder());
//通过SurfaceView显示取景画面
camera.startPreview();//开始预览
preview =true;
}
catch (Exception e) {
Log.i(TAG, e.getMessage());
}
//preview = true;

}

@Override
publicvoid surfaceDestroyed(SurfaceHolder holder) {
if(camera!=null){
if(preview){
camera.stopPreview();
//停止预览
}
camera.release();
}

}

}
}
posted @ 2011-11-13 18:59  OYK  阅读(612)  评论(0编辑  收藏  举报