Skip to content

fix camera preview issue on android and ios #376

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

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
4 changes: 2 additions & 2 deletions src/android/QRScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.json.JSONObject;
import android.hardware.Camera;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import androidx.core.app.ActivityCompat;
import android.view.ViewGroup;
import android.widget.FrameLayout;

Expand Down Expand Up @@ -619,7 +619,7 @@ private void show(final CallbackContext callbackContext) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
webView.getView().setBackgroundColor(Color.argb(1, 0, 0, 0));
webView.getView().setBackgroundColor(Color.TRANSPARENT);
showing = true;
getStatus(callbackContext);
}
Expand Down
4 changes: 2 additions & 2 deletions src/android/qrscanner.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ repositories {
}

dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.3.0'
compile 'com.android.support:appcompat-v7:23.1.0'
implementation 'com.journeyapps:zxing-android-embedded:3.3.0'
implementation 'com.android.support:appcompat-v7:23.1.0'
}

android {
Expand Down
22 changes: 14 additions & 8 deletions src/ios/QRScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,17 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
if (captureSession?.isRunning != true){
cameraView.backgroundColor = UIColor.clear
self.webView!.superview!.insertSubview(cameraView, belowSubview: self.webView!)
let availableVideoDevices = AVCaptureDevice.devices(for: AVMediaType.video)
for device in availableVideoDevices {
if device.position == AVCaptureDevice.Position.back {
backCamera = device
let session = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .unspecified)
let cameras = session.devices.compactMap { $0 }
for camera in cameras {
if camera.position == .front {
self.frontCamera = camera
}
else if device.position == AVCaptureDevice.Position.front {
frontCamera = device
if camera.position == .back {
self.backCamera = camera
try camera.lockForConfiguration()
camera.focusMode = .continuousAutoFocus
camera.unlockForConfiguration()
}
}
// older iPods have no back camera
Expand Down Expand Up @@ -197,8 +201,9 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
}

@objc func makeOpaque(){
self.webView?.isOpaque = false
self.webView?.backgroundColor = UIColor.clear
self.webView?.isOpaque = true
self.webView?.backgroundColor = UIColor.white
self.webView?.scrollView.backgroundColor = UIColor.white
}

@objc func boolToNumberString(bool: Bool) -> String{
Expand Down Expand Up @@ -292,6 +297,7 @@ class QRScanner : CDVPlugin, AVCaptureMetadataOutputObjectsDelegate {
@objc func show(_ command: CDVInvokedUrlCommand) {
self.webView?.isOpaque = false
self.webView?.backgroundColor = UIColor.clear
self.webView?.scrollView.backgroundColor = UIColor.clear
self.getStatus(command)
}

Expand Down