capacitor-community-multilens-camerapreview 7.1.7 → 7.1.8
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.
|
@@ -3,15 +3,20 @@ package com.ahm.capacitor.camera.preview;
|
|
|
3
3
|
import android.app.Activity;
|
|
4
4
|
import android.content.Context;
|
|
5
5
|
import android.graphics.SurfaceTexture;
|
|
6
|
-
import android.hardware.
|
|
7
|
-
import android.
|
|
6
|
+
import android.hardware.camera2.CameraAccessException;
|
|
7
|
+
import android.hardware.camera2.CameraCharacteristics;
|
|
8
|
+
import android.hardware.camera2.CameraDevice;
|
|
9
|
+
import android.hardware.camera2.CameraManager;
|
|
8
10
|
import android.util.Log;
|
|
11
|
+
import android.util.Size;
|
|
9
12
|
import android.view.Surface;
|
|
10
13
|
import android.view.SurfaceHolder;
|
|
11
14
|
import android.view.TextureView;
|
|
12
15
|
import android.view.View;
|
|
13
16
|
import android.widget.RelativeLayout;
|
|
14
|
-
|
|
17
|
+
|
|
18
|
+
import androidx.annotation.NonNull;
|
|
19
|
+
|
|
15
20
|
import java.util.List;
|
|
16
21
|
|
|
17
22
|
class Preview extends RelativeLayout implements SurfaceHolder.Callback, TextureView.SurfaceTextureListener {
|
|
@@ -21,13 +26,13 @@ class Preview extends RelativeLayout implements SurfaceHolder.Callback, TextureV
|
|
|
21
26
|
CustomSurfaceView mSurfaceView;
|
|
22
27
|
CustomTextureView mTextureView;
|
|
23
28
|
SurfaceHolder mHolder;
|
|
24
|
-
SurfaceTexture
|
|
25
|
-
|
|
26
|
-
List<
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
SurfaceTexture mSurfaceTexture;
|
|
30
|
+
Size mPreviewSize;
|
|
31
|
+
List<Size> mSupportedPreviewSizes;
|
|
32
|
+
CameraDevice cameraDevice;
|
|
33
|
+
String cameraId;
|
|
29
34
|
int displayOrientation;
|
|
30
|
-
int facing
|
|
35
|
+
int facing;
|
|
31
36
|
int viewWidth;
|
|
32
37
|
int viewHeight;
|
|
33
38
|
private boolean enableOpacity = false;
|
|
@@ -61,24 +66,10 @@ class Preview extends RelativeLayout implements SurfaceHolder.Callback, TextureV
|
|
|
61
66
|
}
|
|
62
67
|
}
|
|
63
68
|
|
|
64
|
-
public void setCamera(
|
|
65
|
-
if (
|
|
66
|
-
|
|
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);
|
|
69
|
+
public void setCamera(CameraDevice cameraDevice, int cameraId) {
|
|
70
|
+
if (cameraDevice != null) {
|
|
71
|
+
this.cameraDevice = cameraDevice;
|
|
72
|
+
this.cameraId = String.valueOf(cameraId);
|
|
82
73
|
}
|
|
83
74
|
}
|
|
84
75
|
|
|
@@ -90,93 +81,58 @@ class Preview extends RelativeLayout implements SurfaceHolder.Callback, TextureV
|
|
|
90
81
|
return facing;
|
|
91
82
|
}
|
|
92
83
|
|
|
93
|
-
public void printPreviewSize(String from) {
|
|
94
|
-
Log.d(TAG, "printPreviewSize from " + from + ": > width: " + mPreviewSize.width + " height: " + mPreviewSize.height);
|
|
95
|
-
}
|
|
96
84
|
|
|
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
85
|
|
|
105
86
|
public void setCameraDisplayOrientation() {
|
|
106
|
-
|
|
107
|
-
|
|
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;
|
|
87
|
+
if (cameraDevice == null || cameraId == null) {
|
|
88
|
+
return;
|
|
134
89
|
}
|
|
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) {
|
|
90
|
+
CameraManager cameraManager = (CameraManager) getContext().getSystemService(Context.CAMERA_SERVICE);
|
|
149
91
|
try {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
92
|
+
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
|
|
93
|
+
int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
|
|
94
|
+
int rotation = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getRotation();
|
|
95
|
+
int degrees = 0;
|
|
96
|
+
switch (rotation) {
|
|
97
|
+
case Surface.ROTATION_0:
|
|
98
|
+
degrees = 0;
|
|
99
|
+
break;
|
|
100
|
+
case Surface.ROTATION_90:
|
|
101
|
+
degrees = 90;
|
|
102
|
+
break;
|
|
103
|
+
case Surface.ROTATION_180:
|
|
104
|
+
degrees = 180;
|
|
105
|
+
break;
|
|
106
|
+
case Surface.ROTATION_270:
|
|
107
|
+
degrees = 270;
|
|
108
|
+
break;
|
|
161
109
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
110
|
+
int facing = characteristics.get(CameraCharacteristics.LENS_FACING);
|
|
111
|
+
if (facing == CameraCharacteristics.LENS_FACING_FRONT) {
|
|
112
|
+
displayOrientation = (sensorOrientation + degrees) % 360;
|
|
113
|
+
displayOrientation = (360 - displayOrientation) % 360; // compensate for mirror
|
|
114
|
+
} else { // back-facing
|
|
115
|
+
displayOrientation = (sensorOrientation - degrees + 360) % 360;
|
|
116
|
+
}
|
|
117
|
+
Log.d(TAG, "screen is rotated " + degrees + "deg from natural");
|
|
118
|
+
Log.d(
|
|
119
|
+
TAG,
|
|
120
|
+
(facing == CameraCharacteristics.LENS_FACING_FRONT ? "front" : "back") +
|
|
121
|
+
" camera is oriented -" +
|
|
122
|
+
sensorOrientation +
|
|
123
|
+
"deg from natural"
|
|
124
|
+
);
|
|
125
|
+
Log.d(TAG, "need to rotate preview " + displayOrientation + "deg");
|
|
126
|
+
} catch (CameraAccessException e) {
|
|
127
|
+
Log.e(TAG, "Failed to get camera characteristics: " + e.getMessage());
|
|
177
128
|
}
|
|
178
129
|
}
|
|
179
130
|
|
|
131
|
+
public void switchCamera(CameraDevice cameraDevice, String cameraId) {
|
|
132
|
+
setCamera(cameraDevice, Integer.parseInt(cameraId));
|
|
133
|
+
setCameraDisplayOrientation();
|
|
134
|
+
}
|
|
135
|
+
|
|
180
136
|
@Override
|
|
181
137
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
182
138
|
// We purposely disregard child measurements because act as a
|
|
@@ -187,7 +143,7 @@ class Preview extends RelativeLayout implements SurfaceHolder.Callback, TextureV
|
|
|
187
143
|
setMeasuredDimension(width, height);
|
|
188
144
|
|
|
189
145
|
if (mSupportedPreviewSizes != null) {
|
|
190
|
-
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
|
|
146
|
+
// mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
|
|
191
147
|
}
|
|
192
148
|
}
|
|
193
149
|
|
|
@@ -203,12 +159,12 @@ class Preview extends RelativeLayout implements SurfaceHolder.Callback, TextureV
|
|
|
203
159
|
int previewHeight = height;
|
|
204
160
|
|
|
205
161
|
if (mPreviewSize != null) {
|
|
206
|
-
previewWidth = mPreviewSize.
|
|
207
|
-
previewHeight = mPreviewSize.
|
|
162
|
+
previewWidth = mPreviewSize.getWidth();
|
|
163
|
+
previewHeight = mPreviewSize.getHeight();
|
|
208
164
|
|
|
209
165
|
if (displayOrientation == 90 || displayOrientation == 270) {
|
|
210
|
-
previewWidth = mPreviewSize.
|
|
211
|
-
previewHeight = mPreviewSize.
|
|
166
|
+
previewWidth = mPreviewSize.getHeight();
|
|
167
|
+
previewHeight = mPreviewSize.getWidth();
|
|
212
168
|
}
|
|
213
169
|
// LOG.d(TAG, "previewWidth:" + previewWidth + " previewHeight:" + previewHeight);
|
|
214
170
|
}
|
|
@@ -245,146 +201,51 @@ class Preview extends RelativeLayout implements SurfaceHolder.Callback, TextureV
|
|
|
245
201
|
}
|
|
246
202
|
}
|
|
247
203
|
|
|
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
204
|
|
|
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
205
|
|
|
279
|
-
|
|
280
|
-
|
|
206
|
+
public void setOpacity(final float opacity) {
|
|
207
|
+
this.opacity = opacity;
|
|
208
|
+
if (enableOpacity) {
|
|
209
|
+
mTextureView.setAlpha(opacity);
|
|
281
210
|
}
|
|
211
|
+
}
|
|
282
212
|
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
}
|
|
213
|
+
public Surface getSurface() {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
297
216
|
|
|
298
|
-
|
|
299
|
-
|
|
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
|
-
}
|
|
217
|
+
@Override
|
|
218
|
+
public void surfaceCreated(@NonNull SurfaceHolder holder) {
|
|
308
219
|
|
|
309
|
-
Log.d(TAG, "optimal preview size: w: " + optimalSize.width + " h: " + optimalSize.height);
|
|
310
|
-
return optimalSize;
|
|
311
220
|
}
|
|
312
221
|
|
|
313
|
-
|
|
314
|
-
|
|
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
|
-
}
|
|
222
|
+
@Override
|
|
223
|
+
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
|
|
328
224
|
|
|
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
225
|
}
|
|
337
226
|
|
|
338
|
-
|
|
227
|
+
@Override
|
|
228
|
+
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
|
|
339
229
|
|
|
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
230
|
}
|
|
357
231
|
|
|
358
|
-
|
|
232
|
+
@Override
|
|
233
|
+
public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
|
|
359
234
|
|
|
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
235
|
}
|
|
371
236
|
|
|
372
|
-
|
|
237
|
+
@Override
|
|
238
|
+
public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
|
|
373
239
|
|
|
374
|
-
public void setOneShotPreviewCallback(Camera.PreviewCallback callback) {
|
|
375
|
-
if (mCamera != null) {
|
|
376
|
-
mCamera.setOneShotPreviewCallback(callback);
|
|
377
|
-
}
|
|
378
240
|
}
|
|
379
241
|
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
mTextureView.setAlpha(opacity);
|
|
384
|
-
}
|
|
242
|
+
@Override
|
|
243
|
+
public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
|
|
244
|
+
return false;
|
|
385
245
|
}
|
|
386
246
|
|
|
387
|
-
|
|
388
|
-
|
|
247
|
+
@Override
|
|
248
|
+
public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
|
|
249
|
+
|
|
389
250
|
}
|
|
390
251
|
}
|
package/package.json
CHANGED