capacitor-community-multilens-camerapreview 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapacitorCommunityMultilensCamerapreview.podspec +17 -17
- package/README.md +16 -16
- package/android/build.gradle +55 -55
- package/android/src/main/AndroidManifest.xml +4 -4
- package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraActivity.java +1008 -1008
- package/android/src/main/java/com/ahm/capacitor/camera/preview/CameraPreview.java +544 -544
- package/android/src/main/java/com/ahm/capacitor/camera/preview/CustomSurfaceView.java +23 -23
- package/android/src/main/java/com/ahm/capacitor/camera/preview/CustomTextureView.java +29 -29
- package/android/src/main/java/com/ahm/capacitor/camera/preview/Preview.java +386 -386
- package/android/src/main/java/com/ahm/capacitor/camera/preview/TapGestureDetector.java +24 -24
- package/android/src/main/res/layout/bridge_layout_main.xml +15 -15
- package/android/src/main/res/layout/camera_activity.xml +68 -68
- package/android/src/main/res/values/camera_ids.xml +4 -4
- package/android/src/main/res/values/camera_theme.xml +9 -9
- package/android/src/main/res/values/colors.xml +3 -3
- package/android/src/main/res/values/strings.xml +3 -3
- package/android/src/main/res/values/styles.xml +3 -3
- package/dist/docs.json +1 -1
- package/dist/esm/definitions.d.ts +80 -80
- package/dist/esm/definitions.js +1 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/index.d.ts +4 -4
- package/dist/esm/index.js +6 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +28 -28
- package/dist/esm/web.js +155 -155
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +149 -149
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +149 -149
- package/dist/plugin.js.map +1 -1
- package/ios/Plugin/CameraController.swift +720 -720
- package/ios/Plugin/Info.plist +24 -24
- package/ios/Plugin/Plugin.h +10 -10
- package/ios/Plugin/Plugin.m +18 -18
- package/ios/Plugin/Plugin.swift +308 -308
- package/package.json +78 -78
|
@@ -1,386 +1,386 @@
|
|
|
1
|
-
package com.ahm.capacitor.camera.preview;
|
|
2
|
-
|
|
3
|
-
import android.app.Activity;
|
|
4
|
-
import android.content.Context;
|
|
5
|
-
import android.graphics.SurfaceTexture;
|
|
6
|
-
import android.hardware.Camera;
|
|
7
|
-
import android.util.DisplayMetrics;
|
|
8
|
-
import android.util.Log;
|
|
9
|
-
import android.view.Surface;
|
|
10
|
-
import android.view.SurfaceHolder;
|
|
11
|
-
import android.view.TextureView;
|
|
12
|
-
import android.view.View;
|
|
13
|
-
import android.widget.RelativeLayout;
|
|
14
|
-
import java.io.IOException;
|
|
15
|
-
import java.util.List;
|
|
16
|
-
|
|
17
|
-
class Preview extends RelativeLayout implements SurfaceHolder.Callback, TextureView.SurfaceTextureListener {
|
|
18
|
-
|
|
19
|
-
private final String TAG = "Preview";
|
|
20
|
-
|
|
21
|
-
CustomSurfaceView mSurfaceView;
|
|
22
|
-
CustomTextureView mTextureView;
|
|
23
|
-
SurfaceHolder mHolder;
|
|
24
|
-
SurfaceTexture mSurface;
|
|
25
|
-
Camera.Size mPreviewSize;
|
|
26
|
-
List<Camera.Size> mSupportedPreviewSizes;
|
|
27
|
-
Camera mCamera;
|
|
28
|
-
int cameraId;
|
|
29
|
-
int displayOrientation;
|
|
30
|
-
int facing = Camera.CameraInfo.CAMERA_FACING_BACK;
|
|
31
|
-
int viewWidth;
|
|
32
|
-
int viewHeight;
|
|
33
|
-
private boolean enableOpacity = false;
|
|
34
|
-
private float opacity = 1F;
|
|
35
|
-
|
|
36
|
-
Preview(Context context) {
|
|
37
|
-
this(context, false);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
Preview(Context context, boolean enableOpacity) {
|
|
41
|
-
super(context);
|
|
42
|
-
this.enableOpacity = enableOpacity;
|
|
43
|
-
if (!enableOpacity) {
|
|
44
|
-
mSurfaceView = new CustomSurfaceView(context);
|
|
45
|
-
addView(mSurfaceView);
|
|
46
|
-
requestLayout();
|
|
47
|
-
|
|
48
|
-
// Install a SurfaceHolder.Callback so we get notified when the
|
|
49
|
-
// underlying surface is created and destroyed.
|
|
50
|
-
mHolder = mSurfaceView.getHolder();
|
|
51
|
-
mHolder.addCallback(this);
|
|
52
|
-
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
|
53
|
-
} else {
|
|
54
|
-
// Use a TextureView so we can manage opacity
|
|
55
|
-
mTextureView = new CustomTextureView(context);
|
|
56
|
-
// Install a SurfaceTextureListener so we get notified
|
|
57
|
-
mTextureView.setSurfaceTextureListener(this);
|
|
58
|
-
mTextureView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
|
59
|
-
addView(mTextureView);
|
|
60
|
-
requestLayout();
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public void setCamera(Camera camera, int cameraId) {
|
|
65
|
-
if (camera != null) {
|
|
66
|
-
mCamera = camera;
|
|
67
|
-
this.cameraId = cameraId;
|
|
68
|
-
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
|
|
69
|
-
setCameraDisplayOrientation();
|
|
70
|
-
|
|
71
|
-
List<String> mFocusModes = mCamera.getParameters().getSupportedFocusModes();
|
|
72
|
-
|
|
73
|
-
Camera.Parameters params = mCamera.getParameters();
|
|
74
|
-
if (mFocusModes.contains("continuous-picture")) {
|
|
75
|
-
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
|
|
76
|
-
} else if (mFocusModes.contains("continuous-video")) {
|
|
77
|
-
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
|
78
|
-
} else if (mFocusModes.contains("auto")) {
|
|
79
|
-
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
|
|
80
|
-
}
|
|
81
|
-
mCamera.setParameters(params);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
public int getDisplayOrientation() {
|
|
86
|
-
return displayOrientation;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public int getCameraFacing() {
|
|
90
|
-
return facing;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public void printPreviewSize(String from) {
|
|
94
|
-
Log.d(TAG, "printPreviewSize from " + from + ": > width: " + mPreviewSize.width + " height: " + mPreviewSize.height);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public void setCameraPreviewSize() {
|
|
98
|
-
if (mCamera != null) {
|
|
99
|
-
Camera.Parameters parameters = mCamera.getParameters();
|
|
100
|
-
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
|
|
101
|
-
mCamera.setParameters(parameters);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public void setCameraDisplayOrientation() {
|
|
106
|
-
Camera.CameraInfo info = new Camera.CameraInfo();
|
|
107
|
-
int rotation = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getRotation();
|
|
108
|
-
int degrees = 0;
|
|
109
|
-
DisplayMetrics dm = new DisplayMetrics();
|
|
110
|
-
|
|
111
|
-
Camera.getCameraInfo(cameraId, info);
|
|
112
|
-
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
|
|
113
|
-
|
|
114
|
-
switch (rotation) {
|
|
115
|
-
case Surface.ROTATION_0:
|
|
116
|
-
degrees = 0;
|
|
117
|
-
break;
|
|
118
|
-
case Surface.ROTATION_90:
|
|
119
|
-
degrees = 90;
|
|
120
|
-
break;
|
|
121
|
-
case Surface.ROTATION_180:
|
|
122
|
-
degrees = 180;
|
|
123
|
-
break;
|
|
124
|
-
case Surface.ROTATION_270:
|
|
125
|
-
degrees = 270;
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
facing = info.facing;
|
|
129
|
-
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
130
|
-
displayOrientation = (info.orientation + degrees) % 360;
|
|
131
|
-
displayOrientation = (360 - displayOrientation) % 360;
|
|
132
|
-
} else {
|
|
133
|
-
displayOrientation = (info.orientation - degrees + 360) % 360;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
Log.d(TAG, "screen is rotated " + degrees + "deg from natural");
|
|
137
|
-
Log.d(
|
|
138
|
-
TAG,
|
|
139
|
-
(info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ? "front" : "back") +
|
|
140
|
-
" camera is oriented -" +
|
|
141
|
-
info.orientation +
|
|
142
|
-
"deg from natural"
|
|
143
|
-
);
|
|
144
|
-
Log.d(TAG, "need to rotate preview " + displayOrientation + "deg");
|
|
145
|
-
mCamera.setDisplayOrientation(displayOrientation);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
public void switchCamera(Camera camera, int cameraId) {
|
|
149
|
-
try {
|
|
150
|
-
setCamera(camera, cameraId);
|
|
151
|
-
|
|
152
|
-
Log.d("CameraPreview", "before set camera");
|
|
153
|
-
|
|
154
|
-
View v;
|
|
155
|
-
if (enableOpacity) {
|
|
156
|
-
camera.setPreviewTexture(mSurface);
|
|
157
|
-
v = mTextureView;
|
|
158
|
-
} else {
|
|
159
|
-
camera.setPreviewDisplay(mHolder);
|
|
160
|
-
v = mSurfaceView;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
Log.d("CameraPreview", "before getParameters");
|
|
164
|
-
|
|
165
|
-
Camera.Parameters parameters = camera.getParameters();
|
|
166
|
-
|
|
167
|
-
Log.d("CameraPreview", "before setPreviewSize");
|
|
168
|
-
|
|
169
|
-
mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
|
|
170
|
-
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, v.getWidth(), v.getHeight());
|
|
171
|
-
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
|
|
172
|
-
Log.d(TAG, mPreviewSize.width + " " + mPreviewSize.height);
|
|
173
|
-
|
|
174
|
-
camera.setParameters(parameters);
|
|
175
|
-
} catch (IOException exception) {
|
|
176
|
-
Log.e(TAG, exception.getMessage());
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
@Override
|
|
181
|
-
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
182
|
-
// We purposely disregard child measurements because act as a
|
|
183
|
-
// wrapper to a SurfaceView that centers the camera preview instead
|
|
184
|
-
// of stretching it.
|
|
185
|
-
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
|
|
186
|
-
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
|
|
187
|
-
setMeasuredDimension(width, height);
|
|
188
|
-
|
|
189
|
-
if (mSupportedPreviewSizes != null) {
|
|
190
|
-
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
@Override
|
|
195
|
-
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
|
196
|
-
if (changed && getChildCount() > 0) {
|
|
197
|
-
final View child = getChildAt(0);
|
|
198
|
-
|
|
199
|
-
int width = r - l;
|
|
200
|
-
int height = b - t;
|
|
201
|
-
|
|
202
|
-
int previewWidth = width;
|
|
203
|
-
int previewHeight = height;
|
|
204
|
-
|
|
205
|
-
if (mPreviewSize != null) {
|
|
206
|
-
previewWidth = mPreviewSize.width;
|
|
207
|
-
previewHeight = mPreviewSize.height;
|
|
208
|
-
|
|
209
|
-
if (displayOrientation == 90 || displayOrientation == 270) {
|
|
210
|
-
previewWidth = mPreviewSize.height;
|
|
211
|
-
previewHeight = mPreviewSize.width;
|
|
212
|
-
}
|
|
213
|
-
// LOG.d(TAG, "previewWidth:" + previewWidth + " previewHeight:" + previewHeight);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
int nW;
|
|
217
|
-
int nH;
|
|
218
|
-
int top;
|
|
219
|
-
int left;
|
|
220
|
-
|
|
221
|
-
float scale = 1.0f;
|
|
222
|
-
|
|
223
|
-
// Center the child SurfaceView within the parent.
|
|
224
|
-
if (width * previewHeight < height * previewWidth) {
|
|
225
|
-
Log.d(TAG, "center horizontally");
|
|
226
|
-
int scaledChildWidth = (int) ((previewWidth * height / previewHeight) * scale);
|
|
227
|
-
nW = (width + scaledChildWidth) / 2;
|
|
228
|
-
nH = (int) (height * scale);
|
|
229
|
-
top = 0;
|
|
230
|
-
left = (width - scaledChildWidth) / 2;
|
|
231
|
-
} else {
|
|
232
|
-
Log.d(TAG, "center vertically");
|
|
233
|
-
int scaledChildHeight = (int) ((previewHeight * width / previewWidth) * scale);
|
|
234
|
-
nW = (int) (width * scale);
|
|
235
|
-
nH = (height + scaledChildHeight) / 2;
|
|
236
|
-
top = (height - scaledChildHeight) / 2;
|
|
237
|
-
left = 0;
|
|
238
|
-
}
|
|
239
|
-
child.layout(left, top, nW, nH);
|
|
240
|
-
|
|
241
|
-
Log.d("layout", "left:" + left);
|
|
242
|
-
Log.d("layout", "top:" + top);
|
|
243
|
-
Log.d("layout", "right:" + nW);
|
|
244
|
-
Log.d("layout", "bottom:" + nH);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
public void surfaceCreated(SurfaceHolder holder) {
|
|
249
|
-
// The Surface has been created, acquire the camera and tell it where
|
|
250
|
-
// to draw.
|
|
251
|
-
try {
|
|
252
|
-
if (mCamera != null) {
|
|
253
|
-
mSurfaceView.setWillNotDraw(false);
|
|
254
|
-
mCamera.setPreviewDisplay(holder);
|
|
255
|
-
}
|
|
256
|
-
} catch (Exception exception) {
|
|
257
|
-
Log.e(TAG, "Exception caused by setPreviewDisplay()", exception);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
public void surfaceDestroyed(SurfaceHolder holder) {
|
|
262
|
-
// Surface will be destroyed when we return, so stop the preview.
|
|
263
|
-
try {
|
|
264
|
-
if (mCamera != null) {
|
|
265
|
-
mCamera.stopPreview();
|
|
266
|
-
}
|
|
267
|
-
} catch (Exception exception) {
|
|
268
|
-
Log.e(TAG, "Exception caused by surfaceDestroyed()", exception);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
|
|
273
|
-
final double ASPECT_TOLERANCE = 0.1;
|
|
274
|
-
double targetRatio = (double) w / h;
|
|
275
|
-
if (displayOrientation == 90 || displayOrientation == 270) {
|
|
276
|
-
targetRatio = (double) h / w;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
if (sizes == null) {
|
|
280
|
-
return null;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
Camera.Size optimalSize = null;
|
|
284
|
-
double minDiff = Double.MAX_VALUE;
|
|
285
|
-
|
|
286
|
-
int targetHeight = h;
|
|
287
|
-
|
|
288
|
-
// Try to find an size match aspect ratio and size
|
|
289
|
-
for (Camera.Size size : sizes) {
|
|
290
|
-
double ratio = (double) size.width / size.height;
|
|
291
|
-
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
|
|
292
|
-
if (Math.abs(size.height - targetHeight) < minDiff) {
|
|
293
|
-
optimalSize = size;
|
|
294
|
-
minDiff = Math.abs(size.height - targetHeight);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// Cannot find the one match the aspect ratio, ignore the requirement
|
|
299
|
-
if (optimalSize == null) {
|
|
300
|
-
minDiff = Double.MAX_VALUE;
|
|
301
|
-
for (Camera.Size size : sizes) {
|
|
302
|
-
if (Math.abs(size.height - targetHeight) < minDiff) {
|
|
303
|
-
optimalSize = size;
|
|
304
|
-
minDiff = Math.abs(size.height - targetHeight);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
Log.d(TAG, "optimal preview size: w: " + optimalSize.width + " h: " + optimalSize.height);
|
|
310
|
-
return optimalSize;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
|
|
314
|
-
if (mCamera != null) {
|
|
315
|
-
try {
|
|
316
|
-
// Now that the size is known, set up the camera parameters and begin
|
|
317
|
-
// the preview.
|
|
318
|
-
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
|
|
319
|
-
if (mSupportedPreviewSizes != null) {
|
|
320
|
-
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, w, h);
|
|
321
|
-
}
|
|
322
|
-
startCamera();
|
|
323
|
-
} catch (Exception exception) {
|
|
324
|
-
Log.e(TAG, "Exception caused by surfaceChanged()", exception);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
private void startCamera() {
|
|
330
|
-
Camera.Parameters parameters = mCamera.getParameters();
|
|
331
|
-
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
|
|
332
|
-
requestLayout();
|
|
333
|
-
//mCamera.setDisplayOrientation(90);
|
|
334
|
-
mCamera.setParameters(parameters);
|
|
335
|
-
mCamera.startPreview();
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
// Texture Callbacks
|
|
339
|
-
|
|
340
|
-
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
|
|
341
|
-
// The Surface has been created, acquire the camera and tell it where
|
|
342
|
-
// to draw.
|
|
343
|
-
try {
|
|
344
|
-
mSurface = surface;
|
|
345
|
-
if (mSupportedPreviewSizes != null) {
|
|
346
|
-
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
|
|
347
|
-
}
|
|
348
|
-
if (mCamera != null) {
|
|
349
|
-
mTextureView.setAlpha(opacity);
|
|
350
|
-
mCamera.setPreviewTexture(surface);
|
|
351
|
-
startCamera();
|
|
352
|
-
}
|
|
353
|
-
} catch (Exception exception) {
|
|
354
|
-
Log.e(TAG, "Exception caused by onSurfaceTextureAvailable()", exception);
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}
|
|
359
|
-
|
|
360
|
-
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
|
|
361
|
-
try {
|
|
362
|
-
if (mCamera != null) {
|
|
363
|
-
mCamera.stopPreview();
|
|
364
|
-
}
|
|
365
|
-
} catch (Exception exception) {
|
|
366
|
-
Log.e(TAG, "Exception caused by onSurfaceTextureDestroyed()", exception);
|
|
367
|
-
return false;
|
|
368
|
-
}
|
|
369
|
-
return true;
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
|
|
373
|
-
|
|
374
|
-
public void setOneShotPreviewCallback(Camera.PreviewCallback callback) {
|
|
375
|
-
if (mCamera != null) {
|
|
376
|
-
mCamera.setOneShotPreviewCallback(callback);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
public void setOpacity(final float opacity) {
|
|
381
|
-
this.opacity = opacity;
|
|
382
|
-
if (mCamera != null && enableOpacity) {
|
|
383
|
-
mTextureView.setAlpha(opacity);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
}
|
|
1
|
+
package com.ahm.capacitor.camera.preview;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.content.Context;
|
|
5
|
+
import android.graphics.SurfaceTexture;
|
|
6
|
+
import android.hardware.Camera;
|
|
7
|
+
import android.util.DisplayMetrics;
|
|
8
|
+
import android.util.Log;
|
|
9
|
+
import android.view.Surface;
|
|
10
|
+
import android.view.SurfaceHolder;
|
|
11
|
+
import android.view.TextureView;
|
|
12
|
+
import android.view.View;
|
|
13
|
+
import android.widget.RelativeLayout;
|
|
14
|
+
import java.io.IOException;
|
|
15
|
+
import java.util.List;
|
|
16
|
+
|
|
17
|
+
class Preview extends RelativeLayout implements SurfaceHolder.Callback, TextureView.SurfaceTextureListener {
|
|
18
|
+
|
|
19
|
+
private final String TAG = "Preview";
|
|
20
|
+
|
|
21
|
+
CustomSurfaceView mSurfaceView;
|
|
22
|
+
CustomTextureView mTextureView;
|
|
23
|
+
SurfaceHolder mHolder;
|
|
24
|
+
SurfaceTexture mSurface;
|
|
25
|
+
Camera.Size mPreviewSize;
|
|
26
|
+
List<Camera.Size> mSupportedPreviewSizes;
|
|
27
|
+
Camera mCamera;
|
|
28
|
+
int cameraId;
|
|
29
|
+
int displayOrientation;
|
|
30
|
+
int facing = Camera.CameraInfo.CAMERA_FACING_BACK;
|
|
31
|
+
int viewWidth;
|
|
32
|
+
int viewHeight;
|
|
33
|
+
private boolean enableOpacity = false;
|
|
34
|
+
private float opacity = 1F;
|
|
35
|
+
|
|
36
|
+
Preview(Context context) {
|
|
37
|
+
this(context, false);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
Preview(Context context, boolean enableOpacity) {
|
|
41
|
+
super(context);
|
|
42
|
+
this.enableOpacity = enableOpacity;
|
|
43
|
+
if (!enableOpacity) {
|
|
44
|
+
mSurfaceView = new CustomSurfaceView(context);
|
|
45
|
+
addView(mSurfaceView);
|
|
46
|
+
requestLayout();
|
|
47
|
+
|
|
48
|
+
// Install a SurfaceHolder.Callback so we get notified when the
|
|
49
|
+
// underlying surface is created and destroyed.
|
|
50
|
+
mHolder = mSurfaceView.getHolder();
|
|
51
|
+
mHolder.addCallback(this);
|
|
52
|
+
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
|
53
|
+
} else {
|
|
54
|
+
// Use a TextureView so we can manage opacity
|
|
55
|
+
mTextureView = new CustomTextureView(context);
|
|
56
|
+
// Install a SurfaceTextureListener so we get notified
|
|
57
|
+
mTextureView.setSurfaceTextureListener(this);
|
|
58
|
+
mTextureView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
|
|
59
|
+
addView(mTextureView);
|
|
60
|
+
requestLayout();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public void setCamera(Camera camera, int cameraId) {
|
|
65
|
+
if (camera != null) {
|
|
66
|
+
mCamera = camera;
|
|
67
|
+
this.cameraId = cameraId;
|
|
68
|
+
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
|
|
69
|
+
setCameraDisplayOrientation();
|
|
70
|
+
|
|
71
|
+
List<String> mFocusModes = mCamera.getParameters().getSupportedFocusModes();
|
|
72
|
+
|
|
73
|
+
Camera.Parameters params = mCamera.getParameters();
|
|
74
|
+
if (mFocusModes.contains("continuous-picture")) {
|
|
75
|
+
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
|
|
76
|
+
} else if (mFocusModes.contains("continuous-video")) {
|
|
77
|
+
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
|
|
78
|
+
} else if (mFocusModes.contains("auto")) {
|
|
79
|
+
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
|
|
80
|
+
}
|
|
81
|
+
mCamera.setParameters(params);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public int getDisplayOrientation() {
|
|
86
|
+
return displayOrientation;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public int getCameraFacing() {
|
|
90
|
+
return facing;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public void printPreviewSize(String from) {
|
|
94
|
+
Log.d(TAG, "printPreviewSize from " + from + ": > width: " + mPreviewSize.width + " height: " + mPreviewSize.height);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public void setCameraPreviewSize() {
|
|
98
|
+
if (mCamera != null) {
|
|
99
|
+
Camera.Parameters parameters = mCamera.getParameters();
|
|
100
|
+
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
|
|
101
|
+
mCamera.setParameters(parameters);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public void setCameraDisplayOrientation() {
|
|
106
|
+
Camera.CameraInfo info = new Camera.CameraInfo();
|
|
107
|
+
int rotation = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getRotation();
|
|
108
|
+
int degrees = 0;
|
|
109
|
+
DisplayMetrics dm = new DisplayMetrics();
|
|
110
|
+
|
|
111
|
+
Camera.getCameraInfo(cameraId, info);
|
|
112
|
+
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
|
|
113
|
+
|
|
114
|
+
switch (rotation) {
|
|
115
|
+
case Surface.ROTATION_0:
|
|
116
|
+
degrees = 0;
|
|
117
|
+
break;
|
|
118
|
+
case Surface.ROTATION_90:
|
|
119
|
+
degrees = 90;
|
|
120
|
+
break;
|
|
121
|
+
case Surface.ROTATION_180:
|
|
122
|
+
degrees = 180;
|
|
123
|
+
break;
|
|
124
|
+
case Surface.ROTATION_270:
|
|
125
|
+
degrees = 270;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
facing = info.facing;
|
|
129
|
+
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
|
|
130
|
+
displayOrientation = (info.orientation + degrees) % 360;
|
|
131
|
+
displayOrientation = (360 - displayOrientation) % 360;
|
|
132
|
+
} else {
|
|
133
|
+
displayOrientation = (info.orientation - degrees + 360) % 360;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
Log.d(TAG, "screen is rotated " + degrees + "deg from natural");
|
|
137
|
+
Log.d(
|
|
138
|
+
TAG,
|
|
139
|
+
(info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ? "front" : "back") +
|
|
140
|
+
" camera is oriented -" +
|
|
141
|
+
info.orientation +
|
|
142
|
+
"deg from natural"
|
|
143
|
+
);
|
|
144
|
+
Log.d(TAG, "need to rotate preview " + displayOrientation + "deg");
|
|
145
|
+
mCamera.setDisplayOrientation(displayOrientation);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public void switchCamera(Camera camera, int cameraId) {
|
|
149
|
+
try {
|
|
150
|
+
setCamera(camera, cameraId);
|
|
151
|
+
|
|
152
|
+
Log.d("CameraPreview", "before set camera");
|
|
153
|
+
|
|
154
|
+
View v;
|
|
155
|
+
if (enableOpacity) {
|
|
156
|
+
camera.setPreviewTexture(mSurface);
|
|
157
|
+
v = mTextureView;
|
|
158
|
+
} else {
|
|
159
|
+
camera.setPreviewDisplay(mHolder);
|
|
160
|
+
v = mSurfaceView;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
Log.d("CameraPreview", "before getParameters");
|
|
164
|
+
|
|
165
|
+
Camera.Parameters parameters = camera.getParameters();
|
|
166
|
+
|
|
167
|
+
Log.d("CameraPreview", "before setPreviewSize");
|
|
168
|
+
|
|
169
|
+
mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
|
|
170
|
+
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, v.getWidth(), v.getHeight());
|
|
171
|
+
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
|
|
172
|
+
Log.d(TAG, mPreviewSize.width + " " + mPreviewSize.height);
|
|
173
|
+
|
|
174
|
+
camera.setParameters(parameters);
|
|
175
|
+
} catch (IOException exception) {
|
|
176
|
+
Log.e(TAG, exception.getMessage());
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@Override
|
|
181
|
+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
182
|
+
// We purposely disregard child measurements because act as a
|
|
183
|
+
// wrapper to a SurfaceView that centers the camera preview instead
|
|
184
|
+
// of stretching it.
|
|
185
|
+
final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
|
|
186
|
+
final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
|
|
187
|
+
setMeasuredDimension(width, height);
|
|
188
|
+
|
|
189
|
+
if (mSupportedPreviewSizes != null) {
|
|
190
|
+
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
@Override
|
|
195
|
+
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
|
196
|
+
if (changed && getChildCount() > 0) {
|
|
197
|
+
final View child = getChildAt(0);
|
|
198
|
+
|
|
199
|
+
int width = r - l;
|
|
200
|
+
int height = b - t;
|
|
201
|
+
|
|
202
|
+
int previewWidth = width;
|
|
203
|
+
int previewHeight = height;
|
|
204
|
+
|
|
205
|
+
if (mPreviewSize != null) {
|
|
206
|
+
previewWidth = mPreviewSize.width;
|
|
207
|
+
previewHeight = mPreviewSize.height;
|
|
208
|
+
|
|
209
|
+
if (displayOrientation == 90 || displayOrientation == 270) {
|
|
210
|
+
previewWidth = mPreviewSize.height;
|
|
211
|
+
previewHeight = mPreviewSize.width;
|
|
212
|
+
}
|
|
213
|
+
// LOG.d(TAG, "previewWidth:" + previewWidth + " previewHeight:" + previewHeight);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
int nW;
|
|
217
|
+
int nH;
|
|
218
|
+
int top;
|
|
219
|
+
int left;
|
|
220
|
+
|
|
221
|
+
float scale = 1.0f;
|
|
222
|
+
|
|
223
|
+
// Center the child SurfaceView within the parent.
|
|
224
|
+
if (width * previewHeight < height * previewWidth) {
|
|
225
|
+
Log.d(TAG, "center horizontally");
|
|
226
|
+
int scaledChildWidth = (int) ((previewWidth * height / previewHeight) * scale);
|
|
227
|
+
nW = (width + scaledChildWidth) / 2;
|
|
228
|
+
nH = (int) (height * scale);
|
|
229
|
+
top = 0;
|
|
230
|
+
left = (width - scaledChildWidth) / 2;
|
|
231
|
+
} else {
|
|
232
|
+
Log.d(TAG, "center vertically");
|
|
233
|
+
int scaledChildHeight = (int) ((previewHeight * width / previewWidth) * scale);
|
|
234
|
+
nW = (int) (width * scale);
|
|
235
|
+
nH = (height + scaledChildHeight) / 2;
|
|
236
|
+
top = (height - scaledChildHeight) / 2;
|
|
237
|
+
left = 0;
|
|
238
|
+
}
|
|
239
|
+
child.layout(left, top, nW, nH);
|
|
240
|
+
|
|
241
|
+
Log.d("layout", "left:" + left);
|
|
242
|
+
Log.d("layout", "top:" + top);
|
|
243
|
+
Log.d("layout", "right:" + nW);
|
|
244
|
+
Log.d("layout", "bottom:" + nH);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
public void surfaceCreated(SurfaceHolder holder) {
|
|
249
|
+
// The Surface has been created, acquire the camera and tell it where
|
|
250
|
+
// to draw.
|
|
251
|
+
try {
|
|
252
|
+
if (mCamera != null) {
|
|
253
|
+
mSurfaceView.setWillNotDraw(false);
|
|
254
|
+
mCamera.setPreviewDisplay(holder);
|
|
255
|
+
}
|
|
256
|
+
} catch (Exception exception) {
|
|
257
|
+
Log.e(TAG, "Exception caused by setPreviewDisplay()", exception);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
public void surfaceDestroyed(SurfaceHolder holder) {
|
|
262
|
+
// Surface will be destroyed when we return, so stop the preview.
|
|
263
|
+
try {
|
|
264
|
+
if (mCamera != null) {
|
|
265
|
+
mCamera.stopPreview();
|
|
266
|
+
}
|
|
267
|
+
} catch (Exception exception) {
|
|
268
|
+
Log.e(TAG, "Exception caused by surfaceDestroyed()", exception);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
|
|
273
|
+
final double ASPECT_TOLERANCE = 0.1;
|
|
274
|
+
double targetRatio = (double) w / h;
|
|
275
|
+
if (displayOrientation == 90 || displayOrientation == 270) {
|
|
276
|
+
targetRatio = (double) h / w;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (sizes == null) {
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
Camera.Size optimalSize = null;
|
|
284
|
+
double minDiff = Double.MAX_VALUE;
|
|
285
|
+
|
|
286
|
+
int targetHeight = h;
|
|
287
|
+
|
|
288
|
+
// Try to find an size match aspect ratio and size
|
|
289
|
+
for (Camera.Size size : sizes) {
|
|
290
|
+
double ratio = (double) size.width / size.height;
|
|
291
|
+
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
|
|
292
|
+
if (Math.abs(size.height - targetHeight) < minDiff) {
|
|
293
|
+
optimalSize = size;
|
|
294
|
+
minDiff = Math.abs(size.height - targetHeight);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Cannot find the one match the aspect ratio, ignore the requirement
|
|
299
|
+
if (optimalSize == null) {
|
|
300
|
+
minDiff = Double.MAX_VALUE;
|
|
301
|
+
for (Camera.Size size : sizes) {
|
|
302
|
+
if (Math.abs(size.height - targetHeight) < minDiff) {
|
|
303
|
+
optimalSize = size;
|
|
304
|
+
minDiff = Math.abs(size.height - targetHeight);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
Log.d(TAG, "optimal preview size: w: " + optimalSize.width + " h: " + optimalSize.height);
|
|
310
|
+
return optimalSize;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
|
|
314
|
+
if (mCamera != null) {
|
|
315
|
+
try {
|
|
316
|
+
// Now that the size is known, set up the camera parameters and begin
|
|
317
|
+
// the preview.
|
|
318
|
+
mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
|
|
319
|
+
if (mSupportedPreviewSizes != null) {
|
|
320
|
+
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, w, h);
|
|
321
|
+
}
|
|
322
|
+
startCamera();
|
|
323
|
+
} catch (Exception exception) {
|
|
324
|
+
Log.e(TAG, "Exception caused by surfaceChanged()", exception);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private void startCamera() {
|
|
330
|
+
Camera.Parameters parameters = mCamera.getParameters();
|
|
331
|
+
parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
|
|
332
|
+
requestLayout();
|
|
333
|
+
//mCamera.setDisplayOrientation(90);
|
|
334
|
+
mCamera.setParameters(parameters);
|
|
335
|
+
mCamera.startPreview();
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Texture Callbacks
|
|
339
|
+
|
|
340
|
+
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
|
|
341
|
+
// The Surface has been created, acquire the camera and tell it where
|
|
342
|
+
// to draw.
|
|
343
|
+
try {
|
|
344
|
+
mSurface = surface;
|
|
345
|
+
if (mSupportedPreviewSizes != null) {
|
|
346
|
+
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
|
|
347
|
+
}
|
|
348
|
+
if (mCamera != null) {
|
|
349
|
+
mTextureView.setAlpha(opacity);
|
|
350
|
+
mCamera.setPreviewTexture(surface);
|
|
351
|
+
startCamera();
|
|
352
|
+
}
|
|
353
|
+
} catch (Exception exception) {
|
|
354
|
+
Log.e(TAG, "Exception caused by onSurfaceTextureAvailable()", exception);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}
|
|
359
|
+
|
|
360
|
+
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
|
|
361
|
+
try {
|
|
362
|
+
if (mCamera != null) {
|
|
363
|
+
mCamera.stopPreview();
|
|
364
|
+
}
|
|
365
|
+
} catch (Exception exception) {
|
|
366
|
+
Log.e(TAG, "Exception caused by onSurfaceTextureDestroyed()", exception);
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
return true;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
|
|
373
|
+
|
|
374
|
+
public void setOneShotPreviewCallback(Camera.PreviewCallback callback) {
|
|
375
|
+
if (mCamera != null) {
|
|
376
|
+
mCamera.setOneShotPreviewCallback(callback);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
public void setOpacity(final float opacity) {
|
|
381
|
+
this.opacity = opacity;
|
|
382
|
+
if (mCamera != null && enableOpacity) {
|
|
383
|
+
mTextureView.setAlpha(opacity);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|