Android 项目 NullPointerException on try to call Files.readAllBytes(Paths.get( "Image Path" ))) 的位图图像



我正在尝试使用github上某人的一些API:https://github.com/LintraMax/SourceAFIS-Android 为了比较我的考勤项目中的 2 个指纹,基本上我从 bytes[] 转换为位图,然后保存到本地存储并生成一个 uri 来调用比较 2 个指纹的方法。每次我都得到这个异常,它看起来像在安卓方法中 "Paths.get(((((("无法获取图像。

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;
}

错误

E/Android运行时:致命异常:main 流程: com.andrushka.schooloutation, 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(

位图图像来自 arrayList of bytes[],其中包含 2 个合法且有效的指纹数组,因为我将它们导出.jpg本地 android 数据应用程序文件夹中,您可以在设备资源管理器中看到它们,因此 bytes[] 数组很好。您可以在下面查看图片(证明(。

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();
}

在此处输入图像描述

从错误来看,我假设您的probe对象是null.我猜assert语句默认情况下不适用于 Android。断言是在Java 1.4中引入的,但是要处理以前的代码库,你必须在JVM中手动启用它,因为在断言之前是允许命名的,而不是关键字。

现在,在 Android 中,您可以在模拟器/设备运行时通过adb在特定设备上启用断言(它将在设备重新启动之前一直工作(:

$ adb shell setprop debug.assert 1

但我建议您手动检查可空性,并记录结果,这样您就可以在将对象传递给FingerPrint匹配器之前查看对象的外观。

现在,为什么probe为空?我猜你没有正确传递图像字节,你可以检查这个线程如何正确做到这一点:https://stackoverflow.com/a/22116684/13709773。

最新更新