本文实例为大家分享了Android实现拍照和录制视频功能的具体代码,供大家参考,具体内容如下
文档中的Camera
要使用Camera,首先我们先看一下文档中是怎么介绍的。相对于其他绝大多数类,文档对Camera的介绍还是比较详尽的,包含了使用过程中所需要的步骤说明,当然,这也表明了它在实际使用中的繁琐。
首先,需要在AndroidManifest.xml中声明以下权限和特性:
1
2
3
|
<uses-permission android:name= "android.permission.CAMERA" />
<uses-feature android:name= "android.hardware.camera" />
<uses-feature android:name= "android.hardware.camera.autofocus" />
|
然后,拍照的话,需要以下十步:
1. 通过open(int)方法得到一个实例
2. 通过getParameters()方法得到默认的设置
3. 如果有必要,修改上面所返回的Camera.Parameters对象,并调用setParameters(Camera.Parameters) 进行设置
4. 如果有需要,调用setDisplayOrientation(int)设置显示的方向
5. 这一步很重要,通过setPreviewDisplay(SurfaceHolder)传入一个已经初始化了的SurfaceHolder,否则无法进行预览。
6. 这一步也很重要,通过startPreview()开始更新你的预览界面,在你拍照之前,它必须开始。
7. 调用takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback)进行拍照,等待它的回调
8. 拍照之后,预览的展示会停止。如果想继续拍照,需要先再调用startPreview()。
9. 调用stopPreview()停止预览。
10. 非常重要,调用release()释放Camera,以使其他应用也能够使用相机。你的应用应该在onPause()被调用时就进行释放,在onResume()时再重新open()。
上面就是文档中关于使用Camera进行拍照的介绍了。接下来说一下我的使用场景。
直接上代码好咯
布局:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<? 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: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"
>
< SurfaceView
android:layout_width = "match_parent"
android:layout_height = "0dp"
android:layout_weight = "1"
android:id = "@+id/sv_main_surface"
/>
< Button
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:onClick = "takePhoto"
android:text = "拍照"
/>
</ LinearLayout >
|
Activity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
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 android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private SurfaceView sv_main_surface;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
sv_main_surface.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(sv_main_surface.getHolder());
} catch (IOException 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 ;
}
}
});
}
public void takePhoto(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 (FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
}
|
录制视频:
布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<? 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: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"
>
< SurfaceView
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:id = "@+id/sv_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 >
|
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
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 MediaRecorderActivity extends AppCompatActivity {
private SurfaceView sv_recorder_surface;
private MediaRecorder mediaRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_media_recorder);
sv_recorder_surface = (SurfaceView) findViewById(R.id.sv_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(sv_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 ;
}
}
}
|
最重要的是别忘了加权限哦
1
2
3
4
5
6
7
|
<!-- 打开照相机的权限 -->
<uses-permission android:name= "android.permission.CAMERA" />
<uses-permission android:name= "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name= "android.permission.RECORD_AUDIO" >
</uses-permission>
|
简单的拍照还录制视频的功能就实现了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/HuangGui971022/article/details/55046385