这篇文章给大家分享的是有关android如何实现定时拍照功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
创新互联长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为龙沙企业提供专业的成都网站制作、成都网站设计,龙沙网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。
在手机上面实现,设置一段时间(以秒计时)之后,自动拍照,适用于摄影师建立一个场景,之后设置时间,再进入场景。
界面主要就是一个设置时间的EditText和启动倒计时的Button,设置完时间之后,点击倒计时按钮。
在清单文件中加入权限:
主程序:
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); } ListcolorEffects = parameters.getSupportedColorEffects(); Iterator 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(); } }
感谢各位的阅读!关于“android如何实现定时拍照功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!