相机拍照、视频录制相机拍照
全屏显示有两种方式:
在androidManifest.xml里面的application标签下设置
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"全屏显示
Java代码
//指定Activity没有标题(一定要在setContentView之前调用)
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
设置横屏
在androidManifest.xml里面的activity标签下设置
android:screenOrientation="landscape"横屏显示
相机代码
/** surfaceview的创建是在Activity完全显示之后,才创建
* 只能使用回调监听
* */
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//指定Activity没有标题(一定要在setContentView之前调用)
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
surfaceview = (SurfaceView) findViewById(R.id.surfaceview);
// 通过holder来设置surfaceview的参数
SurfaceHolder holder = surfaceview.getHolder();
// 设置显示固定大小
holder.setFixedSize(176,144);
// 不要让SurfaceView 缓存数据,直接输出
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//保持屏幕的高亮
surfaceview.setKeepScreenOn(true);
//添加Surfaceview的创建监听
holder.addCallback(newMyCallback());
}
privateclassMyCallbackimplementsCallback{
@Override
publicvoidsurfaceCreated(SurfaceHolderholder) {
//TODO Auto-generated method stub
try{
//得到相机对象
camera = Camera.open();
//相机的参数
Parameters parameters =camera.getParameters();
Log.i("i",parameters.flatten());
//设置预览显示
camera.setPreviewDisplay(holder);
//开始预览
camera.startPreview();
}catch(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intwidth,
intheight) {
//TODO Auto-generated method stub
}
@Override
publicvoidsurfaceDestroyed(SurfaceHolder holder) {
//TODO Auto-generated method stub
if(camera!=null){
camera.release();
camera= null;
}
}
}
//对焦都是由硬件直接完成
publicvoidfoucs(View v){
camera.autoFocus(null);
}
publicvoidtake(View v){
/**
*shutter,快门
*raw, 相机捕获的原始数据
*jpeg 相机处理后的数据
*/
camera.takePicture(null,null, newMyPictureCallback());
}
privateclassMyPictureCallbackimplementsPictureCallback{
@Override
publicvoidonPictureTaken(byte[] data,Camera camera) {
//TODO Auto-generated method stub
try{
File file = newFile(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
FileOutputStream fos = newFileOutputStream(file);
fos.write(data);
fos.close();
//处理照片的时候,相机被占用了。要想重新预览,就开始预览
camera.startPreview();
}catch(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protectedvoidonDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
所需权限:
视频录制
publicclassMainActivityextendsActivity{
privateSurfaceView surfaceview;
privateMediaRecorder mr;
privateButton bt_start;
privateButton bt_stop;
/**Called when the activity is first created. */
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
surfaceview = (SurfaceView) findViewById(R.id.surfaceview);
bt_start = (Button) findViewById(R.id.bt_start);
bt_stop = (Button) findViewById(R.id.bt_stop);
SurfaceHolder holder = surfaceview.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setFixedSize(176,144);
surfaceview.setKeepScreenOn(true);
}
publicvoidstart(View v){
try{
mr = newMediaRecorder();
mr.reset();
mr.setAudioSource(AudioSource.MIC);
mr.setVideoSource(VideoSource.CAMERA);
mr.setOutputFormat(OutputFormat.THREE_GPP);
mr.setAudioEncoder(AudioEncoder.AMR_NB);
mr.setVideoEncoder(VideoEncoder.H264);
Filefile = newFile(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3gp");
mr.setOutputFile(file.getAbsolutePath());
//设置预览显示
mr.setPreviewDisplay(surfaceview.getHolder().getSurface());
mr.prepare();
mr.start();
bt_start.setEnabled(false);
bt_stop.setEnabled(true);
} catch(Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
publicvoidstop(View v){
mr.stop();
bt_start.setEnabled(true);
bt_stop.setEnabled(false);
}
@Override
protectedvoidonDestroy() {
//TODO Auto-generated method stub
super.onDestroy();
if(mr!=null){
mr.release();
mr = null;
}
}
}
所需权限:
课后问题
怎么设置全屏显示
全屏显示有两种方式:
在androidManifest.xml里面的application标签下设置
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"全屏显示
Java代码
//指定Activity没有标题(一定要在setContentView之前调用)
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
//全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
怎么设置横屏显示
设置横屏
在androidManifest.xml里面的activity标签下设置
android:screenOrientation="landscape"横屏显示
Camer在使用的是是否需要权限
需要
使用哪个类可以完成视频的刻录
MediaRecorder