Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
mobile_scanner
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Julian Steenbakker
2022-02-21 22:49:43 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
e6b37c69b4e1738c7c026cc315a9e8b498d42a68
e6b37c69
1 parent
ed0d4d4c
feat: add image picker from gallery for android
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
1 deletions
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
example/lib/main.dart
example/pubspec.yaml
lib/src/mobile_scanner_controller.dart
android/src/main/kotlin/dev/steenbakker/mobile_scanner/MobileScanner.kt
View file @
e6b37c6
...
...
@@ -4,6 +4,7 @@ import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.graphics.Point
import android.net.Uri
import android.util.Log
import android.util.Size
import android.view.Surface
...
...
@@ -22,6 +23,8 @@ import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry
import io.flutter.view.TextureRegistry
import java.io.File
import java.net.URI
class MobileScanner(private val activity: Activity, private val textureRegistry: TextureRegistry)
...
...
@@ -50,6 +53,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
"torch" -> switchTorch(call, result)
"analyze" -> switchAnalyzeMode(call, result)
"stop" -> stop(result)
"analyzeImage" -> analyzeImage(call, result)
else -> result.notImplemented()
}
}
...
...
@@ -197,6 +201,23 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
result.success(null)
}
private fun analyzeImage(call: MethodCall, result: MethodChannel.Result) {
val uri = Uri.fromFile( File(call.arguments.toString()))
val inputImage = InputImage.fromFilePath(activity, uri)
scanner.process(inputImage)
.addOnSuccessListener { barcodes ->
for (barcode in barcodes) {
val event = mapOf("name" to "barcode", "data" to barcode.data)
sink?.success(event)
}
}
.addOnFailureListener { e -> Log.e(TAG, e.message, e)
result.error(TAG, e.message, e)}
.addOnCompleteListener { result.success(null) }
}
private fun stop(result: MethodChannel.Result) {
val owner = activity as LifecycleOwner
camera!!.cameraInfo.torchState.removeObservers(owner)
...
...
example/lib/main.dart
View file @
e6b37c6
import
'package:flutter/material.dart'
;
import
'package:image_picker/image_picker.dart'
;
import
'package:mobile_scanner/mobile_scanner.dart'
;
void
main
(
)
{
...
...
@@ -73,7 +74,7 @@ class _AnalyzeViewState extends State<AnalyzeView>
),
Center
(
child:
SizedBox
(
width:
MediaQuery
.
of
(
context
).
size
.
width
-
1
2
0
,
width:
MediaQuery
.
of
(
context
).
size
.
width
-
1
8
0
,
height:
50
,
child:
FittedBox
(
child:
Text
(
...
...
@@ -103,6 +104,19 @@ class _AnalyzeViewState extends State<AnalyzeView>
iconSize:
32.0
,
onPressed:
()
=>
controller
.
switchCamera
(),
),
IconButton
(
color:
Colors
.
white
,
icon:
Icon
(
Icons
.
browse_gallery
),
iconSize:
32.0
,
onPressed:
()
async
{
final
ImagePicker
_picker
=
ImagePicker
();
// Pick an image
final
XFile
?
image
=
await
_picker
.
pickImage
(
source
:
ImageSource
.
gallery
);
if
(
image
!=
null
)
{
controller
.
analyzeImage
(
image
.
path
);
}
},
),
],
),
),
...
...
example/pubspec.yaml
View file @
e6b37c6
...
...
@@ -6,6 +6,7 @@ environment:
sdk
:
"
>=2.12.0
<3.0.0"
dependencies
:
image_picker
:
^0.8.4+9
flutter
:
sdk
:
flutter
...
...
lib/src/mobile_scanner_controller.dart
View file @
e6b37c6
...
...
@@ -170,6 +170,10 @@ class MobileScannerController {
start
();
}
Future
<
void
>
analyzeImage
(
dynamic
path
)
async
{
await
methodChannel
.
invokeMethod
(
'analyzeImage'
,
path
);
}
/// Disposes the controller and closes all listeners.
void
dispose
()
{
if
(
hashCode
==
_controllerHashcode
)
{
...
...
Please
register
or
login
to post a comment