欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android實現(xiàn)定時拍照功能

 更新時間:2018年05月29日 13:54:03   作者:yhcelebrite  
這篇文章主要為大家詳細介紹了android實現(xiàn)定時拍照功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在手機上面實現(xiàn),設(shè)置一段時間(以秒計時)之后,自動拍照,適用于攝影師建立一個場景,之后設(shè)置時間,再進入場景。

界面主要就是一個設(shè)置時間的EditText和啟動倒計時的Button,設(shè)置完時間之后,點擊倒計時按鈕。

<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> 

在清單文件中加入權(quán)限:

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

主程序:

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(); 
  } 
 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android實現(xiàn)微信加號菜單模式

    Android實現(xiàn)微信加號菜單模式

    這篇文章主要為大家詳細介紹了Android實現(xiàn)微信加號菜單模式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 簡單實用的Android studio 調(diào)試技巧

    簡單實用的Android studio 調(diào)試技巧

    這篇文章主要介紹了簡單實用的Android studio 調(diào)試技巧的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • Android 跨進程模擬按鍵(KeyEvent )實例詳解

    Android 跨進程模擬按鍵(KeyEvent )實例詳解

    這篇文章主要介紹了Android 跨進程模擬按鍵(KeyEvent )實例詳解的相關(guān)資料,類似手機遙控器的需求就可以這么做,需要的朋友可以參考下
    2016-11-11
  • Android 解析XML 文件的四種方法總結(jié)

    Android 解析XML 文件的四種方法總結(jié)

    本文將詳細介紹用解析XML的四種方法,XML現(xiàn)在已經(jīng)成為一種通用的數(shù)據(jù)交換格式,平臺的無關(guān)性使得很多場合都需要用到XML,這里對幾種解析XML 的方法做詳解
    2016-07-07
  • Android自定義拋出異常的方法詳解

    Android自定義拋出異常的方法詳解

    這篇文章主要給大家介紹了關(guān)于Android自定義拋出異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • kotlin實現(xiàn)五子棋單機游戲

    kotlin實現(xiàn)五子棋單機游戲

    這篇文章主要為大家詳細介紹了kotlin實現(xiàn)五子棋單機游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Android 控件(button)對齊方法實現(xiàn)詳解

    Android 控件(button)對齊方法實現(xiàn)詳解

    horizontal是讓所有的子元素按水平方向從左到右排列,vertical是讓所有的子元素按豎直方向從上到下排列,下面為大家介紹下控件(button)的對齊方法
    2013-06-06
  • Android實現(xiàn)滑動側(cè)邊欄

    Android實現(xiàn)滑動側(cè)邊欄

    這篇文章主要為大家詳細介紹了Android實現(xiàn)滑動側(cè)邊欄效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Kotlin惰性集合操作之Sequence序列使用示例

    Kotlin惰性集合操作之Sequence序列使用示例

    這篇文章主要為大家介紹了Kotlin惰性集合操作之Sequence序列使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • Kotlin函數(shù)默認值的完全講解

    Kotlin函數(shù)默認值的完全講解

    這篇文章主要給大家介紹了關(guān)于Kotlin函數(shù)默認值的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Kotlin具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評論