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