转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持!
相机拍照、视频录制相机拍照
全屏显示有两种方式:
在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 public void onCreate(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(new MyCallback()); } private class MyCallback implements Callback{ @Override public void surfaceCreated(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 public void surfaceChanged(SurfaceHolderholder, int format, int width, int height) { //TODO Auto-generated method stub } @Override public voidsurfaceDestroyed(SurfaceHolder holder) { //TODO Auto-generated method stub if(camera!= null){ camera.release(); camera= null; } } } //对焦都是由硬件直接完成 public void foucs(View v){ camera.autoFocus(null); } public void take(View v){ /** *shutter,快门 *raw, 相机捕获的原始数据 *jpeg 相机处理后的数据 */ camera.takePicture(null, null, newMyPictureCallback()); } private class MyPictureCallback implements PictureCallback{ @Override public void onPictureTaken(byte[] data,Camera camera) { //TODO Auto-generated method stub try{ File file = new File(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 protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }
所需权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.CAMERA"/>
视频录制
public class MainActivity extends Activity{ private SurfaceView surfaceview; privateMediaRecorder mr; privateButton bt_start; privateButton bt_stop; /**Called when the activity is first created. */ @Override public void onCreate(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); } public void start(View v){ try { mr = new MediaRecorder(); 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 = new File(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(); } } public void stop(View v){ mr.stop(); bt_start.setEnabled(true); bt_stop.setEnabled(false); } @Override protected voidonDestroy() { //TODO Auto-generated method stub super.onDestroy(); if(mr!= null){ mr.release(); mr = null; } } }
所需权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/>
课后问题
怎么设置全屏显示
全屏显示有两种方式:
在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