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

Implement camera switching #51

Open
wants to merge 10 commits into
base: main
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
2 changes: 1 addition & 1 deletion fast_barcode_scanner/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ group 'com.jhoogstraat.fast_barcode_scanner'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.6.10'
repositories {
google()
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import java.util.ArrayList
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

data class CameraConfig(val formats: IntArray, val mode: DetectionMode, val resolution: Resolution, val framerate: Framerate, val position: CameraPosition)
data class CameraConfig(val formats: IntArray, val mode: DetectionMode, val resolution: Resolution, val framerate: Framerate, var position: CameraPosition)

class BarcodeReader(private val flutterTextureEntry: TextureRegistry.SurfaceTextureEntry, private val listener: (List<Barcode>) -> Unit) : RequestPermissionsResultListener {
/* Android Lifecycle */
Expand Down Expand Up @@ -106,6 +106,28 @@ class BarcodeReader(private val flutterTextureEntry: TextureRegistry.SurfaceText
}, ContextCompat.getMainExecutor(activity))
}

fun canChangeCamera(result: Result) {
try {
val cameraProviderFuture = ProcessCameraProvider.getInstance(activity!!)
cameraProviderFuture.addListener(Runnable {
val cameraProviderForChangeCamera = cameraProviderFuture.get()
val hasFrontCamera = cameraProviderForChangeCamera.hasCamera(CameraSelector.DEFAULT_FRONT_CAMERA)
val hasBackCamera = cameraProviderForChangeCamera.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA)
result.success(hasFrontCamera && hasBackCamera)
}, ContextCompat.getMainExecutor(activity!!))
} catch (exc: Exception) {
result.success(false)
}
}

fun changeCamera(position: String, result: Result) {
cameraConfig.position = when (position) {
"front" -> CameraPosition.front
else -> CameraPosition.back
}
initCamera()
}

private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(activity!!.applicationContext, it) == PackageManager.PERMISSION_GRANTED
}
Expand Down Expand Up @@ -148,12 +170,8 @@ class BarcodeReader(private val flutterTextureEntry: TextureRegistry.SurfaceText
// Select camera
val selectorBuilder = CameraSelector.Builder()
when (cameraConfig.position) {
CameraPosition.front -> {
selectorBuilder.requireLensFacing(CameraSelector.LENS_FACING_FRONT)
}
CameraPosition.back -> {
selectorBuilder.requireLensFacing(CameraSelector.LENS_FACING_BACK)
}
CameraPosition.front -> selectorBuilder.requireLensFacing(CameraSelector.LENS_FACING_FRONT)
CameraPosition.back -> selectorBuilder.requireLensFacing(CameraSelector.LENS_FACING_BACK)
}
cameraSelector = selectorBuilder.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class FastBarcodeScannerPlugin: FlutterPlugin, MethodCallHandler, ActivityAware
"pause" -> reader.stop(result)
"resume" -> reader.resume(result)
"toggleTorch" -> reader.toggleTorch(result)
"canChangeCamera" -> reader.canChangeCamera(result)
"changeCamera" -> reader.changeCamera(call.arguments as String, result)
else -> result.notImplemented()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:theme="@style/LaunchTheme"
android:exported="true"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
Expand Down
6 changes: 3 additions & 3 deletions fast_barcode_scanner/example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
ext.kotlin_version = '1.4.32'
ext.kotlin_version = '1.8.0'
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'com.android.tools.build:gradle:7.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand All @@ -24,6 +24,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
32 changes: 21 additions & 11 deletions fast_barcode_scanner/example/lib/scanner_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:fast_barcode_scanner/fast_barcode_scanner.dart';
import 'package:flutter/material.dart';

import 'detections_counter.dart';

final codeStream = StreamController<Barcode>.broadcast();
Expand All @@ -15,6 +16,18 @@ class ScannerScreen extends StatefulWidget {

class _ScannerScreenState extends State<ScannerScreen> {
final _torchIconState = ValueNotifier(false);
bool _canChangeCamera = false;

@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => _checkCanChangeCamera());
}

Future<void> _checkCanChangeCamera() async {
_canChangeCamera = await CameraController.instance.canChangeCamera();
setState(() {});
}

@override
Widget build(BuildContext context) {
Expand All @@ -32,25 +45,22 @@ class _ScannerScreenState extends State<ScannerScreen> {
ValueListenableBuilder<bool>(
valueListenable: _torchIconState,
builder: (context, state, _) => IconButton(
icon: state
? const Icon(Icons.flash_on)
: const Icon(Icons.flash_off),
icon: state ? const Icon(Icons.flash_on) : const Icon(Icons.flash_off),
onPressed: () async {
await CameraController.instance.toggleTorch();
_torchIconState.value =
CameraController.instance.state.torchState;
_torchIconState.value = CameraController.instance.state.torchState;
},
),
),
if (_canChangeCamera)
IconButton(
icon: const Icon(Icons.cameraswitch),
onPressed: CameraController.instance.toggleCamera,
),
],
),
body: BarcodeCamera(
types: const [
BarcodeType.ean8,
BarcodeType.ean13,
BarcodeType.code128,
BarcodeType.qr
],
types: const [BarcodeType.ean8, BarcodeType.ean13, BarcodeType.code128, BarcodeType.qr],
resolution: Resolution.hd720,
framerate: Framerate.fps30,
mode: DetectionMode.pauseVideo,
Expand Down
5 changes: 5 additions & 0 deletions fast_barcode_scanner/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: fast_barcode_scanner_example
description: Demonstrates how to use the fast_barcode_scanner plugin.
publish_to: 'none'
version: 0.0.1+1

environment:
sdk: '>=2.12.0 <3.0.0'
Expand All @@ -15,5 +16,9 @@ dependencies:
dev_dependencies:
flutter_lints: ^1.0.4

dependency_overrides:
fast_barcode_scanner_platform_interface:
path: ../../fast_barcode_scanner_platform_interface/

flutter:
uses-material-design: true
Loading