Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding ability to preserve WebView's opacity when stop() is called #343

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ Ex: VueJS >> App.vue component
```


### stop()
### stop(options)

| Option | values | descriptions |
|-----------------|---------------|---------------------------------------------------------------------------------------------------------------------------|
| backgroundColor | number | (optional) Android only. Background color for WebView. Set to 0 to keep TRANSPARENT background. Default -1 (Color.WHITE). |


<info>Stops the camera preview instance.</info><br/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public void captureSample(PluginCall call) {

@PluginMethod
public void stop(final PluginCall call) {
final Integer backgroundColor = call.getInt("backgroundColor", Color.WHITE);
bridge
.getActivity()
.runOnUiThread(
Expand All @@ -123,7 +124,7 @@ public void run() {

if (containerView != null) {
((ViewGroup) getBridge().getWebView().getParent()).removeView(containerView);
getBridge().getWebView().setBackgroundColor(Color.WHITE);
getBridge().getWebView().setBackgroundColor(backgroundColor == null ? Color.WHITE : backgroundColor);
FragmentManager fragmentManager = getActivity().getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment);
Expand Down
10 changes: 9 additions & 1 deletion ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class CameraPreview: CAPPlugin {
var enableZoom: Bool?
var highResolutionOutput: Bool = false
var disableAudio: Bool = false
var originalOpacity: Bool?
var originalBackgroundColor: UIColor?
var originalScrollbarBgColor: UIColor?

@objc func rotated() {
let height = self.paddingBottom != nil ? self.height! - self.paddingBottom!: self.height!
Expand Down Expand Up @@ -86,8 +89,11 @@ public class CameraPreview: CAPPlugin {
}
let height = self.paddingBottom != nil ? self.height! - self.paddingBottom!: self.height!
self.previewView = UIView(frame: CGRect(x: self.x ?? 0, y: self.y ?? 0, width: self.width!, height: height))
self.originalOpacity = self.webView?.isOpaque
self.webView?.isOpaque = false
self.originalBackgroundColor = self.webView?.backgroundColor
self.webView?.backgroundColor = UIColor.clear
self.originalScrollbarBgColor = self.webView?.scrollView.backgroundColor
self.webView?.scrollView.backgroundColor = UIColor.clear
self.webView?.superview?.addSubview(self.previewView)
if self.toBack! {
Expand Down Expand Up @@ -125,7 +131,9 @@ public class CameraPreview: CAPPlugin {
if self.cameraController.captureSession?.isRunning ?? false {
self.cameraController.captureSession?.stopRunning()
self.previewView.removeFromSuperview()
self.webView?.isOpaque = true
self.webView?.isOpaque = self.originalOpacity!
self.webView?.backgroundColor = self.originalBackgroundColor!
self.webView?.scrollView.backgroundColor = self.originalScrollbarBgColor!
call.resolve()
} else {
call.reject("camera already stopped")
Expand Down
7 changes: 6 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export interface CameraSampleOptions {
quality?: number;
}

export interface CameraPreviewStopOptions {
/** Android only. Defaults to -1 (Color.WHITE). Set to 0 if you want to keep transparent background */
backgroundColor?: number;
}

export type CameraPreviewFlashMode = 'off' | 'on' | 'auto' | 'red-eye' | 'torch';

export interface CameraOpacityOptions {
Expand All @@ -61,7 +66,7 @@ export interface CameraOpacityOptions {
export interface CameraPreviewPlugin {
start(options: CameraPreviewOptions): Promise<{}>;
startRecordVideo(options: CameraPreviewOptions): Promise<{}>;
stop(): Promise<{}>;
stop(options: CameraPreviewStopOptions): Promise<{}>;
stopRecordVideo(): Promise<{}>;
capture(options: CameraPreviewPictureOptions): Promise<{ value: string }>;
captureSample(options: CameraSampleOptions): Promise<{ value: string }>;
Expand Down