1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.example.rscamera; 17 18 import android.app.Activity; 19 import android.os.Bundle; 20 import android.view.View; 21 import android.widget.Button; 22 import android.widget.SeekBar; 23 import android.widget.ViewFlipper; 24 25 import com.android.example.rscamera.rscamera.R; 26 27 import java.text.DecimalFormat; 28 import java.util.Timer; 29 import java.util.TimerTask; 30 31 /** 32 * Main Activity for this app 33 * It presents a ui for setting ISO, Shutter speed, and focus 34 */ 35 public class MainActivity extends Activity { 36 private static final String TAG = "MainActivity"; 37 private static final long ONE_SECOND = 1000000000; 38 private CameraView mPreviewView; 39 private ViewFlipper mViewFlipper; 40 private Button mSpeedButton; 41 private Button mISOButton; 42 private Button mFocusButton; 43 private Timer mTimer; 44 45 @Override onCreate(Bundle savedInstanceState)46 protected void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 setContentView(R.layout.activity_main); 49 mSpeedButton = findViewById(R.id.speed); 50 mISOButton = findViewById(R.id.iso); 51 mFocusButton = findViewById(R.id.focus); 52 mPreviewView = findViewById(R.id.preview); 53 mViewFlipper = findViewById(R.id.viewFlipper); 54 SeekBar seekBar = findViewById(R.id.focusbar); 55 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 56 @Override 57 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 58 mPreviewView.setFocusInMeters(seekBar.getProgress() / 10.f); 59 } 60 61 @Override 62 public void onStartTrackingTouch(SeekBar seekBar) { 63 64 } 65 66 @Override 67 public void onStopTrackingTouch(SeekBar seekBar) { 68 69 } 70 }); 71 mPreviewView.setParametersChangedCallback(new CameraView.ParametersChangedCallback() { 72 @Override 73 public void parametersChanged() { 74 update_buttons(); 75 } 76 }); 77 mTimer = new Timer(); 78 79 mTimer.scheduleAtFixedRate(new TimerTask() { 80 81 @Override 82 public void run() { 83 // TODO Auto-generated method stub 84 runOnUiThread(new Runnable() { 85 public void run() { 86 setTitle("RS Camera (" + mPreviewView.getFps() + "fps)"); 87 } 88 }); 89 90 } 91 }, 250, 250); 92 } 93 94 @Override onResume()95 protected void onResume() { 96 super.onResume(); 97 mPreviewView.resume(this); 98 } 99 100 @Override onPause()101 protected void onPause() { 102 super.onPause(); 103 mPreviewView.pause(); 104 } 105 setShutterSpeed(View v)106 public void setShutterSpeed(View v) { 107 if (mPreviewView.isAutoExposure()) { 108 mPreviewView.setAutoExposure(false); 109 mPreviewView.setMode(CameraView.MODE_SPEED); 110 } else { 111 mPreviewView.setMode(CameraView.MODE_NONE); 112 mPreviewView.setAutoExposure(true); 113 } 114 update_buttons(); 115 } 116 setISO(View v)117 public void setISO(View v) { 118 if (mPreviewView.isAutoExposure()) { 119 mPreviewView.setAutoExposure(false); 120 mPreviewView.setMode(CameraView.MODE_ISO); 121 } else { 122 mPreviewView.setMode(CameraView.MODE_NONE); 123 mPreviewView.setAutoExposure(true); 124 } 125 update_buttons(); 126 } 127 setFocus(View v)128 public void setFocus(View v) { 129 if (mPreviewView.isAutofocus()) { 130 mPreviewView.setAutofocus(false); 131 mPreviewView.setMode(CameraView.MODE_FOCUS); 132 mViewFlipper.setInAnimation(this, R.anim.slide_in_from_left); 133 mViewFlipper.setOutAnimation(this, R.anim.slide_out_to_right); 134 mViewFlipper.showNext(); 135 } else { 136 mPreviewView.setMode(CameraView.MODE_NONE); 137 mPreviewView.setAutofocus(true); 138 } 139 update_buttons(); 140 } 141 back(View v)142 public void back(View v) { 143 mViewFlipper.setInAnimation(this, R.anim.slide_in_from_left); 144 mViewFlipper.setOutAnimation(this, R.anim.slide_out_to_right); 145 mViewFlipper.showNext(); 146 } 147 capture(View v)148 public void capture(View v) { 149 mPreviewView.takePicture(); 150 } 151 update_buttons()152 private void update_buttons() { 153 byte mode = mPreviewView.getMode(); 154 mSpeedButton.setElevation(mode == CameraView.MODE_SPEED ? 20 : 0); 155 mFocusButton.setElevation(mode == CameraView.MODE_FOCUS ? 20 : 0); 156 mISOButton.setElevation(mode == CameraView.MODE_ISO ? 20 : 0); 157 158 String a; 159 a = (mPreviewView.isAutoExposure()) ? "A " : " "; 160 if (ONE_SECOND > mPreviewView.getExposure()) { 161 mSpeedButton.setText(a + 1 + "/" + (ONE_SECOND / mPreviewView.getExposure()) + "s"); 162 } else { 163 mSpeedButton.setText(a + (mPreviewView.getExposure() / ONE_SECOND) + "\"s"); 164 165 } 166 a = (mPreviewView.isAutofocus()) ? "A " : " "; 167 DecimalFormat df = new DecimalFormat("#.###"); 168 float d = mPreviewView.getFocusDist(); 169 if (d < 0.01) { 170 d = 0; 171 } 172 mFocusButton.setText(a + df.format(0.1 / d) + " m"); 173 a = (mPreviewView.isAutoExposure()) ? "A ISO " : " ISO "; 174 mISOButton.setText(a + mPreviewView.getIso() + " M"); 175 } 176 } 177