Android 仿相机拍照 录像功能

来源:互联网 发布:nginx 域名不能访问 编辑:程序博客网 时间:2024/06/02 07:30

一.模仿的是拍照功能

   下面是代码

   

public class MainActivity extends AppCompatActivity {    private Camera camera;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化控件        initview();    }    //初始化控件    private void initview(){       final SurfaceView surfaceView_sf = (SurfaceView) findViewById(R.id.surfaceView_sf);       Button takePhoto_btn = (Button) findViewById(R.id.takePhoto_btn);        //SurfaceView回调方法        surfaceView_sf.getHolder().addCallback(new SurfaceHolder.Callback() {            //创建调用            @Override            public void surfaceCreated(SurfaceHolder surfaceHolder) {                  //打开照相机                 camera = Camera.open();                   //给相机参数设置值                   Camera.Parameters parameters =camera.getParameters();                      //设置图片格式                      parameters.setPictureFormat(PixelFormat.JPEG);                      //设置图片质量                      parameters.set("jpeg-quality",85);                   //给相机设置参数                   camera.setParameters(parameters);                   //把相机捕捉到的画面 展示到SurfaceView                   try{                      camera.setPreviewDisplay(surfaceView_sf.getHolder());                   }catch (Exception e){                       e.printStackTrace();                   }                 //开启预览效果                camera.startPreview();            }            //改变调用            @Override            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {            }            //销毁调用            @Override            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {                 if(camera!=null){                     camera.stopPreview();                     camera.release();                     camera=null;                 }            }        });        //点击拍照        takePhoto_btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {               camera.takePicture(null, null, new Camera.PictureCallback() {                   @Override                   public void onPictureTaken(byte[] bytes, Camera camera) {                       //图片压缩                     Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.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代码

<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:orientation="vertical"    tools:context="com.example.android_27_videoplayer.MainActivity">    <SurfaceView        android:layout_width="match_parent"        android:layout_height="300dp"        android:id="@+id/surfaceView_sf"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="拍照"        android:id="@+id/takePhoto_btn"        /></LinearLayout>

二.模仿的是录像功能

 

public class MediaRecorder_Activity extends AppCompatActivity {    private MediaRecorder mediaRecorder;    private SurfaceView surfaceView_recoder_sf;    private Button start_btn;    private Button pause_btn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_media_recorder_);        //初始化视图        initview();    }    //初始化视图    private void initview(){       surfaceView_recoder_sf = (SurfaceView) findViewById(R.id.surfaceView_recoder_sf);        start_btn = (Button) findViewById(R.id.start_btn);        pause_btn = (Button) findViewById(R.id.pause_btn);        //实例化媒体录制器        mediaRecorder = new MediaRecorder();        //开始录制        start_btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                mediaRecorder.reset();                mediaRecorder.setAudioSource(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(surfaceView_recoder_sf.getHolder().getSurface());                try{                   mediaRecorder.prepare();                    mediaRecorder.start();                }catch (Exception e){                    e.printStackTrace();                }            }        });        //停止录制        pause_btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if(mediaRecorder !=null){                    mediaRecorder.stop();                    mediaRecorder.release();                    mediaRecorder =null;                }            }        });    }}

   xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_media_recorder_"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.android_27_videoplayer.MediaRecorder_Activity">    <SurfaceView        android:layout_width="match_parent"        android:layout_height="350dp"        android:id="@+id/surfaceView_recoder_sf"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="开始"            android:id="@+id/start_btn"            android:textSize="20sp"            />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="停止"            android:id="@+id/pause_btn"            android:textSize="20sp"            />    </LinearLayout></LinearLayout>



 



0 0
原创粉丝点击