Julian Steenbakker

feat: add image picker from gallery for android

... ... @@ -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)
... ...
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 - 120,
width: MediaQuery.of(context).size.width - 180,
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);
}
},
),
],
),
),
... ...
... ... @@ -6,6 +6,7 @@ environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
image_picker: ^0.8.4+9
flutter:
sdk: flutter
... ...
... ... @@ -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) {
... ...