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

PerceptualHash algorithm does not work and give me Invalid value: Valid value range is empty #15

Open
mohShahbal opened this issue Aug 12, 2024 · 0 comments

Comments

@mohShahbal
Copy link

The Error:

E/flutter ( 6730): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: RangeError (index): Invalid value: Valid value range is empty: 0
E/flutter ( 6730): #0 List.[] (dart:core-patch/growable_array.dart:264:36)
E/flutter ( 6730): #1 PerceptualHash.calcPhash (package:image_compare_2/src/algorithms.dart:400:28)
E/flutter ( 6730): #2 PerceptualHash.compare (package:image_compare_2/src/algorithms.dart:382:17)
E/flutter ( 6730): #3 compareImages (package:image_compare_2/src/functions.dart:26:26)
E/flutter ( 6730):
E/flutter ( 6730): #4 _ImageCompareState.compare (package:flutter_application_1/main.dart:95:9)
E/flutter ( 6730):
E/flutter ( 6730):

my code;

import 'dart:developer';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_compare_2/image_compare_2.dart';
import 'package:image_picker/image_picker.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

void main() {
WidgetsFlutterBinding.ensureInitialized();
WakelockPlus.enable();
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const ImageCompare(),
);
}
}

class ImageCompare extends StatefulWidget {
const ImageCompare({super.key});

@OverRide
State createState() => _ImageCompareState();
}

class _ImageCompareState extends State {
File? image1;
File? image2;
double diff = 0.0;
@OverRide
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () => compare(image1!, image2!),
),
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
onPressed: () async {
image1 = await pickImage();
},
child: const Text('image 1'),
),
const SizedBox(height: 20),
TextButton(
onPressed: () async {
image2 = await pickImage();
},
child: const Text('image 2'),
),
const SizedBox(height: 20),
Text("Difference: ${diff.toStringAsFixed(0)}%")
],
),
),
),
);
}

Future<File?> pickImage() async {
final ImagePicker picker = ImagePicker();
final XFile? image = await picker.pickImage(
source: ImageSource.camera,
imageQuality: 10,
maxHeight: 400,
maxWidth: 400);
if (image != null) {
log(image.path);
return File(image.path);
} else {
return null;
}
}

void compare(File file1, File file2) async {
log(file1.path);
log(file2.path);

var networkResult =
    await compareImages(src1: file1, src2: file2, algorithm: PerceptualHash());

// ignore: avoid_function_literals_in_foreach_calls
setState(() => diff = networkResult * 100);
log('Difference: ${(networkResult * 100)}%');

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant