summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/ui/TrackingFocusRenderer.java
blob: 3738137c28da1e3cf8f381074b6e0e66a55ca2ea (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Copyright (c) 2016, The Linux Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of The Linux Foundation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.camera.ui;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;

import com.android.camera.CameraActivity;
import com.android.camera.CaptureModule;
import com.android.camera.CaptureUI;
import com.android.camera.imageprocessor.filter.TrackingFocusFrameListener;

public class TrackingFocusRenderer extends OverlayRenderer implements FocusIndicator {
    private FocusRequestThread mFocusRequestThread = null;
    private TrackingFocusFrameListener.Result mResult;
    private CameraActivity mActivity;
    private CaptureModule mModule;
    private Paint mTargetPaint;
    private int mInX = -1;
    private int mInY = -1;
    private final static int CIRCLE_THUMB_SIZE = 100;
    private Object mLock = new Object();
    private Rect mSurfaceDim;
    private CaptureUI mUI;

    public final static int STATUS_INIT = 0;
    public final static int STATUS_INPUT = 1;
    public final static int STATUS_TRACKING = 2;
    public final static int STATUS_TRACKED = 3;
    private int mStatus = STATUS_INIT;

    private final static String TAG = "TrackingFocusRenderer";
    private final static boolean DEBUG = false; //Enabling DEBUG LOG reduces the performance drastically.

    private void printErrorLog(String msg) {
        if(DEBUG) {
            android.util.Log.e(TAG, msg);
        }
    }

    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        if(!visible) {
            synchronized (mLock) {
                mStatus = STATUS_INIT;
                mResult = null;
                mInX = 0;
                mInY = 0;
            }
            if(mFocusRequestThread != null) {
                mFocusRequestThread.kill();
                mFocusRequestThread = null;
            }
        } else {
            mFocusRequestThread = new FocusRequestThread();
            mFocusRequestThread.start();
        }
    }

    public void setSurfaceDim(int left, int top, int right, int bottom) {
        mSurfaceDim = new Rect(left, top, right, bottom);
    }

    public TrackingFocusRenderer(CameraActivity activity, CaptureModule module, CaptureUI ui) {
        mActivity = activity;
        mModule = module;
        mUI = ui;
        mTargetPaint = new Paint();
        mTargetPaint.setStrokeWidth(4f);
        mTargetPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    public boolean handlesTouch() {
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch(event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
                synchronized (mLock) {
                    mInX = (int) event.getX();
                    mInY = (int) event.getY();
                    if(!mSurfaceDim.contains(mInX, mInY)) {
                        break;
                    }
                    mStatus = STATUS_INPUT;
                }
                update();
                break;
        }
        return true;
    }

    public int[] getInputCords(int width, int height) {
        synchronized (mLock) {
            if (mStatus != STATUS_INPUT) {
                return null;
            }
            mStatus = STATUS_TRACKING;
            int x = (mUI.getDisplaySize().y-1-mInY);
            int y = mInX;
            int bottomMargin = mUI.getDisplaySize().y - mSurfaceDim.bottom;
            x = (int)((x - bottomMargin)*((float)width/mSurfaceDim.height()));
            y = (int)((y - mSurfaceDim.left)*((float)height/mSurfaceDim.width()));

            /* It's supposed to give x,y like above but library x,y is reversed*/
            if(mModule.isBackCamera()) {
                x = width - 1 - x;
            }
            y = height-1-y;

            return new int[]{x, y};
        }
    }

    public void putRegisteredCords(TrackingFocusFrameListener.Result result, int width, int height) {
        synchronized (mLock) {
            if(result != null && result.pos != null &&
                    !(result.pos.width() == 0 && result.pos.height() == 0)) {
                result.pos = translateToSurface(result.pos, width, height);
                mResult = result;
                mStatus = STATUS_TRACKED;
            } else {
                mStatus = STATUS_TRACKING;
            }
        }
        mActivity.runOnUiThread(new Runnable() {
            public void run() {
                update();
            }
        });
    }

    private Rect translateToSurface(Rect src, int width, int height) {
        /* It's supposed to be this but direction is reversed in library.
        int x = src.centerY();
        int y = width-1-src.centerX();
         */
        int x = height-1-src.centerY();
        int y = src.centerX();
        if(!mModule.isBackCamera()) {
            y = width-1-src.centerX();
        }
        int w = (int)(src.height()*((float)mSurfaceDim.width()/height));
        int h = (int)(src.width()*((float)mSurfaceDim.height()/width));
        x = mSurfaceDim.left + (int)(x*((float)mSurfaceDim.width()/height));
        y = mSurfaceDim.top + (int)(y*((float)mSurfaceDim.height()/width));
        Rect rect = new Rect();
        rect.left = x - w/2;
        rect.top = y - h/2;
        rect.right = rect.left + w;
        rect.bottom = rect.top + h;
        return rect;
    }

    private Rect mRect;

    @Override
    public void onDraw(Canvas canvas) {
        synchronized (mLock) {
            if(mResult == null) {
                return;
            }
            if(mStatus == STATUS_TRACKED) {
                mRect = mResult.pos;
            }
        }

        if(mStatus == STATUS_TRACKED) {
            if(mRect != null) {
                mTargetPaint.setColor(Color.GREEN);
                canvas.drawRect(mRect, mTargetPaint);
            }
        } else if(mStatus == STATUS_TRACKING){
            if(mRect != null) {
                mTargetPaint.setColor(Color.RED);
                canvas.drawRect(mRect, mTargetPaint);
            }
        } else if(mStatus == STATUS_INPUT){
            mTargetPaint.setColor(Color.RED);
            canvas.drawCircle(mInX, mInY, CIRCLE_THUMB_SIZE, mTargetPaint);
        }
    }

    @Override
    public void showStart() {
    }

    @Override
    public void showSuccess(boolean timeout) {
    }

    @Override
    public void showFail(boolean timeout) {

    }

    @Override
    public void clear() {

    }

    private class FocusRequestThread extends Thread {
        private boolean isRunning = true;
        private final static int FOCUS_DELAY = 1000;
        private final static int MIN_DIFF_CORDS = 100;
        private final static int MIN_DIFF_SIZE = 100;
        private int mOldX = -MIN_DIFF_CORDS;
        private int mOldY = -MIN_DIFF_CORDS;
        private int mOldWidth = -MIN_DIFF_SIZE;
        private int mOldHeight = -MIN_DIFF_SIZE;
        private int mNewX;
        private int mNewY;
        private int mNewWidth;
        private int mNewHeight;

        public void kill() {
            isRunning = false;
        }

        public void run() {
            while(isRunning) {
                try {
                    Thread.sleep(FOCUS_DELAY);
                }catch(InterruptedException e) {
                    //Ignore
                }

                synchronized (mLock) {
                    if (mResult == null || mResult.pos == null
                            || (mResult.pos.centerX() == 0 && mResult.pos.centerY() == 0)) {
                        continue;
                    }
                    mNewX = mResult.pos.centerX();
                    mNewY = mResult.pos.centerY();
                    mNewWidth = mResult.pos.width();
                    mNewHeight = mResult.pos.height();
                }
                if(Math.abs(mOldX - mNewX) >= MIN_DIFF_CORDS || Math.abs(mOldY - mNewY) >= MIN_DIFF_CORDS  ||
                        Math.abs(mOldWidth - mNewWidth) >= MIN_DIFF_SIZE || Math.abs(mOldHeight - mNewHeight) >= MIN_DIFF_SIZE) {
                    mModule.onSingleTapUp(null, mNewX, mNewY);
                    mOldX = mNewX;
                    mOldY = mNewY;
                    mOldWidth = mNewWidth;
                    mOldHeight = mNewHeight;
                }
            }
        }
    }
}