亲宝软件园·资讯

展开

Android SurfaceView拍照录像 Android SurfaceView拍照录像实现方法

Smile_muse 人气:0
想了解Android SurfaceView拍照录像实现方法的相关内容吗,Smile_muse在本文为您仔细讲解Android SurfaceView拍照录像的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,SurfaceView,拍照,录像,下面大家一起来学习吧。

Surface的拍照实现也是很简单,一个小demo就可以把流程看懂了。

话不多说,直接上代码

布局文件

<SurfaceView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/sv_main_surface"
    />

  <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="tackPhoto"
    android:text="拍照"
    />

Activity

public class MainActivity extends AppCompatActivity {

  private SurfaceView sv_main_surface;
  private Camera camera;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);

    //添加surface回调函数
    sv_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(sv_main_surface.getHolder());
        } catch (IOException e) {
          e.printStackTrace();
        }
        //开启预览效果
        camera.startPreview();

      }

      @Override//控件改变
      public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

      }

      @Override//控件销毁
      public void surfaceDestroyed(SurfaceHolder holder) {
        //照相同一时刻只能允许一个软件打开
        if(camera!=null){
          camera.stopPreview();
          camera.release();//释放内存
          camera=null;
        }
      }
    });




  }


  public void takePhoto(View view){
    camera.takePicture(null, null, new Camera.PictureCallback() {
      @Override
      public void onPictureTaken(byte[] bytes, Camera camera) {
        //技术:图片压缩技术(如果图片不压缩,图片大小会过大,会报一个oom内存溢出的错误)
        Bitmap bitmap= BitmapFactory.decodeByteArray(bytes,0,bytes.length);
        try {
          FileOutputStream fos = new FileOutputStream("/mnt/sdcard/qq"+System.currentTimeMillis()+".png");//图片保存路径
          bitmap.compress(Bitmap.CompressFormat.PNG,85,fos);//压缩格式,质量,压缩路径

          camera.stopPreview();
          camera.startPreview();
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        }
      }
    });

  }
}

我们还要添加权限

   <!--打开照相机的权限-->
  <uses-permission android:name="android.permission.CAMERA"></uses-permission>
  <!--创建文件的权限-->
  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
  <!--写内存卡的权限-->
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

就这样一个小案例就完成了

既然可以拍照,那肯定也是可以录像的,所以我们再来看看录像是怎么实现的

布局文件

<?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_recorder"
  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.zking.familyapp.MediaRecorderActivity">

  <SurfaceView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/sv_media_surface"
    />

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    ></LinearLayout>

  <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"
    />

</RelativeLayout>


Activity

public class MediaRecorderActivity extends AppCompatActivity {

  private SurfaceView sv_media_surface;
  private MediaRecorder mediaRecorder;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_media_recorder);
    sv_media_surface = (SurfaceView) findViewById(R.id.sv_media_surface);

    //实例化媒体录制器
    mediaRecorder = new MediaRecorder();
  }


  public void start(View view){
    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/uu"+System.currentTimeMillis()+".mp4");

    mediaRecorder.setPreviewDisplay(sv_media_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;
    }

  }
}


添加权限

<!-- 录像的权限-->
  <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>

加载全部内容

相关教程
猜你喜欢
用户评论