0

I'm trying to use some API from someone on github: https://github.com/LintraMax/SourceAFIS-Android to compare 2 fingerprints on my attendance project, basically I convert from bytes[] to bitmap then I save to local storage and generate a uri to call the method to compare 2 fingerprints. I get this exception, every time, it looks like in android method "Paths.get()))))" is unable to get the image.

private double compareFingerPrints(Bitmap bitmapProbe, Bitmap bitmapCandidate){

        boolean matches = false;
        double score = 0;
        Uri uriProbe = saveReceivedImage(bitmapProbe);
        Uri uriCandidate = saveReceivedImage(bitmapCandidate);
        FingerprintTemplate probe = null, candidate = null;

        try {
            probe = new FingerprintTemplate(
                    new FingerprintImage()
                            .dpi(500)
                            .decode(Files.readAllBytes(Paths.get(String.valueOf(uriProbe)))));

            candidate = new FingerprintTemplate(
                    new FingerprintImage()
                            .dpi(500)
                            .decode(Files.readAllBytes(Paths.get(String.valueOf(uriCandidate)))));

        } catch (Exception e) {
            e.printStackTrace();
        }
        assert probe != null;
        assert candidate != null;

        score = new FingerprintMatcher().index(probe).match(candidate);

        matches = score >= 40;

        return score;

    }



 private Uri saveReceivedImage(Bitmap bitmap) {
        String filePath = null;
        Uri uri = null;
        try {
            String imageName = "FingerPrint_" + Timestamp.now().getSeconds();
            File path = new File(getContext().getFilesDir(), "FingerPrints" + File.separator);
            if (!path.exists()) {
                path.mkdirs();
            }
            File outFile = new File(path, imageName + ".jpeg");
            FileOutputStream outputStream = new FileOutputStream(outFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            uri = Uri.fromFile(outFile);
            outputStream.close();
        } catch (FileNotFoundException e) {
            Log.e(TAG_SAVE_LOCAL, "Saving received message failed with", e);
        } catch (IOException e) {
            Log.e(TAG_SAVE_LOCAL, "Saving received message failed with", e);
        }
        return uri;
    }

error

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.andrushka.schoolattendance, PID: 6255 java.lang.NullPointerException at java.util.Objects.requireNonNull(Objects.java:203) at com.machinezoo.sourceafis.FingerprintMatcher.index(FingerprintMatcher.java:84) at com.andrushka.studentattendance.fragments.AttendanceFragment.compareFingerPrints(AttendanceFragment.java:552) at com.andrushka.studentattendance.fragments.AttendanceFragment.createPopupAttendanceScanFingerPrint(AttendanceFragment.java:454) at com.andrushka.studentattendance.fragments.AttendanceFragment.access$200(AttendanceFragment.java:69) at com.andrushka.studentattendance.fragments.AttendanceFragment$3.onClick(AttendanceFragment.java:183)

The bitmap images, coming from an arrayList of bytes[], which has 2 fingerprints arrays that are legit and work, cause I exported them .jpg in local android data app folder, where u can see them in device explorer, so the bytes[] arrays are good. U can check the pic down below (proof).

   private void createPopupAttendanceScanFingerPrint(View view) throws IOException {
            builder = new AlertDialog.Builder(view.getContext());
            final View viewDialog = getLayoutInflater().inflate(R.layout.attendance_maker, null);

            tvMessage = viewDialog.findViewById(R.id.tvMessage);
            tvError = viewDialog.findViewById(R.id.tvError);
            tvStatus = viewDialog.findViewById(R.id.tvStatus);
            ivFinger = viewDialog.findViewById(R.id.ivFingerDisplay);
            fingerPrintImageDisplay = viewDialog.findViewById(R.id.displayFingerPrintDB);
            fingerPrintTextViewState = viewDialog.findViewById(R.id.fingerPrintMatchState);

            buttonScan = viewDialog.findViewById(R.id.buttonScan);
            buttonScan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    fingerprint.scan(viewDialog.getContext(), printHandler, updateHandler);
                }
            });

        if (!bytesArrayList.isEmpty()) {
BitmapFactory.decodeByteArray(betterImageByteArray, 0,betterImageByteArray.length);
                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytesArrayList.get(0), 0, bytesArrayList.get(0).length);
                    Bitmap bitmapDb = BitmapFactory.decodeByteArray(bytesArrayList.get(1), 0, bytesArrayList.get(1).length);
                    fingerPrintImageDisplay.setImageBitmap(bitmap);


                    double comparisionScore = compareFingerPrints(bitmap, bitmapDb);

                    Toast.makeText(view.getContext(), "Comparision score : " + comparisionScore, Toast.LENGTH_LONG).show();
                    fingerPrintTextViewState.setText("Attendance status");

                } else {
                    Toast.makeText(view.getContext(), "Bytes not dehashed, byteArrayList empty", Toast.LENGTH_LONG).show();
                }
    builder.setView(viewDialog);
    dialog = builder.create();
    dialog.show();
}

enter image description here

  • Where are `uriProbe` and `uriCandidate` coming from? What do their values look like? The `uri` prefix suggests that they are `Uri` values, and most of the time, a `Uri` is not a file. – CommonsWare Jun 09 '20 at 20:01
  • I just updated my post, u can check and I uploaded a picture with finger print and my IDE, I used the a File method to save in local storage, but again my method with the 2 fingerprint comparision couldn't find it in there. – Andrei Razmerita Jun 09 '20 at 21:25
  • I agree with u here, I just tried with Uri uri, cause I thought I can get the method Paths.get() to fectch my picture - Bitmap format. But apparently is not a file.Like a said down, I tried everything. IntelJ, pics the .jpg,.png from local project directory and it works, does the comparision, I tested. I just in my android project, this methods don't know where to search the file, or just I don't know how to pass the file parth and how to save it befora that. and where?! – Andrei Razmerita Jun 09 '20 at 21:36

1 Answers1

0

From the error, I'm assuming that your probe object is null. I'm guessing the assert statement doesn't work on Android by default. Assert was introduced in Java 1.4, but to handle previous codebases, you have to enable it in JVM manually, since before assert was allowed naming, and not a keyword.

Now, in Android, you can enable assertions on a particular device through adb when the emulator/device is running (it will work until the device is restarted):

$ adb shell setprop debug.assert 1

But I would suggest you check for nullability manually and just maybe log the result, so you'll see how your object looks before passing it to the FingerPrint matcher.

Now, why is the probe null? I'm guessing you don't correctly pass the image bytes, you can check this thread on how to do it correctly: https://stackoverflow.com/a/22116684/13709773.

chef417
  • 494
  • 2
  • 7
  • Yes u are right, my probe and candidate are null, cause I can't get them to find my file, I have this project in java, and I tried in IntelJ ide, and it works. But in android, I don't know how to get the file so probe and candidate object can pick it. Should I put a image file in project or in device internal storage,no idea. I tried with absolute Path and a lot of staff and nothing.I can check with logs that for sure my byte[] are ok, but still, this decode method doesn't like byte[] array for some reasson, only pictures,from local storage.That works in Intelj,u put pics in the project folder – Andrei Razmerita Jun 09 '20 at 21:28
  • If you're deploying the images with your application, you need to put them into the assets directory. Then you can load them, see here on how to do it: https://stackoverflow.com/a/36199316/13709773 – chef417 Jun 09 '20 at 21:33
  • Ok, I will try that. I hope that I can get the file path, which im sure I can, and my methods accepts the path of the files. – Andrei Razmerita Jun 09 '20 at 21:40