在我们开发app时,会遇到程序中需要摄像的功能,我们最好的选择是跳转到手机自带的摄像程序中,但有时侯也可以在自己的应用程序中实现这个东西。
要用到的控件:SurfaceView显示照相机的画面
实现照相机的核心类:camera
实现视频录制的核心类:MediaRecorder
activity_main.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.example.android27_camera.MainActivity">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/sv_Camera"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="takePhoto"
android:text="拍照"
/>
</LinearLayout>
MainActivity.java
package com.example.android27_camera;
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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private SurfaceView sv_camera;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//surfaceView显示容器
sv_camera = (SurfaceView) findViewById(R.id.sv_Camera);
sv_camera.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
//打开摄像头(默认为后置摄像头,1为前置)
camera = Camera.open(1);
//给摄像设置参数
Camera.Parameters parameters= camera.getParameters();
//每秒几栈
// parameters.setPreviewFrameRate(3);
//设置照片的输出格式
parameters.setPictureFormat(PixelFormat.JPEG);
//设置照片质量
parameters.set("jpeg-quality",85);
//给照相机设置参数
camera.setParameters(parameters);
try {
//将摄像头的画面显示在SurfaceView上
camera.setPreviewDisplay(sv_camera.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) {
camera.stopPreview();
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/DCIM/camera/myCamera"+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.READ_EXTERNAL_STORAGE" />
<!-- 获得摄像头权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<!--获得存储的权限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
activity_media_recorder.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_media_recorder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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.example.android27_camera.MediaRecorderActivity">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="350dp"
android:id="@+id/sv_recorder"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:text="开始"
android:onClick="start"
android:layout_height="wrap_content" />
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:text="停止"
android:onClick="stop"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
MediaRecorderActivity.java:
package com.example.android27_camera;
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;
import static android.os.Build.VERSION_CODES.M;
public class MediaRecorderActivity extends AppCompatActivity {
private SurfaceView sv_recorder;
private MediaRecorder mediaRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_recorder);
sv_recorder = (SurfaceView) findViewById(R.id.sv_recorder);
//实例化媒体录制器
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.setVideoFrameRate(3);
//设置保存的路径
mediaRecorder.setOutputFile("mnt/sdcard/MyRecorder"+System.currentTimeMillis()+".mp4");
//将画面展示到SurfaceView
mediaRecorder.setPreviewDisplay(sv_recorder.getHolder().getSurface());
try {
//准备
mediaRecorder.prepare();
//启动
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop(View view){
mediaRecorder.stop();
}
}
最后别忘了加权限
<!--添加媒体录制器权限-->
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
源码下载: