Julian Steenbakker

feat: add image picker from gallery for android

@@ -4,6 +4,7 @@ import android.Manifest @@ -4,6 +4,7 @@ import android.Manifest
4 import android.app.Activity 4 import android.app.Activity
5 import android.content.pm.PackageManager 5 import android.content.pm.PackageManager
6 import android.graphics.Point 6 import android.graphics.Point
  7 +import android.net.Uri
7 import android.util.Log 8 import android.util.Log
8 import android.util.Size 9 import android.util.Size
9 import android.view.Surface 10 import android.view.Surface
@@ -22,6 +23,8 @@ import io.flutter.plugin.common.MethodCall @@ -22,6 +23,8 @@ import io.flutter.plugin.common.MethodCall
22 import io.flutter.plugin.common.MethodChannel 23 import io.flutter.plugin.common.MethodChannel
23 import io.flutter.plugin.common.PluginRegistry 24 import io.flutter.plugin.common.PluginRegistry
24 import io.flutter.view.TextureRegistry 25 import io.flutter.view.TextureRegistry
  26 +import java.io.File
  27 +import java.net.URI
25 28
26 29
27 class MobileScanner(private val activity: Activity, private val textureRegistry: TextureRegistry) 30 class MobileScanner(private val activity: Activity, private val textureRegistry: TextureRegistry)
@@ -50,6 +53,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -50,6 +53,7 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
50 "torch" -> switchTorch(call, result) 53 "torch" -> switchTorch(call, result)
51 "analyze" -> switchAnalyzeMode(call, result) 54 "analyze" -> switchAnalyzeMode(call, result)
52 "stop" -> stop(result) 55 "stop" -> stop(result)
  56 + "analyzeImage" -> analyzeImage(call, result)
53 else -> result.notImplemented() 57 else -> result.notImplemented()
54 } 58 }
55 } 59 }
@@ -197,6 +201,23 @@ class MobileScanner(private val activity: Activity, private val textureRegistry: @@ -197,6 +201,23 @@ class MobileScanner(private val activity: Activity, private val textureRegistry:
197 result.success(null) 201 result.success(null)
198 } 202 }
199 203
  204 + private fun analyzeImage(call: MethodCall, result: MethodChannel.Result) {
  205 + val uri = Uri.fromFile( File(call.arguments.toString()))
  206 + val inputImage = InputImage.fromFilePath(activity, uri)
  207 +
  208 + scanner.process(inputImage)
  209 + .addOnSuccessListener { barcodes ->
  210 + for (barcode in barcodes) {
  211 + val event = mapOf("name" to "barcode", "data" to barcode.data)
  212 + sink?.success(event)
  213 + }
  214 + }
  215 + .addOnFailureListener { e -> Log.e(TAG, e.message, e)
  216 + result.error(TAG, e.message, e)}
  217 + .addOnCompleteListener { result.success(null) }
  218 +
  219 + }
  220 +
200 private fun stop(result: MethodChannel.Result) { 221 private fun stop(result: MethodChannel.Result) {
201 val owner = activity as LifecycleOwner 222 val owner = activity as LifecycleOwner
202 camera!!.cameraInfo.torchState.removeObservers(owner) 223 camera!!.cameraInfo.torchState.removeObservers(owner)
1 import 'package:flutter/material.dart'; 1 import 'package:flutter/material.dart';
  2 +import 'package:image_picker/image_picker.dart';
2 import 'package:mobile_scanner/mobile_scanner.dart'; 3 import 'package:mobile_scanner/mobile_scanner.dart';
3 4
4 void main() { 5 void main() {
@@ -73,7 +74,7 @@ class _AnalyzeViewState extends State<AnalyzeView> @@ -73,7 +74,7 @@ class _AnalyzeViewState extends State<AnalyzeView>
73 ), 74 ),
74 Center( 75 Center(
75 child: SizedBox( 76 child: SizedBox(
76 - width: MediaQuery.of(context).size.width - 120, 77 + width: MediaQuery.of(context).size.width - 180,
77 height: 50, 78 height: 50,
78 child: FittedBox( 79 child: FittedBox(
79 child: Text( 80 child: Text(
@@ -103,6 +104,19 @@ class _AnalyzeViewState extends State<AnalyzeView> @@ -103,6 +104,19 @@ class _AnalyzeViewState extends State<AnalyzeView>
103 iconSize: 32.0, 104 iconSize: 32.0,
104 onPressed: () => controller.switchCamera(), 105 onPressed: () => controller.switchCamera(),
105 ), 106 ),
  107 + IconButton(
  108 + color: Colors.white,
  109 + icon: Icon(Icons.browse_gallery),
  110 + iconSize: 32.0,
  111 + onPressed: () async {
  112 + final ImagePicker _picker = ImagePicker();
  113 + // Pick an image
  114 + final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
  115 + if (image != null) {
  116 + controller.analyzeImage(image.path);
  117 + }
  118 + },
  119 + ),
106 ], 120 ],
107 ), 121 ),
108 ), 122 ),
@@ -6,6 +6,7 @@ environment: @@ -6,6 +6,7 @@ environment:
6 sdk: ">=2.12.0 <3.0.0" 6 sdk: ">=2.12.0 <3.0.0"
7 7
8 dependencies: 8 dependencies:
  9 + image_picker: ^0.8.4+9
9 flutter: 10 flutter:
10 sdk: flutter 11 sdk: flutter
11 12
@@ -170,6 +170,10 @@ class MobileScannerController { @@ -170,6 +170,10 @@ class MobileScannerController {
170 start(); 170 start();
171 } 171 }
172 172
  173 + Future<void> analyzeImage(dynamic path) async {
  174 + await methodChannel.invokeMethod('analyzeImage', path);
  175 + }
  176 +
173 /// Disposes the controller and closes all listeners. 177 /// Disposes the controller and closes all listeners.
174 void dispose() { 178 void dispose() {
175 if (hashCode == _controllerHashcode) { 179 if (hashCode == _controllerHashcode) {