Commit 18022025
This commit is contained in:
@@ -4,6 +4,7 @@ package Config;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.zxing.MultiFormatReader;
|
||||
import javafx.embed.swing.SwingFXUtils;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.image.Image;
|
||||
import org.bytedeco.javacv.Java2DFrameConverter;
|
||||
import org.bytedeco.javacv.OpenCVFrameConverter;
|
||||
@@ -129,13 +130,9 @@ public class SomeCodes {
|
||||
}
|
||||
}
|
||||
|
||||
public static Rect ScaleRect(Rect original, double scaleX, double scaleY){
|
||||
if (original!=null){
|
||||
return new Rect((int)(original.x()*scaleX), (int)(original.y()*scaleY),
|
||||
(int)(original.width()*scaleX), (int)(original.height()*scaleY));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Make thumbfile from source jpg file
|
||||
@@ -175,6 +172,11 @@ public class SomeCodes {
|
||||
return x.format(dtf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract resource file to current directory
|
||||
* @param filename resource file name
|
||||
* @return extracted file path if success, or null if failed
|
||||
*/
|
||||
public static String ExtractResource(String filename){
|
||||
try{
|
||||
File destination = new File(currentDirectory, filename);
|
||||
@@ -193,6 +195,11 @@ public class SomeCodes {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if path is valid directory
|
||||
* @param path directory path
|
||||
* @return true if valid directory, false if not valid directory
|
||||
*/
|
||||
public static boolean ValidDirectory(String path){
|
||||
if (ValidString(path)){
|
||||
File ff = new File(path);
|
||||
@@ -201,10 +208,20 @@ public class SomeCodes {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if port number is valid
|
||||
* @param port port number
|
||||
* @return true if valid port number, false if not valid port number
|
||||
*/
|
||||
public static boolean ValidPortNumber(int port){
|
||||
return port>0 && port<65536;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert string to integer
|
||||
* @param x string
|
||||
* @return integer value if success, or 0 if failed
|
||||
*/
|
||||
public static int toInt(String x){
|
||||
try {
|
||||
return Integer.parseInt(x);
|
||||
@@ -213,6 +230,11 @@ public class SomeCodes {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is valid IPV4 address
|
||||
* @param ipaddress IPV4 address
|
||||
* @return true if valid IPV4 address, false if not valid IPV4 address
|
||||
*/
|
||||
public static boolean ValidIPV4(String ipaddress){
|
||||
if (ValidString(ipaddress)){
|
||||
try{
|
||||
@@ -228,6 +250,11 @@ public class SomeCodes {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is valid IPV6 address
|
||||
* @param ipaddress IPV6 address
|
||||
* @return true if valid IPV6 address, false if not valid IPV6 address
|
||||
*/
|
||||
public static boolean ValidIPV6(String ipaddress){
|
||||
if (ValidString(ipaddress)){
|
||||
try{
|
||||
@@ -243,6 +270,11 @@ public class SomeCodes {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file name from file path
|
||||
* @param filepath file path
|
||||
* @return file name if success, or empty string if failed
|
||||
*/
|
||||
public static String GetFileName(String filepath){
|
||||
if (ValidString(filepath)){
|
||||
File ff = new File(filepath);
|
||||
@@ -253,6 +285,11 @@ public class SomeCodes {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is valid file
|
||||
* @param filename file name
|
||||
* @return true if valid file, false if not valid file
|
||||
*/
|
||||
public static boolean ValidFile(String filename){
|
||||
if (ValidString(filename)){
|
||||
File ff = new File(filename);
|
||||
@@ -261,6 +298,102 @@ public class SomeCodes {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file as byte array
|
||||
* @param filename file name
|
||||
* @return byte array if success, or null if failed
|
||||
*/
|
||||
public static byte[] ReadFile(String filename){
|
||||
if (ValidFile(filename)){
|
||||
try{
|
||||
return Files.readAllBytes(Path.of(filename));
|
||||
} catch (Exception e){
|
||||
Logger.error("Error reading file: "+filename+", Msg : "+e.getMessage());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Resize Rect
|
||||
* @param original original Rect
|
||||
* @param scaleX scale factor
|
||||
* @param scaleY scale factor
|
||||
* @param Xoffset X offset, positive value to increase width, negative value to decrease width
|
||||
* @param Yoffset Y offset, positive value to increase height, negative value to decrease height
|
||||
* @return resized Rect if success, or null if failed
|
||||
*/
|
||||
public static Rect ResizeRect(Rect original, double scaleX, double scaleY, int Xoffset, int Yoffset){
|
||||
if (original!=null){
|
||||
int newX = (int)(original.x()*scaleX);
|
||||
newX -= Xoffset;
|
||||
if (newX<0) newX = 0;
|
||||
int newY = (int)(original.y()*scaleY);
|
||||
newY -= Yoffset;
|
||||
if (newY<0) newY = 0;
|
||||
int newWidth = (int)(original.width()*scaleX);
|
||||
newWidth += Xoffset*2;
|
||||
int newHeight = (int)(original.height()*scaleX);
|
||||
newHeight += Yoffset*2;
|
||||
return new Rect(newX, newY, newWidth, newHeight);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static UMat CropUMat(UMat source, Rect ROI){
|
||||
if (source!=null && !source.empty() && ValidROI(ROI)){
|
||||
int x = ROI.x();
|
||||
int y = ROI.y();
|
||||
int width = ROI.width();
|
||||
int height = ROI.height();
|
||||
|
||||
if (x>=0 && y>=0 && width>0 && height>0){
|
||||
if (x+width>source.cols()) width = source.cols()-x;
|
||||
if (y+height>source.rows()) height = source.rows()-y;
|
||||
if (width>0 && height>0){
|
||||
Rect crop = new Rect(x, y, width, height);
|
||||
return new UMat(source, crop);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenate byte arrays
|
||||
* @param args byte arrays
|
||||
* @return concatenated byte array if success, or null if failed
|
||||
*/
|
||||
public static byte[] Concat(byte[]... args){
|
||||
if (args!=null && args.length>0){
|
||||
int total = 0;
|
||||
for(byte[] x : args){
|
||||
if (x!=null && x.length>0){
|
||||
total += x.length;
|
||||
}
|
||||
}
|
||||
if (total>0){
|
||||
byte[] result = new byte[total];
|
||||
int offset = 0;
|
||||
for(byte[] x : args){
|
||||
if (x!=null && x.length>0){
|
||||
System.arraycopy(x, 0, result, offset, x.length);
|
||||
offset += x.length;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is valid
|
||||
* @param x string
|
||||
* @return true if valid, false if not valid
|
||||
*/
|
||||
public static boolean ValidString(String x){
|
||||
if (x!=null){
|
||||
return !x.isEmpty();
|
||||
@@ -268,6 +401,10 @@ public class SomeCodes {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open picture in default viewer
|
||||
* @param filename picture file name
|
||||
*/
|
||||
public static void OpenPictureInDefaultViewer(String filename){
|
||||
try{
|
||||
File ff = new File(filename);
|
||||
@@ -299,6 +436,11 @@ public class SomeCodes {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Make array from string arguments
|
||||
* @param args string arguments
|
||||
* @return array of strings if success, or empty array if failed
|
||||
*/
|
||||
public static String[] MakeArray(String... args){
|
||||
if (args!=null && args.length>0){
|
||||
List<String> ll = new ArrayList<>();
|
||||
@@ -310,14 +452,28 @@ public class SomeCodes {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
public static boolean ValidBarCode(String value){
|
||||
if (value!=null && value.length()==10){
|
||||
boolean valid = true;
|
||||
for(int i=0; i<value.length(); i++){
|
||||
if (!Character.isDigit(value.charAt(i))){
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return valid;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Valid PatientID found
|
||||
* Patient ID is 10 digits number
|
||||
* @param patientid Patient ID
|
||||
* @return true if valid, false if not valid
|
||||
*/
|
||||
public static boolean ValidPatientID(String patientid){
|
||||
if (patientid!=null && patientid.length()==10){
|
||||
public static boolean ValidMedicalRecordId(String patientid){
|
||||
if (patientid!=null && !patientid.isEmpty()){
|
||||
boolean valid = true;
|
||||
for(int i=0; i<patientid.length(); i++){
|
||||
if (!Character.isDigit(patientid.charAt(i))){
|
||||
@@ -329,17 +485,32 @@ public class SomeCodes {
|
||||
} else return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check Region of Interest is a valid Rect
|
||||
* @param ROI Region of Interest
|
||||
* @return true if valid, false if not valid
|
||||
*/
|
||||
public static boolean ValidROI(Rect ROI){
|
||||
if (ROI!=null){
|
||||
if (ROI.width()>0){
|
||||
if (ROI.height()>0){
|
||||
return true;
|
||||
if (ROI.x()>=0){
|
||||
if (ROI.y()>=0){
|
||||
if (ROI.width()>0){
|
||||
if (ROI.height()>0){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Region of Interest is inside UMat
|
||||
* @param ROI Region of Interest
|
||||
* @param mat UMat
|
||||
* @return true if inside, false if not inside
|
||||
*/
|
||||
public static boolean ROIInsideUMat(Rect ROI, UMat mat){
|
||||
if (ValidROI(ROI)){
|
||||
if (mat!=null){
|
||||
@@ -351,6 +522,12 @@ public class SomeCodes {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Region of Interest 1 is same with Region of Interest 2
|
||||
* @param ROI1 Region of Interest 1
|
||||
* @param ROI2 Region of Interest 2
|
||||
* @return true if same, false if not same
|
||||
*/
|
||||
public static boolean IsSameROI(Rect ROI1, Rect ROI2){
|
||||
if (ValidROI(ROI1) && ValidROI(ROI2)){
|
||||
return ROI1.x()==ROI2.x() && ROI1.y()==ROI2.y() &&
|
||||
@@ -359,6 +536,12 @@ public class SomeCodes {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Rect 1 is inside Rect 2
|
||||
* @param smaller Rect 1
|
||||
* @param bigger Rect 2
|
||||
* @return true if inside, false if not inside
|
||||
*/
|
||||
public static boolean IsInsideRect(Rect smaller, Rect bigger){
|
||||
if (smaller!=null && bigger!=null){
|
||||
return smaller.x()>=bigger.x() && smaller.y()>=bigger.y() &&
|
||||
@@ -378,12 +561,30 @@ public class SomeCodes {
|
||||
if (!ff.isDirectory()){
|
||||
try{
|
||||
Files.createDirectories(ff.toPath());
|
||||
System.out.println("Directory created: "+path);
|
||||
Logger.info("Directory created: "+path);
|
||||
} catch (Exception e){
|
||||
System.out.println("Error creating directory: "+path+", Msg : "+e.getMessage());
|
||||
Logger.info("Error creating directory: "+path+", Msg : "+e.getMessage());
|
||||
}
|
||||
} else System.out.println("Directory exists: "+path);
|
||||
} else Logger.info("Directory exists: "+path);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static short ToShort(String x){
|
||||
try{
|
||||
return Short.parseShort(x);
|
||||
} catch (Exception e){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ShowAlert(Alert.AlertType type, String title, String header, String content){
|
||||
Alert alert = new Alert(type);
|
||||
alert.setTitle(title);
|
||||
alert.setHeaderText(header);
|
||||
alert.setContentText(content);
|
||||
alert.showAndWait();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user