最近研究了下用安卓编写照相机代码和录制的代码,分享给大家
首先是照相机的代码,xml里面的代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.liuqian.android_27camera.MainActivity"> <SurfaceView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/main_surface"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" android:onClick="takePhoto" /> </LinearLayout>
然后是Java文件的代码
package com.liuqian.android_27camera; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import java.io.FileOutputStream; public class MainActivity extends AppCompatActivity { private SurfaceView main_surface; private Camera camera; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); main_surface = (SurfaceView) findViewById(R.id.main_surface); main_surface.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { //打开照相机 camera = Camera.open(); //参数 Camera.Parameters parameters= camera.getParameters(); //设置照片的输出格式 parameters.setPictureFormat(PixelFormat.JPEG); //设置照片的质量 parameters.set("jpeg-quality",85); //设置参数 camera.setParameters(parameters); //将画面展现到SurfaceView try{ camera.setPreviewDisplay(main_surface.getHolder()); }catch (Exception e){ e.printStackTrace(); } //开启预览模式 camera.startPreview(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { //当预览界面关闭时,销毁照相机 camera.startPreview(); camera.release(); camera=null; } }); } public void takePhoto(View view){ camera.takePicture(null, null, new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { //图片压缩技术 Bitmap bitmap= BitmapFactory.decodeByteArray(data,0,data.length); try{ FileOutputStream fos=new FileOutputStream("/mnt/sdcard/G150820"+System.currentTimeMillis()+".png"); bitmap.compress(Bitmap.CompressFormat.PNG,85,fos); camera.stopPreview(); camera.startPreview(); }catch (Exception e){ e.printStackTrace(); } } }); } }
接下来的话是录制的代码,而首先的xml里面的代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_media_record" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.liuqian.android_27camera.MediaRecordActivity"> <SurfaceView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recorder_surface" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" android:onClick="start" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止" android:onClick="stop" /> </LinearLayout> </RelativeLayout>
接着的是录制Java文件的代码
package com.liuqian.android_27camera; import android.media.MediaRecorder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.SurfaceView; import android.view.View; import java.io.IOException; public class MediaRecordActivity extends AppCompatActivity { private SurfaceView recorder_surface; private MediaRecorder mediaRecorder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_media_record); //得到SurfaceView recorder_surface = (SurfaceView) findViewById(R.id.recorder_surface); //实例化媒体录制器 mediaRecorder = new MediaRecorder(); } public void start(View view){ //重置媒体录制器 mediaRecorder.reset(); //从照相机采集视频 mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置格式 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); //设置编码方式 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //设置保存路径 mediaRecorder.setOutputFile("/mnt/sdcard/G150820"+System.currentTimeMillis()+".mp4"); //设置预览 mediaRecorder.setPreviewDisplay(recorder_surface.getHolder().getSurface()); try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } } public void stop(View view){ if(mediaRecorder!=null){ mediaRecorder.stop(); mediaRecorder.release();; mediaRecorder=null; } } }
分享也就到此结束了,如有失误,请指正,本人虚心接受,谢谢!!