@capacitor-community/camera-preview 7.0.3 → 7.0.5

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.
@@ -160,151 +160,150 @@ public class CameraActivity extends Fragment {
160
160
  final GestureDetector gestureDetector = new GestureDetector(getActivity().getApplicationContext(), new TapGestureDetector());
161
161
 
162
162
  getActivity().runOnUiThread(
163
- new Runnable() {
164
- @Override
165
- public void run() {
166
- frameContainerLayout.setClickable(true);
167
- frameContainerLayout.setOnTouchListener(
168
- new View.OnTouchListener() {
169
- private int mLastTouchX;
170
- private int mLastTouchY;
171
- private int mPosX = 0;
172
- private int mPosY = 0;
173
-
174
- @Override
175
- public boolean onTouch(View v, MotionEvent event) {
176
- FrameLayout.LayoutParams layoutParams =
177
- (FrameLayout.LayoutParams) frameContainerLayout.getLayoutParams();
178
-
179
- boolean isSingleTapTouch = gestureDetector.onTouchEvent(event);
180
- int action = event.getAction();
181
- int eventCount = event.getPointerCount();
182
- Log.d(TAG, "onTouch event, action, count: " + event + ", " + action + ", " + eventCount);
183
- if (eventCount > 1) {
184
- // handle multi-touch events
185
- Camera.Parameters params = mCamera.getParameters();
186
- if (action == MotionEvent.ACTION_POINTER_DOWN) {
187
- mDist = getFingerSpacing(event);
188
- } else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
189
- handleZoom(event, params);
190
- }
191
- } else {
192
- if (action != MotionEvent.ACTION_MOVE && isSingleTapTouch) {
193
- if (tapToTakePicture && tapToFocus) {
194
- setFocusArea(
195
- (int) event.getX(0),
196
- (int) event.getY(0),
197
- new Camera.AutoFocusCallback() {
198
- public void onAutoFocus(boolean success, Camera camera) {
199
- if (success) {
200
- takePicture(0, 0, 85);
201
- } else {
202
- Log.d(TAG, "onTouch:" + " setFocusArea() did not suceed");
203
- }
204
- }
205
- }
206
- );
207
- } else if (tapToTakePicture) {
208
- takePicture(0, 0, 85);
209
- } else if (tapToFocus) {
210
- setFocusArea(
211
- (int) event.getX(0),
212
- (int) event.getY(0),
213
- new Camera.AutoFocusCallback() {
214
- public void onAutoFocus(boolean success, Camera camera) {
215
- if (success) {
216
- // A callback to JS might make sense here.
217
- } else {
218
- Log.d(TAG, "onTouch:" + " setFocusArea() did not suceed");
219
- }
163
+ new Runnable() {
164
+ @Override
165
+ public void run() {
166
+ frameContainerLayout.setClickable(true);
167
+ frameContainerLayout.setOnTouchListener(
168
+ new View.OnTouchListener() {
169
+ private int mLastTouchX;
170
+ private int mLastTouchY;
171
+ private int mPosX = 0;
172
+ private int mPosY = 0;
173
+
174
+ @Override
175
+ public boolean onTouch(View v, MotionEvent event) {
176
+ FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) frameContainerLayout.getLayoutParams();
177
+
178
+ boolean isSingleTapTouch = gestureDetector.onTouchEvent(event);
179
+ int action = event.getAction();
180
+ int eventCount = event.getPointerCount();
181
+ Log.d(TAG, "onTouch event, action, count: " + event + ", " + action + ", " + eventCount);
182
+ if (eventCount > 1) {
183
+ // handle multi-touch events
184
+ Camera.Parameters params = mCamera.getParameters();
185
+ if (action == MotionEvent.ACTION_POINTER_DOWN) {
186
+ mDist = getFingerSpacing(event);
187
+ } else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
188
+ handleZoom(event, params);
189
+ }
190
+ } else {
191
+ if (action != MotionEvent.ACTION_MOVE && isSingleTapTouch) {
192
+ if (tapToTakePicture && tapToFocus) {
193
+ setFocusArea(
194
+ (int) event.getX(0),
195
+ (int) event.getY(0),
196
+ new Camera.AutoFocusCallback() {
197
+ public void onAutoFocus(boolean success, Camera camera) {
198
+ if (success) {
199
+ takePicture(0, 0, 85);
200
+ } else {
201
+ Log.d(TAG, "onTouch:" + " setFocusArea() did not suceed");
220
202
  }
221
203
  }
222
- );
223
- }
224
- return true;
225
- } else {
226
- if (dragEnabled) {
227
- int x;
228
- int y;
229
-
230
- switch (event.getAction()) {
231
- case MotionEvent.ACTION_DOWN:
232
- if (mLastTouchX == 0 || mLastTouchY == 0) {
233
- mLastTouchX = (int) event.getRawX() - layoutParams.leftMargin;
234
- mLastTouchY = (int) event.getRawY() - layoutParams.topMargin;
204
+ }
205
+ );
206
+ } else if (tapToTakePicture) {
207
+ takePicture(0, 0, 85);
208
+ } else if (tapToFocus) {
209
+ setFocusArea(
210
+ (int) event.getX(0),
211
+ (int) event.getY(0),
212
+ new Camera.AutoFocusCallback() {
213
+ public void onAutoFocus(boolean success, Camera camera) {
214
+ if (success) {
215
+ // A callback to JS might make sense here.
235
216
  } else {
236
- mLastTouchX = (int) event.getRawX();
237
- mLastTouchY = (int) event.getRawY();
217
+ Log.d(TAG, "onTouch:" + " setFocusArea() did not suceed");
238
218
  }
239
- break;
240
- case MotionEvent.ACTION_MOVE:
241
- x = (int) event.getRawX();
242
- y = (int) event.getRawY();
219
+ }
220
+ }
221
+ );
222
+ }
223
+ return true;
224
+ } else {
225
+ if (dragEnabled) {
226
+ int x;
227
+ int y;
228
+
229
+ switch (event.getAction()) {
230
+ case MotionEvent.ACTION_DOWN:
231
+ if (mLastTouchX == 0 || mLastTouchY == 0) {
232
+ mLastTouchX = (int) event.getRawX() - layoutParams.leftMargin;
233
+ mLastTouchY = (int) event.getRawY() - layoutParams.topMargin;
234
+ } else {
235
+ mLastTouchX = (int) event.getRawX();
236
+ mLastTouchY = (int) event.getRawY();
237
+ }
238
+ break;
239
+ case MotionEvent.ACTION_MOVE:
240
+ x = (int) event.getRawX();
241
+ y = (int) event.getRawY();
243
242
 
244
- final float dx = x - mLastTouchX;
245
- final float dy = y - mLastTouchY;
243
+ final float dx = x - mLastTouchX;
244
+ final float dy = y - mLastTouchY;
246
245
 
247
- mPosX += dx;
248
- mPosY += dy;
246
+ mPosX += dx;
247
+ mPosY += dy;
249
248
 
250
- layoutParams.leftMargin = mPosX;
251
- layoutParams.topMargin = mPosY;
249
+ layoutParams.leftMargin = mPosX;
250
+ layoutParams.topMargin = mPosY;
252
251
 
253
- frameContainerLayout.setLayoutParams(layoutParams);
252
+ frameContainerLayout.setLayoutParams(layoutParams);
254
253
 
255
- // Remember this touch position for the next move event
256
- mLastTouchX = x;
257
- mLastTouchY = y;
254
+ // Remember this touch position for the next move event
255
+ mLastTouchX = x;
256
+ mLastTouchY = y;
258
257
 
259
- break;
260
- default:
261
- break;
262
- }
258
+ break;
259
+ default:
260
+ break;
263
261
  }
264
262
  }
265
263
  }
266
- return true;
267
264
  }
265
+ return true;
268
266
  }
269
- );
270
- frameContainerLayout.setFocusableInTouchMode(true);
271
- frameContainerLayout.requestFocus();
272
- frameContainerLayout.setOnKeyListener(
273
- new View.OnKeyListener() {
274
- @Override
275
- public boolean onKey(View v, int keyCode, android.view.KeyEvent event) {
276
- if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
277
- eventListener.onBackButton();
278
- return true;
279
- }
280
- return false;
267
+ }
268
+ );
269
+ frameContainerLayout.setFocusableInTouchMode(true);
270
+ frameContainerLayout.requestFocus();
271
+ frameContainerLayout.setOnKeyListener(
272
+ new View.OnKeyListener() {
273
+ @Override
274
+ public boolean onKey(View v, int keyCode, android.view.KeyEvent event) {
275
+ if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
276
+ eventListener.onBackButton();
277
+ return true;
281
278
  }
279
+ return false;
282
280
  }
283
- );
284
- }
281
+ }
282
+ );
283
+ }
285
284
 
286
- private float mDist = 0F;
287
-
288
- private void handleZoom(MotionEvent event, Camera.Parameters params) {
289
- if (mCamera != null) {
290
- mCamera.cancelAutoFocus();
291
- int maxZoom = params.getMaxZoom();
292
- int zoom = params.getZoom();
293
- float newDist = getFingerSpacing(event);
294
- if (newDist > mDist) {
295
- //zoom in
296
- if (zoom < maxZoom) zoom++;
297
- } else if (newDist < mDist) {
298
- //zoom out
299
- if (zoom > 0) zoom--;
300
- }
301
- mDist = newDist;
302
- params.setZoom(zoom);
303
- mCamera.setParameters(params);
285
+ private float mDist = 0F;
286
+
287
+ private void handleZoom(MotionEvent event, Camera.Parameters params) {
288
+ if (mCamera != null) {
289
+ mCamera.cancelAutoFocus();
290
+ int maxZoom = params.getMaxZoom();
291
+ int zoom = params.getZoom();
292
+ float newDist = getFingerSpacing(event);
293
+ if (newDist > mDist) {
294
+ //zoom in
295
+ if (zoom < maxZoom) zoom++;
296
+ } else if (newDist < mDist) {
297
+ //zoom out
298
+ if (zoom > 0) zoom--;
304
299
  }
300
+ mDist = newDist;
301
+ params.setZoom(zoom);
302
+ mCamera.setParameters(params);
305
303
  }
306
304
  }
307
- );
305
+ }
306
+ );
308
307
  }
309
308
 
310
309
  private void setDefaultCameraId() {
@@ -400,9 +399,10 @@ public class CameraActivity extends Fragment {
400
399
  getResources().getIdentifier("frame_container", "id", appResourcesPackage)
401
400
  );
402
401
 
403
- final int previousOrientation = frameContainerLayout.getHeight() > frameContainerLayout.getWidth()
404
- ? Configuration.ORIENTATION_PORTRAIT
405
- : Configuration.ORIENTATION_LANDSCAPE;
402
+ final int previousOrientation =
403
+ frameContainerLayout.getHeight() > frameContainerLayout.getWidth()
404
+ ? Configuration.ORIENTATION_PORTRAIT
405
+ : Configuration.ORIENTATION_LANDSCAPE;
406
406
  // Checks if the orientation of the screen has changed
407
407
  if (newConfig.orientation != previousOrientation) {
408
408
  final RelativeLayout frameCamContainerLayout = (RelativeLayout) view.findViewById(
@@ -450,9 +450,10 @@ public class CameraActivity extends Fragment {
450
450
  // etc.
451
451
  numberOfCameras = Camera.getNumberOfCameras();
452
452
 
453
- int nextFacing = cameraCurrentlyLocked == Camera.CameraInfo.CAMERA_FACING_BACK
454
- ? Camera.CameraInfo.CAMERA_FACING_FRONT
455
- : Camera.CameraInfo.CAMERA_FACING_BACK;
453
+ int nextFacing =
454
+ cameraCurrentlyLocked == Camera.CameraInfo.CAMERA_FACING_BACK
455
+ ? Camera.CameraInfo.CAMERA_FACING_FRONT
456
+ : Camera.CameraInfo.CAMERA_FACING_BACK;
456
457
 
457
458
  // Find the next ID of the camera to switch to (front if the current is back and visa versa)
458
459
  Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
@@ -759,9 +760,10 @@ public class CameraActivity extends Fragment {
759
760
  bytes = rotateNV21(bytes, size.width, size.height, orientation);
760
761
  }
761
762
  // switch width/height when rotating 90/270 deg
762
- Rect rect = orientation == 90 || orientation == 270
763
- ? new Rect(0, 0, size.height, size.width)
764
- : new Rect(0, 0, size.width, size.height);
763
+ Rect rect =
764
+ orientation == 90 || orientation == 270
765
+ ? new Rect(0, 0, size.height, size.width)
766
+ : new Rect(0, 0, size.width, size.height);
765
767
  YuvImage yuvImage = new YuvImage(bytes, parameters.getPreviewFormat(), rect.width(), rect.height(), null);
766
768
  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
767
769
  yuvImage.compressToJpeg(rect, quality, byteArrayOutputStream);
@@ -2,6 +2,7 @@ package com.ahm.capacitor.camera.preview;
2
2
 
3
3
  import static android.Manifest.permission.CAMERA;
4
4
 
5
+ import android.annotation.SuppressLint;
5
6
  import android.app.FragmentManager;
6
7
  import android.app.FragmentTransaction;
7
8
  import android.content.pm.ActivityInfo;
@@ -108,6 +109,7 @@ public class CameraPreview extends Plugin implements CameraActivity.CameraPrevie
108
109
  fragment.takeSnapshot(quality);
109
110
  }
110
111
 
112
+ @SuppressLint("WrongConstant")
111
113
  @PluginMethod
112
114
  public void stop(final PluginCall call) {
113
115
  bridge
@@ -120,13 +122,16 @@ public class CameraPreview extends Plugin implements CameraActivity.CameraPrevie
120
122
 
121
123
  // allow orientation changes after closing camera:
122
124
  getBridge().getActivity().setRequestedOrientation(previousOrientationRequest);
125
+ getBridge().getWebView().setOnTouchListener(null);
123
126
 
124
127
  if (containerView != null) {
125
128
  ((ViewGroup) getBridge().getWebView().getParent()).removeView(containerView);
126
129
  getBridge().getWebView().setBackgroundColor(Color.WHITE);
127
130
  FragmentManager fragmentManager = getActivity().getFragmentManager();
128
131
  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
129
- fragmentTransaction.remove(fragment);
132
+ if (fragment != null) {
133
+ fragmentTransaction.remove(fragment);
134
+ }
130
135
  fragmentTransaction.commit();
131
136
  fragment = null;
132
137
 
@@ -346,7 +351,6 @@ public class CameraPreview extends Plugin implements CameraActivity.CameraPrevie
346
351
  ((ViewGroup) getBridge().getWebView().getParent()).addView(containerView);
347
352
  if (toBack == true) {
348
353
  getBridge().getWebView().getParent().bringChildToFront(getBridge().getWebView());
349
- setupBroadcast();
350
354
  }
351
355
 
352
356
  FragmentManager fragmentManager = getBridge().getActivity().getFragmentManager();
@@ -410,9 +414,17 @@ public class CameraPreview extends Plugin implements CameraActivity.CameraPrevie
410
414
 
411
415
  @Override
412
416
  public void onCameraStarted() {
417
+ if (fragment != null && fragment.toBack) {
418
+ setupBroadcast();
419
+ }
420
+
413
421
  PluginCall pluginCall = bridge.getSavedCall(cameraStartCallbackId);
414
- pluginCall.resolve();
415
- bridge.releaseCall(pluginCall);
422
+ if (pluginCall != null) {
423
+ pluginCall.resolve();
424
+ bridge.releaseCall(pluginCall);
425
+ } else {
426
+ Logger.warn(getLogTag(), "onCameraStarted but no saved start call (cameraStartCallbackId=" + cameraStartCallbackId + ")");
427
+ }
416
428
  }
417
429
 
418
430
  @Override
@@ -480,7 +492,7 @@ public class CameraPreview extends Plugin implements CameraActivity.CameraPrevie
480
492
  new View.OnTouchListener() {
481
493
  @Override
482
494
  public boolean onTouch(View v, MotionEvent event) {
483
- if ((null != fragment) && (fragment.toBack == true)) {
495
+ if (fragment != null && fragment.toBack && fragment.frameContainerLayout != null) {
484
496
  fragment.frameContainerLayout.dispatchTouchEvent(event);
485
497
  }
486
498
  return false;
@@ -137,9 +137,9 @@ class Preview extends RelativeLayout implements SurfaceHolder.Callback, TextureV
137
137
  Log.d(
138
138
  TAG,
139
139
  (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ? "front" : "back") +
140
- " camera is oriented -" +
141
- info.orientation +
142
- "deg from natural"
140
+ " camera is oriented -" +
141
+ info.orientation +
142
+ "deg from natural"
143
143
  );
144
144
  Log.d(TAG, "need to rotate preview " + displayOrientation + "deg");
145
145
  mCamera.setDisplayOrientation(displayOrientation);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor-community/camera-preview",
3
- "version": "7.0.3",
3
+ "version": "7.0.5",
4
4
  "description": "Camera preview",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -18,10 +18,9 @@
18
18
  "fmt": "concurrently -g \"npm:eslint -- --fix\" \"npm:prettier -- --write\" \"npm:swiftlint -- lint --fix --format ios\"",
19
19
  "eslint": "eslint src --ext ts",
20
20
  "prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
21
- "swiftlint": "node-swiftlint",
21
+ "swiftlint": "node-swiftlint --config .swiftlint.yml",
22
22
  "prepublishOnly": "npm run build",
23
- "prepare": "husky install && npm run build",
24
- "postinstall": "patch-package"
23
+ "prepare": "husky install && npm run build"
25
24
  },
26
25
  "author": "Ariel Hernandez Musa",
27
26
  "license": "MIT",
@@ -37,7 +36,6 @@
37
36
  "concurrently": "^9.2.1",
38
37
  "eslint": "^9.39.2",
39
38
  "husky": "^9.1.7",
40
- "patch-package": "^8.0.1",
41
39
  "prettier": "^3.8.1",
42
40
  "prettier-plugin-java": "^2.8.1",
43
41
  "pretty-quick": "^4.1.1",