Trial 11022025

This commit is contained in:
2025-02-11 09:10:36 +07:00
parent 30ef123832
commit 00f9852fa8
29 changed files with 596 additions and 281 deletions

View File

@@ -22,9 +22,7 @@ import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.stage.DirectoryChooser;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
@@ -55,15 +53,21 @@ public class CaptureView {
private TextArea directorypath, prefixfile;
@FXML
private Button btnTakePhoto;
@FXML
private Button btnAutoFocus;
@FXML
private AnchorPane progressanchor;
private AudioPlayer audioPlayer;
private String audio_posisikan_muka = "satu.wav";
private String audio_posisi_diam = "dua.wav";
private String audio_foto_selesai = "tiga.wav";
private String audio_ke_ruangtunggu = "empat.wav";
private String audio_posisikan_muka = "posisikan_wajah.wav";
private String audio_scan_barcode = "scan_barcode.wav";
private String audio_pengambilan_gagal = "pengambilan_gagal.wav";
private String audio_pengambilan_berhasil = "pengambilan_berhasil.wav";
private String audio_upload_berhasil = "upload_berhasil.wav";
private String audio_upload_gagal = "upload_gagal.wav";
private String audio_countdown = "countdown321.wav";
private List<String> cams;
@@ -114,166 +118,191 @@ public class CaptureView {
@FXML
private void TakePhotos(){
boolean has_face = Arrays.stream(have_face).anyMatch(AtomicBoolean::get);
if (!has_face){
audioPlayer.PlayFile(audio_posisikan_muka, ps);
return;
}
Size thumbsize = new Size(160,120);
String directory = directorypath.getText();
String prefix = RemoveSpaces(prefixfile.getText()) ;
if (ValidDirectory(directory)){
if (ValidString(prefix)){
isTakingPhoto.set(true);
//audioPlayer.PlayFile(audio_posisi_diam, ps);
PhotoReviewClass prc = new PhotoReviewClass();
prc.setPrefix(prefix);
if (has_face){
if (audioPlayer.isPlaying()) audioPlayer.StopCurrentPlayback();
audioPlayer.PlayFile(audio_countdown, ps);
long nanostart = System.nanoTime(); // for performance measurement
btnAutoFocus.fire();
ExecutorService executor = Executors.newFixedThreadPool(5);
Size thumbsize = new Size(160,120);
String directory = directorypath.getText();
String prefix = RemoveSpaces(prefixfile.getText()) ;
if (ValidDirectory(directory)){
if (ValidPatientID(prefix)){
Callable<String> task1 = ()->{
if (image1!=null) {
String p1 = image1.TakePhoto(directory,prefix);
if (ValidFile(p1)) {
Platform.runLater(()->image1.setCameraStatus("Photo: "+ SomeCodes.GetFileName(p1)));
PhotoReviewClass prc = new PhotoReviewClass();
prc.setPrefix(prefix);
prc.setFileLeft90(p1);
String thumb1 = MakeThumbfile(p1, thumbsize);
if (ValidFile(thumb1)) prc.setThumbLeft90(thumb1);
long nanostart = System.nanoTime(); // for performance measurement
ExecutorService executor = Executors.newFixedThreadPool(5);
Callable<String> task1 = ()->{
if (image1!=null) {
image1.RemapROI();
String p1 = image1.TakePhoto(directory,prefix, image1.getBestMatROI(), image1.getReducedMatROI());
if (ValidFile(p1)) {
Platform.runLater(()->image1.setCameraStatus("Photo: "+ SomeCodes.GetFileName(p1)));
prc.setFileLeft90(p1);
String thumb1 = MakeThumbfile(p1, thumbsize);
if (ValidFile(thumb1)) {
prc.setThumbLeft90(thumb1);
}
} else System.out.println("Image1 ValidFile is false");
} else System.out.println("Image1 is null");
return "Task 1 Done";
};
Callable<String> task2 = ()->{
if (image2!=null) {
image2.RemapROI();
String p2 = image2.TakePhoto(directory,prefix, image2.getBestMatROI(), image2.getReducedMatROI());
if (ValidFile(p2)) {
Platform.runLater(()->image2.setCameraStatus("Photo: "+ SomeCodes.GetFileName(p2)));
prc.setFileLeft45(p2);
String thumb2 = MakeThumbfile(p2, thumbsize);
if (ValidFile(thumb2)) prc.setThumbLeft45(thumb2);
} else System.out.println("Image2 ValidFile is false");
} else System.out.println("Image2 is null");
return "Task 2 Done";
};
Callable<String> task3 = ()->{
if (image3!=null) {
image3.RemapROI();
String p3 = image3.TakePhoto(directory,prefix, image3.getBestMatROI(), image3.getReducedMatROI());
if (ValidFile(p3)) {
Platform.runLater(()->image3.setCameraStatus("Photo: "+ SomeCodes.GetFileName(p3)));
prc.setFileCenter(p3);
String thumb3 = MakeThumbfile(p3, thumbsize);
if (ValidFile(thumb3)) prc.setThumbCenter(thumb3);
} else System.out.println("Image3 ValidFile is false");
} else System.out.println("Image3 is null");
return "Task 3 Done";
};
Callable<String> task4 = ()->{
if (image4!=null) {
image4.RemapROI();
String p4 = image4.TakePhoto(directory,prefix, image4.getBestMatROI(), image4.getReducedMatROI());
if (ValidFile(p4)) {
Platform.runLater(()->image4.setCameraStatus("Photo: "+ SomeCodes.GetFileName(p4)));
prc.setFileRight45(p4);
String thumb4 = MakeThumbfile(p4, thumbsize);
if (ValidFile(thumb4)) prc.setThumbRight45(thumb4);
} else System.out.println("Image4 ValidFile is false");
} else System.out.println("Image4 is null");
return "Task 4 Done";
};
Callable<String> task5 = ()->{
if (image5!=null) {
image5.RemapROI();
String p5 = image5.TakePhoto(directory,prefix, image5.getBestMatROI(), image5.getReducedMatROI());
if (ValidFile(p5)) {
Platform.runLater(()->image5.setCameraStatus("Photo: "+ SomeCodes.GetFileName(p5)));
prc.setFileRight90(p5);
String thumb5 = MakeThumbfile(p5, thumbsize);
if (ValidFile(thumb5)) prc.setThumbRight90(thumb5);
} else System.out.println("Image5 ValidFile is false");
} else System.out.println("Image5 is null");
return "Task 5 Done";
};
try{
Future<String> f1 = executor.submit(task1);
Future<String> f2 = executor.submit(task2);
Future<String> f3 = executor.submit(task3);
Future<String> f4 = executor.submit(task4);
Future<String> f5 = executor.submit(task5);
f1.get();
f2.get();
f3.get();
f4.get();
f5.get();
} catch (Exception e){
Logger.error("Error TakePhotos: " + e.getMessage());
} finally {
executor.shutdown();
}
long duration = (System.nanoTime() - nanostart) / 1000000; // in milliseconds
System.out.println("TakePhotos duration: "+duration+" ms");
if (audioPlayer.isPlaying()) {
if (!audio_pengambilan_berhasil.equals((audioPlayer.getCurrentFile()))){
audioPlayer.StopCurrentPlayback();
}
}
return "Task 1 Done";
};
Callable<String> task2 = ()->{
if (image2!=null) {
String p2 = image2.TakePhoto(directory,prefix);
if (ValidFile(p2)) {
Platform.runLater(()->image2.setCameraStatus("Photo: "+ SomeCodes.GetFileName(p2)));
audioPlayer.PlayFile(audio_pengambilan_berhasil, ps);
String[] files = prc.files();
if (files!=null && files.length>0){
InsertSQL(prc);
prc.setFileLeft45(p2);
String thumb2 = MakeThumbfile(p2, thumbsize);
if (ValidFile(thumb2)) prc.setThumbLeft45(thumb2);
}
progressanchor.getChildren().clear();
prefixfile.setText("");
//TODO ganti ke API upload
// new Thread(()-> {
// try{
// InetAddress target = InetAddress.getByName(config.getFTPHost());
// int port = toInt(config.getFTPPort());
// if (target.isReachable(2000)){
// Socket socket = new Socket();
// InetSocketAddress isa = new InetSocketAddress(target, port);
// socket.connect(isa,2000);
// socket.close();
// // sampai sini berarti FTP Host reachable
// UploadToFTP(files);
// } else throw new Exception("FTP Host not reachable");
// } catch (Exception e){
// Logger.error("Error UploadToFTP: "+e.getMessage());
// Platform.runLater(()->{
// Alert alert = new Alert(AlertType.ERROR);
// alert.setTitle("Error");
// alert.setHeaderText("Upload to FTP Failed");
// alert.setContentText("Error: "+e.getMessage());
// alert.showAndWait();
// });
//
// }
// }).start();
} else {
Alert Alert = new Alert(AlertType.ERROR);
Alert.setTitle("Error");
Alert.setHeaderText("No Photos Taken");
Alert.setContentText("No Photos Taken, please check camera");
Alert.showAndWait();
}
return "Task 2 Done";
};
Callable<String> task3 = ()->{
if (image3!=null) {
String p3 = image3.TakePhoto(directory,prefix);
if (ValidFile(p3)) {
Platform.runLater(()->image3.setCameraStatus("Photo: "+ SomeCodes.GetFileName(p3)));
prc.setFileCenter(p3);
String thumb3 = MakeThumbfile(p3, thumbsize);
if (ValidFile(thumb3)) prc.setThumbCenter(thumb3);
}
}
return "Task 3 Done";
};
Callable<String> task4 = ()->{
if (image4!=null) {
String p4 = image4.TakePhoto(directory,prefix);
if (ValidFile(p4)) {
Platform.runLater(()->image4.setCameraStatus("Photo: "+ SomeCodes.GetFileName(p4)));
prc.setFileRight45(p4);
String thumb4 = MakeThumbfile(p4, thumbsize);
if (ValidFile(thumb4)) prc.setThumbRight45(thumb4);
}
}
return "Task 4 Done";
};
Callable<String> task5 = ()->{
if (image5!=null) {
String p5 = image5.TakePhoto(directory,prefix);
if (ValidFile(p5)) {
Platform.runLater(()->image5.setCameraStatus("Photo: "+ SomeCodes.GetFileName(p5)));
prc.setFileRight90(p5);
String thumb5 = MakeThumbfile(p5, thumbsize);
if (ValidFile(thumb5)) prc.setThumbRight90(thumb5);
}
}
return "Task 5 Done";
};
try{
Future<String> f1 = executor.submit(task1);
Future<String> f2 = executor.submit(task2);
Future<String> f3 = executor.submit(task3);
Future<String> f4 = executor.submit(task4);
Future<String> f5 = executor.submit(task5);
f1.get();
f2.get();
f3.get();
f4.get();
f5.get();
} catch (Exception e){
Logger.error("Error TakePhotos: " + e.getMessage());
} finally {
executor.shutdown();
}
long duration = (System.nanoTime() - nanostart) / 1000000; // in milliseconds
System.out.println("TakePhotos duration: "+duration+" ms");
// TODO test taruh sini
isTakingPhoto.set(false);
audioPlayer.PlayFile(audio_foto_selesai, ps);
String[] files = prc.files();
if (files!=null && files.length>0){
InsertSQL(prc);
progressanchor.getChildren().clear();
prefixfile.setText("");
new Thread(()-> {
try{
InetAddress target = InetAddress.getByName(config.getFTPHost());
int port = toInt(config.getFTPPort());
if (target.isReachable(2000)){
Socket socket = new Socket();
InetSocketAddress isa = new InetSocketAddress(target, port);
socket.connect(isa,2000);
socket.close();
// sampai sini berarti FTP Host reachable
UploadToFTP(files);
} else throw new Exception("FTP Host not reachable");
} catch (Exception e){
Logger.error("Error UploadToFTP: "+e.getMessage());
Platform.runLater(()->{
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Upload to FTP Failed");
alert.setContentText("Error: "+e.getMessage());
alert.showAndWait();
});
}
}).start();
} else {
Alert Alert = new Alert(AlertType.ERROR);
Alert.setTitle("Error");
Alert.setHeaderText("No Photos Taken");
Alert.setContentText("No Photos Taken, please check camera");
Alert.setHeaderText("Invalid Prefix");
Alert.setContentText("Please input valid prefix or scan QR Code");
Alert.showAndWait();
}
} else {
Alert Alert = new Alert(AlertType.ERROR);
Alert.setTitle("Error");
Alert.setHeaderText("Invalid Prefix");
Alert.setContentText("Please input valid prefix or scan QR Code");
Alert.setHeaderText("Invalid Directory");
Alert.setContentText("Please select valid directory");
Alert.showAndWait();
}
} else {
Alert Alert = new Alert(AlertType.ERROR);
Alert.setTitle("Error");
Alert.setHeaderText("Invalid Directory");
Alert.setContentText("Please select valid directory");
Alert.showAndWait();
if (audioPlayer.isPlaying()) {
if (!audio_posisikan_muka.equals((audioPlayer.getCurrentFile()))){
audioPlayer.StopCurrentPlayback();
}
}
audioPlayer.PlayFile(audio_posisikan_muka, ps);
}
isTakingPhoto.set(false);
}
private void UpdateBtnTakePhoto(){
@@ -283,10 +312,13 @@ public class CaptureView {
@FXML
public void initialize(){
audio_posisikan_muka = ExtractResource("/satu.wav");
audio_posisi_diam = ExtractResource("/dua.wav");
audio_foto_selesai = ExtractResource("/tiga.wav");
audio_ke_ruangtunggu = ExtractResource("/empat.wav");
audio_posisikan_muka = ExtractResource("/posisikan_wajah.wav");
audio_pengambilan_berhasil = ExtractResource("/pengambilan_berhasil.wav");
audio_pengambilan_gagal = ExtractResource("/pengambilan_gagal.wav");
audio_scan_barcode = ExtractResource("/scan_barcode.wav");
audio_upload_berhasil = ExtractResource("/upload_berhasil.wav");
audio_upload_gagal = ExtractResource("/upload_gagal.wav");
audio_countdown = ExtractResource("/countdown321.wav");
audioPlayer = new AudioPlayer(1,48000);
Logger.info("Audio Player : "+(audioPlayer.isInited()? "Inited" : "Not Inited"));
@@ -416,12 +448,16 @@ public class CaptureView {
public void onPlaybackStarted(String filename) {
if (filename.contains(audio_posisikan_muka)){
Logger.info("Audio Positikan Muka Started");
} else if (filename.contains(audio_posisi_diam)){
Logger.info("Audio Posisi Diam Started");
} else if (filename.contains(audio_foto_selesai)){
Logger.info("Audio Foto Selesai Started");
} else if (filename.contains(audio_ke_ruangtunggu)){
Logger.info("Audio Ke Ruang Tunggu Started");
} else if (filename.contains(audio_pengambilan_berhasil)){
Logger.info("Audio Pengambilan Berhasil Started");
} else if (filename.contains(audio_pengambilan_gagal)){
Logger.info("Audio Pengambilan Gagal Started");
} else if (filename.contains(audio_scan_barcode)){
Logger.info("Audio Scan Barcode Started");
} else if (filename.contains(audio_upload_berhasil)){
Logger.info("Audio Upload Berhasil Started");
} else if (filename.contains(audio_upload_gagal)){
Logger.info("Audio Upload Gagal Started");
}
}
@@ -429,12 +465,16 @@ public class CaptureView {
public void onPlaybackFinished(String filename) {
if (filename.contains(audio_posisikan_muka)){
Logger.info("Audio Positikan Muka Finished");
} else if (filename.contains(audio_posisi_diam)){
Logger.info("Audio Posisi Diam Finished");
} else if (filename.contains(audio_foto_selesai)){
Logger.info("Audio Foto Selesai Finished");
} else if (filename.contains(audio_ke_ruangtunggu)){
Logger.info("Audio Ke Ruang Tunggu Finished");
} else if (filename.contains(audio_scan_barcode)){
Logger.info("Audio Scan Barcode Finished");
} else if (filename.contains(audio_upload_berhasil)){
Logger.info("Audio Upload Berhasil Finished");
} else if (filename.contains(audio_upload_gagal)){
Logger.info("Audio Upload Gagal Finished");
} else if (filename.contains(audio_pengambilan_berhasil)){
Logger.info("Audio Pengambilan Berhasil Finished");
} else if (filename.contains(audio_pengambilan_gagal)){
Logger.info("Audio Pengambilan Gagal Finished");
}
}
@@ -442,12 +482,16 @@ public class CaptureView {
public void onPlaybackFailure(String filename) {
if (filename.contains(audio_posisikan_muka)){
Logger.info("Audio Positikan Muka Failure");
} else if (filename.contains(audio_posisi_diam)){
Logger.info("Audio Posisi Diam Failure");
} else if (filename.contains(audio_foto_selesai)){
Logger.info("Audio Foto Selesai Failure");
} else if (filename.contains(audio_ke_ruangtunggu)){
Logger.info("Audio Ke Ruang Tunggu Failure");
} else if (filename.contains(audio_upload_gagal)){
Logger.info("Audio Upload Gagal Failure");
} else if (filename.contains(audio_upload_berhasil)){
Logger.info("Audio Upload Berhasil Failure");
} else if (filename.contains(audio_pengambilan_berhasil)){
Logger.info("Audio Pengambilan Berhasil Failure");
} else if (filename.contains(audio_pengambilan_gagal)){
Logger.info("Audio Pengambilan Gagal Failure");
} else if (filename.contains(audio_scan_barcode)){
Logger.info("Audio Scan Barcode Failure");
}
}
};
@@ -564,50 +608,72 @@ public class CaptureView {
LiveCamEvent lce = new LiveCamEvent() {
@Override
public void onDetectedQRCode(String qrCode) {
Platform.runLater(()->prefixfile.setText(RemoveSpaces(qrCode)));
//if (ValidString(qrCode)) audioPlayer.PlayFile(audio_posisikan_muka, ps);
qrCode = RemoveSpaces(qrCode);
if (ValidPatientID(qrCode)){
String prefix = prefixfile.getText();
if (!qrCode.equals(prefix)){
String finalQrCode = qrCode;
System.out.println("PatientID detected: "+finalQrCode);
Platform.runLater(()->prefixfile.setText(finalQrCode));
if (audioPlayer.isPlaying()) {
if (!audio_posisikan_muka.equals((audioPlayer.getCurrentFile()))){
audioPlayer.StopCurrentPlayback();
}
}
audioPlayer.PlayFile(audio_posisikan_muka, ps);
}
}
}
@Override
public void onFrontalFaceDetector(boolean hasface, int width, int height) {
_have_face.set(hasface);
update_status(image);
if (hasface!= _have_face.get()){
_have_face.set(hasface);
update_status(image);
}
}
@Override
public void onProfileFaceDetector(boolean hasface, int width, int height) {
_have_profile.set(hasface);
update_status(image);
if (hasface!= _have_profile.get()){
_have_profile.set(hasface);
update_status(image);
}
}
@Override
public void onEyeDetector(boolean hasEye, int width, int height) {
_have_eye.set(hasEye);
update_status(image);
// _have_eye.set(hasEye);
// update_status(image);
}
@Override
public void onLeftEarDetector(boolean hasLeftEar, int width, int height) {
_have_left_ear.set(hasLeftEar);
update_status(image);
// _have_left_ear.set(hasLeftEar);
// update_status(image);
}
@Override
public void onRightEarDetector(boolean hasRightEar, int width, int height) {
_have_right_ear.set(hasRightEar);
update_status(image);
// _have_right_ear.set(hasRightEar);
// update_status(image);
}
@Override
public void onLeftEyeDetector(boolean hasLeftEye, int width, int height) {
_have_left_eye.set(hasLeftEye);
update_status(image);
// _have_left_eye.set(hasLeftEye);
// update_status(image);
}
@Override
public void onRightEyeDetector(boolean hasRightEye, int width, int height) {
_have_right_eye.set(hasRightEye);
update_status(image);
// _have_right_eye.set(hasRightEye);
// update_status(image);
}
@Override
@@ -618,23 +684,26 @@ public class CaptureView {
@Override
public void onBlink(int counter) {
if (isTakingPhoto.get()) return; // other camera is taking picture
System.out.println("Blink detected at camera "+title+" delay= "+counter);
isTakingPhoto.set(true);
Platform.runLater(()->{
// System.out.println("Blink detected at camera "+title+" counter: "+counter);
// Platform.runLater(()->{
// Alert Alert = new Alert(AlertType.INFORMATION);
// Alert.setTitle("Blink Detected");
// Alert.setHeaderText("Blink Detected");
// Alert.setContentText("Blink Detected at Camera "+title+" counter: "+counter);
// Alert.show();
//
// String prefix = prefixfile.getText();
// //TODO Pastikan Nomor Pelanggan 8 digit
// if (prefix.length()>=8){
// if (isTakingPhoto.get()) return; // other camera already detect blinking
// btnTakePhoto.fire();
// }
// });
String prefix = prefixfile.getText();
if (prefix.length()==10){
System.out.println("Prefix valid, taking photo");
btnTakePhoto.fire();
} else {
System.out.println("Prefix not valid, not taking photo");
isTakingPhoto.set(false);
if (audioPlayer.isPlaying()) {
if (!audio_scan_barcode.equals((audioPlayer.getCurrentFile()))){
audioPlayer.StopCurrentPlayback();
}
}
audioPlayer.PlayFile(audio_scan_barcode, ps);
}
});
}
@@ -739,6 +808,7 @@ public class CaptureView {
}
}
@Deprecated
private void UploadToFTP(String[] files){
final double uploadprogressheight = 50;
@@ -846,7 +916,8 @@ public class CaptureView {
public void onUploadFinished(int total, int success, int failed, String[] files) {
Logger.info("Upload Finished, Total: {}, Success: {}, Failed: {}", total, success, failed);
Platform.runLater(()->{
audioPlayer.PlayFile(audio_ke_ruangtunggu, ps);
if (audioPlayer.isPlaying()) audioPlayer.StopCurrentPlayback();
audioPlayer.PlayFile(audio_upload_berhasil, ps);
Alert Alert = new Alert(AlertType.INFORMATION);
Alert.setTitle("Upload Finished");
Alert.setHeaderText("Upload Finished");