capacitor-community-multilens-camerapreview 7.1.7 → 7.1.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.
@@ -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.Camera;
7
- import android.util.DisplayMetrics;
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
- import java.io.IOException;
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 mSurface;
25
- Camera.Size mPreviewSize;
26
- List<Camera.Size> mSupportedPreviewSizes;
27
- Camera mCamera;
28
- int cameraId;
29
+ SurfaceTexture mSurfaceTexture;
30
+ Size mPreviewSize;
31
+ List<Size> mSupportedPreviewSizes;
32
+ CameraDevice cameraDevice;
33
+ String cameraId;
29
34
  int displayOrientation;
30
- int facing = Camera.CameraInfo.CAMERA_FACING_BACK;
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(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);
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
- 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;
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
- 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;
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
- 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());
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.width;
207
- previewHeight = mPreviewSize.height;
162
+ previewWidth = mPreviewSize.getWidth();
163
+ previewHeight = mPreviewSize.getHeight();
208
164
 
209
165
  if (displayOrientation == 90 || displayOrientation == 270) {
210
- previewWidth = mPreviewSize.height;
211
- previewHeight = mPreviewSize.width;
166
+ previewWidth = mPreviewSize.getHeight();
167
+ previewHeight = mPreviewSize.getWidth();
212
168
  }
213
169
  // LOG.d(TAG, "previewWidth:" + previewWidth + " previewHeight:" + previewHeight);
214
170
  }
@@ -245,146 +201,59 @@ 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
- if (sizes == null) {
280
- return null;
206
+ public void setOpacity(final float opacity) {
207
+ this.opacity = opacity;
208
+ if (enableOpacity) {
209
+ mTextureView.setAlpha(opacity);
281
210
  }
211
+ }
282
212
 
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);
213
+ public Surface getSurface() {
214
+ if (!enableOpacity) {
215
+ return mHolder.getSurface();
216
+ } else {
217
+ if (mSurfaceTexture != null) {
218
+ return new Surface(mSurfaceTexture);
219
+ } else {
220
+ return null;
295
221
  }
296
222
  }
223
+ }
297
224
 
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
- }
225
+ @Override
226
+ public void surfaceCreated(@NonNull SurfaceHolder holder) {
308
227
 
309
- Log.d(TAG, "optimal preview size: w: " + optimalSize.width + " h: " + optimalSize.height);
310
- return optimalSize;
311
228
  }
312
229
 
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
- }
230
+ @Override
231
+ public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
328
232
 
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
233
  }
337
234
 
338
- // Texture Callbacks
235
+ @Override
236
+ public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
339
237
 
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
238
  }
357
239
 
358
- public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}
240
+ @Override
241
+ public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
359
242
 
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
243
  }
371
244
 
372
- public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
245
+ @Override
246
+ public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
373
247
 
374
- public void setOneShotPreviewCallback(Camera.PreviewCallback callback) {
375
- if (mCamera != null) {
376
- mCamera.setOneShotPreviewCallback(callback);
377
- }
378
248
  }
379
249
 
380
- public void setOpacity(final float opacity) {
381
- this.opacity = opacity;
382
- if (mCamera != null && enableOpacity) {
383
- mTextureView.setAlpha(opacity);
384
- }
250
+ @Override
251
+ public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
252
+ return false;
385
253
  }
386
254
 
387
- public Surface getSurface() {
388
- return null;
255
+ @Override
256
+ public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
257
+
389
258
  }
390
259
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-community-multilens-camerapreview",
3
- "version": "7.1.7",
3
+ "version": "7.1.9",
4
4
  "description": "fork of capacitor community camera preview with support for switchting lenses",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",