Android自定义照相机倒计时拍照

时间:2022-04-17 14:57:34

自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件:

两个TextView是用来显示提示信息和倒计时的秒数的

相关教程:Android开发从相机或相册获取图片裁剪

Android启动相机拍照并返回图片

?
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
<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:background="#266194"
 android:orientation="vertical"
 tools:context=".TestActivity" >
 <SurfaceView
 android:id="@+id/surfaceView"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_centerInParent="true" />
 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:orientation="vertical" >
 <TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="请调整位置到此区域"
 android:textColor="#ff0000"
 android:textSize="32sp" />
 <TextView
 android:id="@+id/tv_time"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:paddingTop="10dp"
 android:gravity="center_horizontal"
 android:textColor="#266194"
 android:textSize="32sp" />
 </LinearLayout>
</RelativeLayout>

接下来是mainActivity中的具体实现以及详细注释:

?
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.dhsr.pujiejia.ui;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.example.pujiejiaapp.R;
@SuppressLint({ "NewApi", "SdCardPath" })
public class CameraActivity extends Activity implements Runnable {
 // 预览图片范围
 private SurfaceView surfaceView;
 private TextView tv_time;
 // 倒计时拍摄
 private int cameratime = 4;
 private Camera camera;
 private boolean preview = false;
 // 文件名字
 private String filename;
 // 文件名字的带的时间戳
 private String timeString;
 // 格式化时间
 private SimpleDateFormat dateFormat;
 // 日期对象
 private Date date;
 // 控制线程
 boolean stopThread = false;
 private File file;
 String photo;
 private Handler mHandler = new Handler() {
 public void handleMessage(android.os.Message msg) {
 int what = msg.what;
 switch (what) {
 case 222:
 tv_time.setText("" + cameratime);
 if ("0".equals(tv_time.getText().toString())) {
  tv_time.setText("拍摄成功!");
  takePhoto();
 }
 break;
 }
 };
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_test);
 CameraActivity.this.setFinishOnTouchOutside(false);
 // 初始化数据
 findView();
 surfaceView.getHolder().addCallback(new SufaceListener());
 /* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */
 surfaceView.getHolder()
 .setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 surfaceView.getHolder().setFixedSize(200, 200); // 设置分辨率
 }
 @Override
 protected void onStart() {
 // TODO Auto-generated method stub
 super.onStart();
 // 开启线程
 new Thread(this).start();
 }
 private final class SufaceListener implements SurfaceHolder.Callback {
 /**
 * surface改变
 */
 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
 int height) {
 }
 /**
 * surface创建
 */
 @Override
 public void surfaceCreated(SurfaceHolder holder) {
 try {
 for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
  CameraInfo info = new CameraInfo();
  Camera.getCameraInfo(i, info);
  // 调用系统的前置摄像头
  if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
  camera = Camera.open(i);
  }
 }
 Camera.Parameters parameters = camera.getParameters();
 /* 每秒从摄像头捕获5帧画面, */
 parameters.setPreviewFrameRate(5);
 /* 设置照片的输出格式:jpg */
 parameters.setPictureFormat(PixelFormat.JPEG);
 /* 照片质量 */
 parameters.set("jpeg-quality", 85);
 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
 camera.setParameters(parameters);
 camera.setPreviewDisplay(surfaceView.getHolder());// 通过SurfaceView显示取景画面
 camera.startPreview();
 preview = true;
 } catch (Exception e) {
 }
 }
 /**
 * surface销毁
 */
 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
 if (camera != null) {
 if (preview)
  camera.stopPreview();
 camera.release();
 camera = null;
 }
 }
 }
 /**
 * 拍摄照片
 */
 private void takePhoto() {
 // 执行拍照效果
 camera.takePicture(null, null, new Camera.PictureCallback() {
 @Override
 public void onPictureTaken(byte[] data, Camera camera) {
 try {
  Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
  data.length);
  timeString = formatDate();
  //保存到data/data目录自定义文件夹下
  filename = "/data/data/com.example.pujiejiaapp/images/"
  + timeString + ".jpg";
  File file = new File(filename);
  boolean createNewFile = file.createNewFile()
  System.out.println("创建文件夹成功没有" + createNewFile);
  System.out.println(file);
  FileOutputStream outStream = new FileOutputStream(file);
  bitmap.compress(Bitmap.CompressFormat.JPEG, 60, outStream);
  outStream.flush();
  outStream.close();
  // 重新浏览
  camera.stopPreview();
  camera.startPreview();
  preview = true;
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
 }
 }
 });
 }
 @Override
 public void run() {
 while (!stopThread) {
 try {
 //按秒数倒计时
 Thread.sleep(1000);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 cameratime--;
 mHandler.sendEmptyMessage(222);
 if (cameratime <= 0) {
 break;
 }
 }
 }
 // 初始化数据
 private void findView() {
 surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
 tv_time = (TextView) findViewById(R.id.tv_time);
 }
 // 格式化系统的时间
 public String formatDate() {
 date = new Date(System.currentTimeMillis());
 // 日期格式
 dateFormat = new SimpleDateFormat("'IMG'_yyyyMMddHHmmss");
 return dateFormat.format(date);
 }
 @Override
 protected void onDestroy() {
 // TODO Auto-generated method stub
 // 线程已关闭
 super.onDestroy();
 stopThread = true;
 }
}

核心代码详解:

1.创建SurfaceView时,surfaceCreated()方法中

?
1
2
3
4
5
6
7
8
for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
  CameraInfo info = new CameraInfo();
  Camera.getCameraInfo(i, info);
  // 调用系统的前置摄像头
  if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
  camera = Camera.open(i);
  }
 }

此部分代码为打开相机时默认打开前置摄像头CameraInfo.CAMERA_FACING_BACK为默认打开后置摄像头,CameraInfo.CAMERA_FACING_FRONT前置摄像头

2.照片拍摄takePhoto()方法中:

?
1
2
3
4
5
6
7
8
9
10
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
  data.length);
  timeString = formatDate();
  filename = "/data/data/com.example.pujiejiaapp/images/"
  + timeString + ".jpg";
  photo = timeString + ".jpg";
  File file = new File(filename);
  boolean createNewFile = file.createNewFile();
  FileOutputStream outStream = new FileOutputStream(file);
  bitmap.compress(Bitmap.CompressFormat.JPEG, 60, outStream);

此部分代码为将拍摄到的图片保存为以bitmap格式保存在指定的目录下

3.开子线程用于倒计时拍摄

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void run() {
 while (!stopThread) {
 try {
 Thread.sleep(1000);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 cameratime--;
 mHandler.sendEmptyMessage(222);
 if (cameratime <= 0) {
 break;
 }
 }
 }

希望大家理解核心代码的详细注释,欢迎提供意见,希望能给大家带来帮助,谢谢!