服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android实现定时拍照功能

android实现定时拍照功能

2022-02-25 15:32yhcelebrite Android

这篇文章主要为大家详细介绍了android实现定时拍照功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在手机上面实现,设置一段时间(以秒计时)之后,自动拍照,适用于摄影师建立一个场景,之后设置时间,再进入场景。

界面主要就是一个设置时间的EditText和启动倒计时的Button,设置完时间之后,点击倒计时按钮。

?
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
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/frameLayout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >
 
  <SurfaceView
    android:id="@+id/imageView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
 
  <LinearLayout
    android:id="@+id/lineLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
      android:id="@+id/startBtn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/startTimer"
      android:layout_gravity="center_horizontal"/>
     
<!--     <TextView -->
<!--       android:id="@+id/countDowntextView" -->
<!--       android:layout_width="fill_parent" -->
<!--       android:layout_height="fill_parent" -->
<!--       android:layout_gravity="center_horizontal|center_vertical|center" -->
<!--       android:gravity="center_horizontal|center_vertical" -->
<!--       android:text="@string/conutTime" -->
<!--       android:textSize="40sp" /> -->
     
    <EditText
      android:id="@+id/countDownEditTextView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_gravity="center_horizontal|center_vertical|center"
      android:gravity="center_horizontal|center_vertical"
      android:text="@string/conutTime"
      android:textSize="80sp"
      android:inputType="number"/>
 
     
  </LinearLayout>
 
</FrameLayout>

在清单文件中加入权限:

?
1
2
3
4
<uses-permission android:name="android.permission.CAMERA" />
<!--下面的可不需要-->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

主程序:

?
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
package cn.yh.cameradelaycontroll;
 
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
 
import android.app.Activity;
import android.content.ContentValues;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore.Images.Media;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity implements SurfaceHolder.Callback,
    OnClickListener, PictureCallback {
 
  private static final String CAMERA_CONTROLL = "CAMERA_CONTROLL";
  private SurfaceView imageSView;
  private Button startButton;
  // private TextView countDownTextView;
  private EditText countDownEditTextView;
  private Camera camera;
  private SurfaceHolder surfaceHolder;
  private Handler timerUpdateHandler;
  private boolean timerRunning = false;
  private int currentTimer = 10;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageSView = (SurfaceView) findViewById(R.id.imageView);
    startButton = (Button) findViewById(R.id.startBtn);
    // countDownTextView = (TextView) findViewById(R.id.countDowntextView);
    countDownEditTextView = (EditText) findViewById(R.id.countDownEditTextView);
    /*
    countDownEditTextView.addTextChangedListener(new TextWatcher() {
 
      @Override
      public void onTextChanged(CharSequence s, int start, int before,
          int count) {
        // TODO Auto-generated method stub
      }
 
      @Override
      public void beforeTextChanged(CharSequence arg0, int arg1,
          int arg2, int arg3) {
        // TODO Auto-generated method stub
 
      }
 
      @Override
      public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub
        currentTimer = Integer.parseInt(countDownEditTextView.getText().toString());
      }
    });
    */
    surfaceHolder = imageSView.getHolder();
 
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 
    surfaceHolder.addCallback(this);
 
    startButton.setOnClickListener(this);
 
    timerUpdateHandler = new Handler();
  }
 
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
 
  @Override
  public void onPictureTaken(byte[] data, Camera camera) {
    // TODO Auto-generated method stub
    Uri imageFileUri = getContentResolver().insert(
        Media.EXTERNAL_CONTENT_URI, new ContentValues());
    try {
      OutputStream imageFileOS = getContentResolver().openOutputStream(
          imageFileUri);
      imageFileOS.write(data);
      imageFileOS.flush();
      imageFileOS.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
    camera.startPreview();
  }
 
  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    currentTimer = Integer.parseInt(countDownEditTextView.getText().toString());
    switch (v.getId()) {
    case R.id.startBtn:
      if (!timerRunning) {
        timerRunning = true;
        timerUpdateHandler.post(timerUpdateTask);
      }
      break;
    }
  }
 
  private Runnable timerUpdateTask = new Runnable() {
 
    @Override
    public void run() {
      // TODO Auto-generated method stub
      if (currentTimer > 1) {
        currentTimer--;
        timerUpdateHandler.postDelayed(timerUpdateTask, 1000);
      } else {
        camera.takePicture(null, null, null, MainActivity.this);
        timerRunning = false;
        currentTimer = 10;
      }
      countDownEditTextView.setText(currentTimer + "");
    }
  };
 
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
      int height) {
    // TODO Auto-generated method stub
    camera.startPreview();
  }
 
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    int cameraNums = Camera.getNumberOfCameras();
    Log.e(CAMERA_CONTROLL, cameraNums + "");
    try {
      camera = Camera.open(cameraNums - 1);
    } catch (Exception e) {
      Log.e(CAMERA_CONTROLL, e.getMessage());
    }
    try {
      camera.setPreviewDisplay(holder);
      Camera.Parameters parameters = camera.getParameters();
      if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        parameters.set("orientation", "portrait");
        camera.setDisplayOrientation(90);
        parameters.setRotation(90);
      }
      List<String> colorEffects = parameters.getSupportedColorEffects();
      Iterator<String> cei = colorEffects.iterator();
      while (cei.hasNext()) {
        String currentEffect = cei.next();
        if (currentEffect.equals(Camera.Parameters.EFFECT_SOLARIZE)) {
          parameters
              .setColorEffect(Camera.Parameters.EFFECT_SOLARIZE);
          break;
        }
      }
      camera.setParameters(parameters);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      // e.printStackTrace();
      camera.release();
    }
  }
 
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera.stopPreview();
    camera.release();
  }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yhcelebrite/article/details/11763417

延伸 · 阅读

精彩推荐