@capgo/camera-preview 8.15.5 → 8.15.7
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/android/build.gradle
CHANGED
|
@@ -71,7 +71,7 @@ dependencies {
|
|
|
71
71
|
implementation 'com.google.android.gms:play-services-mlkit-barcode-scanning:18.3.1'
|
|
72
72
|
|
|
73
73
|
testImplementation "junit:junit:$junitVersion"
|
|
74
|
-
testImplementation 'org.robolectric:robolectric:4.
|
|
74
|
+
testImplementation 'org.robolectric:robolectric:4.18.1'
|
|
75
75
|
testImplementation 'androidx.test:core:1.7.0'
|
|
76
76
|
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
77
77
|
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
@@ -29,6 +29,7 @@ import android.media.MediaScannerConnection;
|
|
|
29
29
|
import android.os.Build;
|
|
30
30
|
import android.os.Environment;
|
|
31
31
|
import android.os.SystemClock;
|
|
32
|
+
import android.provider.Settings;
|
|
32
33
|
import android.util.Base64;
|
|
33
34
|
import android.util.DisplayMetrics;
|
|
34
35
|
import android.util.Log;
|
|
@@ -39,6 +40,7 @@ import android.view.View;
|
|
|
39
40
|
import android.view.ViewGroup;
|
|
40
41
|
import android.view.WindowManager;
|
|
41
42
|
import android.view.WindowMetrics;
|
|
43
|
+
import android.view.animation.PathInterpolator;
|
|
42
44
|
import android.webkit.WebView;
|
|
43
45
|
import android.widget.FrameLayout;
|
|
44
46
|
import androidx.annotation.NonNull;
|
|
@@ -127,6 +129,13 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
127
129
|
|
|
128
130
|
private static final String TAG = "CameraPreview CameraXView";
|
|
129
131
|
private static final String FOCUS_INDICATOR_TAG = "cpcp_focus_indicator";
|
|
132
|
+
// Focus indicator motion, kept in sync with the iOS implementation (CameraController.showFocusIndicator)
|
|
133
|
+
private static final long FOCUS_INDICATOR_ENTER_MS = 250;
|
|
134
|
+
private static final long FOCUS_INDICATOR_EXIT_MS = 150;
|
|
135
|
+
private static final long FOCUS_INDICATOR_MIN_VISIBLE_MS = 500;
|
|
136
|
+
private static final float FOCUS_INDICATOR_START_SCALE = 1.5f;
|
|
137
|
+
// View tag marking that the entrance animation finished; until then the tag may hold a pending exit Runnable
|
|
138
|
+
private static final Object FOCUS_INDICATOR_ENTRANCE_DONE = new Object();
|
|
130
139
|
|
|
131
140
|
public interface CameraXViewListener {
|
|
132
141
|
void onPictureTaken(String base64, JSONObject exif);
|
|
@@ -166,6 +175,7 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
166
175
|
private FrameLayout previewContainer;
|
|
167
176
|
private View focusIndicatorView;
|
|
168
177
|
private long focusIndicatorAnimationId = 0; // Incrementing token to invalidate previous animations
|
|
178
|
+
private long focusIndicatorShownAtMs = 0;
|
|
169
179
|
private CameraSelector currentCameraSelector;
|
|
170
180
|
private String currentDeviceId;
|
|
171
181
|
private String currentPhysicalDeviceId;
|
|
@@ -4093,10 +4103,13 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
4093
4103
|
View child = previewContainer.getChildAt(i);
|
|
4094
4104
|
CharSequence desc = child.getContentDescription();
|
|
4095
4105
|
if (desc != null && FOCUS_INDICATOR_TAG.contentEquals(desc)) {
|
|
4106
|
+
// Cancel any running enter/exit animation so a new tap retargets cleanly
|
|
4107
|
+
child.animate().cancel();
|
|
4096
4108
|
previewContainer.removeViewAt(i);
|
|
4097
4109
|
}
|
|
4098
4110
|
}
|
|
4099
4111
|
if (focusIndicatorView != null) {
|
|
4112
|
+
focusIndicatorView.animate().cancel();
|
|
4100
4113
|
ViewGroup parent = (ViewGroup) focusIndicatorView.getParent();
|
|
4101
4114
|
if (parent != null) parent.removeView(focusIndicatorView);
|
|
4102
4115
|
focusIndicatorView = null;
|
|
@@ -4166,10 +4179,11 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
4166
4179
|
final long thisAnimationId = ++focusIndicatorAnimationId;
|
|
4167
4180
|
final View thisIndicatorView = focusIndicatorView;
|
|
4168
4181
|
|
|
4169
|
-
//
|
|
4170
|
-
|
|
4171
|
-
focusIndicatorView.
|
|
4172
|
-
focusIndicatorView.
|
|
4182
|
+
// Start state for the entrance animation (static if system animations are disabled)
|
|
4183
|
+
final boolean animate = areSystemAnimationsEnabled();
|
|
4184
|
+
focusIndicatorView.setAlpha(animate ? 0f : 1f);
|
|
4185
|
+
focusIndicatorView.setScaleX(animate ? FOCUS_INDICATOR_START_SCALE : 1f);
|
|
4186
|
+
focusIndicatorView.setScaleY(animate ? FOCUS_INDICATOR_START_SCALE : 1f);
|
|
4173
4187
|
focusIndicatorView.setVisibility(View.VISIBLE);
|
|
4174
4188
|
|
|
4175
4189
|
// Ensure container doesn't intercept touch events
|
|
@@ -4188,6 +4202,30 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
4188
4202
|
// Force a layout pass to ensure the view is properly positioned
|
|
4189
4203
|
previewContainer.requestLayout();
|
|
4190
4204
|
|
|
4205
|
+
// Short ease-out scale + fade in, matching the iOS focus box
|
|
4206
|
+
focusIndicatorShownAtMs = SystemClock.uptimeMillis();
|
|
4207
|
+
if (animate) {
|
|
4208
|
+
thisIndicatorView
|
|
4209
|
+
.animate()
|
|
4210
|
+
.alpha(1f)
|
|
4211
|
+
.scaleX(1f)
|
|
4212
|
+
.scaleY(1f)
|
|
4213
|
+
.setDuration(FOCUS_INDICATOR_ENTER_MS)
|
|
4214
|
+
.setInterpolator(new PathInterpolator(0.23f, 1f, 0.32f, 1f))
|
|
4215
|
+
.withEndAction(() -> {
|
|
4216
|
+
// Run an exit that was requested while the entrance was still playing
|
|
4217
|
+
// (e.g. with a large system animator duration scale)
|
|
4218
|
+
Object pendingExit = thisIndicatorView.getTag();
|
|
4219
|
+
thisIndicatorView.setTag(FOCUS_INDICATOR_ENTRANCE_DONE);
|
|
4220
|
+
if (pendingExit instanceof Runnable) {
|
|
4221
|
+
((Runnable) pendingExit).run();
|
|
4222
|
+
}
|
|
4223
|
+
})
|
|
4224
|
+
.start();
|
|
4225
|
+
} else {
|
|
4226
|
+
thisIndicatorView.setTag(FOCUS_INDICATOR_ENTRANCE_DONE);
|
|
4227
|
+
}
|
|
4228
|
+
|
|
4191
4229
|
// Do not schedule delayed cleanup; indicator will be removed when focus completes
|
|
4192
4230
|
return thisAnimationId;
|
|
4193
4231
|
}
|
|
@@ -4209,12 +4247,43 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
4209
4247
|
focusIndicatorView = null;
|
|
4210
4248
|
return;
|
|
4211
4249
|
}
|
|
4212
|
-
|
|
4213
|
-
|
|
4214
|
-
|
|
4250
|
+
final View indicator = focusIndicatorView;
|
|
4251
|
+
focusIndicatorView = null;
|
|
4252
|
+
if (!areSystemAnimationsEnabled()) {
|
|
4253
|
+
removeFocusIndicatorView(indicator);
|
|
4254
|
+
return;
|
|
4255
|
+
}
|
|
4256
|
+
// Keep the indicator on screen briefly even when focus resolves instantly, then fade out fast.
|
|
4257
|
+
// The exit is requested only after both the hold and the entrance end: calling animate().alpha()
|
|
4258
|
+
// earlier would cancel the still-running entrance alpha animation and freeze the indicator mid-fade.
|
|
4259
|
+
long elapsed = SystemClock.uptimeMillis() - focusIndicatorShownAtMs;
|
|
4260
|
+
long delay = Math.max(0, FOCUS_INDICATOR_MIN_VISIBLE_MS - elapsed);
|
|
4261
|
+
Runnable startExit = () -> {
|
|
4262
|
+
// A newer tap replaced (and removed) this indicator, or the preview was torn down
|
|
4263
|
+
if (indicator.getParent() == null || token != focusIndicatorAnimationId) {
|
|
4264
|
+
return;
|
|
4265
|
+
}
|
|
4266
|
+
Runnable exit = () ->
|
|
4267
|
+
indicator
|
|
4268
|
+
.animate()
|
|
4269
|
+
.alpha(0f)
|
|
4270
|
+
.setDuration(FOCUS_INDICATOR_EXIT_MS)
|
|
4271
|
+
.setInterpolator(new PathInterpolator(0.23f, 1f, 0.32f, 1f))
|
|
4272
|
+
.withEndAction(() -> removeFocusIndicatorView(indicator))
|
|
4273
|
+
.start();
|
|
4274
|
+
if (indicator.getTag() == FOCUS_INDICATOR_ENTRANCE_DONE) {
|
|
4275
|
+
exit.run();
|
|
4276
|
+
} else {
|
|
4277
|
+
// Entrance still running: chain the exit after it instead of cancelling it
|
|
4278
|
+
indicator.setTag(exit);
|
|
4279
|
+
}
|
|
4280
|
+
};
|
|
4281
|
+
if (delay > 0) {
|
|
4282
|
+
previewContainer.postDelayed(startExit, delay);
|
|
4283
|
+
} else {
|
|
4284
|
+
startExit.run();
|
|
4215
4285
|
}
|
|
4216
4286
|
} catch (Exception ignore) {
|
|
4217
|
-
} finally {
|
|
4218
4287
|
focusIndicatorView = null;
|
|
4219
4288
|
}
|
|
4220
4289
|
});
|
|
@@ -4224,6 +4293,27 @@ public class CameraXView implements LifecycleOwner, LifecycleObserver {
|
|
|
4224
4293
|
}
|
|
4225
4294
|
}
|
|
4226
4295
|
|
|
4296
|
+
private static void removeFocusIndicatorView(View indicator) {
|
|
4297
|
+
if (indicator == null) return;
|
|
4298
|
+
try {
|
|
4299
|
+
ViewGroup parent = (ViewGroup) indicator.getParent();
|
|
4300
|
+
if (parent != null) {
|
|
4301
|
+
parent.removeView(indicator);
|
|
4302
|
+
}
|
|
4303
|
+
} catch (Exception ignore) {}
|
|
4304
|
+
}
|
|
4305
|
+
|
|
4306
|
+
private boolean areSystemAnimationsEnabled() {
|
|
4307
|
+
try {
|
|
4308
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
4309
|
+
return android.animation.ValueAnimator.areAnimatorsEnabled();
|
|
4310
|
+
}
|
|
4311
|
+
return Settings.Global.getFloat(context.getContentResolver(), Settings.Global.ANIMATOR_DURATION_SCALE, 1f) != 0f;
|
|
4312
|
+
} catch (Exception ignore) {
|
|
4313
|
+
return true;
|
|
4314
|
+
}
|
|
4315
|
+
}
|
|
4316
|
+
|
|
4227
4317
|
public static List<Size> getSupportedPictureSizes(String facing) {
|
|
4228
4318
|
List<Size> sizes = new ArrayList<>();
|
|
4229
4319
|
try {
|
|
@@ -3847,8 +3847,12 @@ extension CameraController: UIGestureRecognizerDelegate {
|
|
|
3847
3847
|
}
|
|
3848
3848
|
|
|
3849
3849
|
private func showFocusIndicator(at point: CGPoint, in view: UIView) {
|
|
3850
|
-
//
|
|
3851
|
-
focusIndicatorView
|
|
3850
|
+
// Cancel any running animation on the previous indicator and remove it so a new tap retargets cleanly
|
|
3851
|
+
if let previous = focusIndicatorView {
|
|
3852
|
+
previous.layer.removeAllAnimations()
|
|
3853
|
+
previous.removeFromSuperview()
|
|
3854
|
+
}
|
|
3855
|
+
let reduceMotion = UIAccessibility.isReduceMotionEnabled
|
|
3852
3856
|
|
|
3853
3857
|
// Create a new focus indicator (iOS Camera style): square with mid-edge ticks
|
|
3854
3858
|
let indicator = UIView(frame: CGRect(x: 0, y: 0, width: 80, height: 80))
|
|
@@ -3858,7 +3862,8 @@ extension CameraController: UIGestureRecognizerDelegate {
|
|
|
3858
3862
|
indicator.layer.cornerRadius = 0
|
|
3859
3863
|
indicator.backgroundColor = UIColor.clear
|
|
3860
3864
|
indicator.alpha = 0
|
|
3861
|
-
|
|
3865
|
+
// Reduce Motion: no scale, just a fade
|
|
3866
|
+
indicator.transform = reduceMotion ? .identity : CGAffineTransform(scaleX: 1.5, y: 1.5)
|
|
3862
3867
|
|
|
3863
3868
|
// Add 4 tiny mid-edge ticks inside the square
|
|
3864
3869
|
let stroke: CGFloat = 2.0
|
|
@@ -3896,27 +3901,35 @@ extension CameraController: UIGestureRecognizerDelegate {
|
|
|
3896
3901
|
view.addSubview(indicator)
|
|
3897
3902
|
focusIndicatorView = indicator
|
|
3898
3903
|
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
indicator
|
|
3902
|
-
|
|
3903
|
-
}) { _ in
|
|
3904
|
-
// Keep the indicator visible briefly
|
|
3905
|
-
UIView.animate(withDuration: 0.2, delay: 0.5, options: [], animations: {
|
|
3906
|
-
indicator.alpha = 0.3
|
|
3907
|
-
}) { _ in
|
|
3908
|
-
// Fade out and remove
|
|
3909
|
-
UIView.animate(withDuration: 0.3, delay: 0.2, options: [], animations: {
|
|
3910
|
-
indicator.alpha = 0
|
|
3911
|
-
indicator.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
|
|
3912
|
-
}) { _ in
|
|
3913
|
-
indicator.removeFromSuperview()
|
|
3914
|
-
if self.focusIndicatorView == indicator {
|
|
3915
|
-
self.focusIndicatorView = nil
|
|
3916
|
-
}
|
|
3917
|
-
}
|
|
3904
|
+
let removeIndicator: () -> Void = { [weak self] in
|
|
3905
|
+
indicator.removeFromSuperview()
|
|
3906
|
+
if self?.focusIndicatorView == indicator {
|
|
3907
|
+
self?.focusIndicatorView = nil
|
|
3918
3908
|
}
|
|
3919
3909
|
}
|
|
3910
|
+
|
|
3911
|
+
// Entrance: short ease-out scale + fade. Exit: quick fade after a brief hold (~0.75s total).
|
|
3912
|
+
UIView.animate(withDuration: reduceMotion ? 0.15 : 0.25,
|
|
3913
|
+
delay: 0,
|
|
3914
|
+
options: [.curveEaseOut, .allowUserInteraction],
|
|
3915
|
+
animations: {
|
|
3916
|
+
indicator.alpha = 1.0
|
|
3917
|
+
indicator.transform = .identity
|
|
3918
|
+
}, completion: { finished in
|
|
3919
|
+
// Interrupted (new tap, teardown): drop it without running the exit
|
|
3920
|
+
guard finished, indicator.superview != nil else {
|
|
3921
|
+
removeIndicator()
|
|
3922
|
+
return
|
|
3923
|
+
}
|
|
3924
|
+
UIView.animate(withDuration: 0.15,
|
|
3925
|
+
delay: 0.35,
|
|
3926
|
+
options: [.curveEaseOut, .allowUserInteraction],
|
|
3927
|
+
animations: {
|
|
3928
|
+
indicator.alpha = 0
|
|
3929
|
+
}, completion: { _ in
|
|
3930
|
+
removeIndicator()
|
|
3931
|
+
})
|
|
3932
|
+
})
|
|
3920
3933
|
}
|
|
3921
3934
|
|
|
3922
3935
|
@objc
|
|
@@ -35,7 +35,7 @@ extension UIWindow {
|
|
|
35
35
|
*/
|
|
36
36
|
@objc(CameraPreview)
|
|
37
37
|
public class CameraPreview: CAPPlugin, CAPBridgedPlugin, CLLocationManagerDelegate {
|
|
38
|
-
private let pluginVersion: String = "8.15.
|
|
38
|
+
private let pluginVersion: String = "8.15.7"
|
|
39
39
|
public let identifier = "CameraPreviewPlugin"
|
|
40
40
|
public let jsName = "CameraPreview"
|
|
41
41
|
public let pluginMethods: [CAPPluginMethod] = [
|