Subversion Repository Public Repository

Nextrek

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
package nextrek.widgets;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.RelativeLayout;

/**
 * Represents the camera's surface and all the initialization involved with it.
 * 
 */
public class CameraSurface extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "CameraSurface";
    private static SurfaceHolder holder = null;
    private static Camera camera = null;

    public CameraSurface(Context context) {
        super(context);

        init(context);
    }

    public CameraSurface(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context);
    }

    private void init(Context context) {
        try {
            holder = getHolder();
            holder.addCallback(this);
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            if (camera != null) {
                try {
                    camera.stopPreview();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                try {
                    camera.release();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                camera = null;
            }

            camera = Camera.open();
            camera.setPreviewDisplay(holder);
        } catch (Exception ex) {
            try {
                if (camera != null) {
                    try {
                        camera.stopPreview();
                    } catch (Exception ex1) {
                        ex.printStackTrace();
                    }
                    try {
                        camera.release();
                    } catch (Exception ex2) {
                        ex.printStackTrace();
                    }
                    camera = null;
                }
            } catch (Exception ex3) {
                ex.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        try {
            if (camera != null) {
                try {
                    camera.stopPreview();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                try {
                    camera.release();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                camera = null;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

        try {
            Camera.Parameters parameters = camera.getParameters();
            int bestw = 0;
            int besth = 0;
            try {
                List<Camera.Size> supportedSizes = null;
                // On older devices (<1.6) the following will fail
                // the camera will work nevertheless
                supportedSizes = getSupportedPreviewSizes(parameters);

                // preview form factor
                float ff = (float) w / h;

                // holder for the best form factor and size
                float bff = 0;

                Iterator<Camera.Size> itr = supportedSizes.iterator();

                // we look for the best preview size, it has to be the closest to the
                // screen form factor, and be less wide than the screen itself
                // the latter requirement is because the HTC Hero with update 2.1 will
                // report camera preview sizes larger than the screen, and it will fail
                // to initialize the camera
                // other devices could work with previews larger than the screen though
                while (itr.hasNext()) {
                    Camera.Size element = itr.next();
                    Log.v(TAG, "preview size " + element.width + "x" + element.height);
                    // current form factor
                    float cff = (float) element.width / element.height;
                    // check if the current element is a candidate to replace the best match so far
                    // current form factor should be closer to the bff
                    // preview height should be less than screen height (otherwise preview will be distorted.. I dunno why!!!)
                    // preview height should be more than current besth
                    // this combination will ensure that the highest resolution will win
                    if ((ff - cff <= ff - bff) && (element.height < h) && (element.height >= besth)) {
                        bff = cff;
                        bestw = element.width;
                        besth = element.height;
                    }
                }

                Log.v(TAG, "choosen preview " + bestw + "x" + besth);
                // Some Samsung phones will end up with bestw and besth = 0 because their minimum preview size is bigger then the screen size.
                // In this case, we use the default values: 480x320
                if ((bestw == 0) || (besth == 0)) {
                    bestw = 352;
                    besth = 288;
                    Log.v(TAG, "forcing preview " + bestw + "x" + besth);
                }
            } catch (Exception ex) {
                Log.v(TAG, "error, forcing preview 352x288");
                    bestw = 352;
                    besth = 288;
            }

            parameters.setPreviewSize(bestw, besth);

            // Force minimun frame rate to save battery
            List<Integer> fps = parameters.getSupportedPreviewFrameRates();
            int minFps = fps.get(0);
            for (int i = 1; i < fps.size(); i++) {
                if (fps.get(i) < minFps) {
                    minFps = fps.get(i);
                }
            }
            parameters.setPreviewFrameRate(minFps);

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();

            int oldWidth = params.width;
            int oldHeight = params.height;
            
            // scale to fit y
            params.width = (int) (bestw * ((float) h / besth));
            params.height = h;
            
            if ((oldWidth != params.width) || (oldHeight != params.height)) {
                setLayoutParams(params);
                requestLayout();
            }

            camera.setParameters(parameters);
            camera.startPreview();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static Method getSupportedPreviewSizes = null;
    private static Method mDefaultDisplay_getRotation = null;

    static {
        initCompatibility();
    };

    private static void initCompatibility() {
        try {
            getSupportedPreviewSizes = Camera.Parameters.class.getMethod("getSupportedPreviewSizes", new Class[] {});
            mDefaultDisplay_getRotation = Display.class.getMethod("getRotation", new Class[] {});

        } catch (NoSuchMethodException nsme) {

        }
    }

    public static int getRotation(Activity activity) {
        int result = 1;
        try {
            Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            Object retObj = mDefaultDisplay_getRotation.invoke(display);
            if (retObj != null)
                result = (Integer) retObj;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    public static List<Camera.Size> getSupportedPreviewSizes(Camera.Parameters params) {
        List<Camera.Size> retList = null;

        try {
            Object retObj = getSupportedPreviewSizes.invoke(params);
            if (retObj != null) {
                retList = (List<Camera.Size>) retObj;
            }
        } catch (InvocationTargetException ite) {
            Throwable cause = ite.getCause();
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            } else if (cause instanceof Error) {
                throw (Error) cause;
            } else {
                throw new RuntimeException(ite);
            }
        } catch (IllegalAccessException ie) {
            ie.printStackTrace();
        }
        return retList;
    }
}

Commits for Nextrek/Android/LibrerieNextrek/src/nextrek/widgets/CameraSurface.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000