first commit
This commit is contained in:
304
src/main/java/Config/SomeCodes.java
Normal file
304
src/main/java/Config/SomeCodes.java
Normal file
@@ -0,0 +1,304 @@
|
||||
package Config;
|
||||
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.zxing.MultiFormatReader;
|
||||
import javafx.embed.swing.SwingFXUtils;
|
||||
import javafx.scene.image.Image;
|
||||
import lombok.val;
|
||||
import org.bytedeco.javacv.Java2DFrameConverter;
|
||||
import org.bytedeco.javacv.OpenCVFrameConverter;
|
||||
import org.bytedeco.opencv.global.opencv_imgcodecs;
|
||||
import org.bytedeco.opencv.global.opencv_imgproc;
|
||||
import org.bytedeco.opencv.opencv_core.Mat;
|
||||
import org.bytedeco.opencv.opencv_core.Size;
|
||||
import org.bytedeco.opencv.opencv_objdetect.CascadeClassifier;
|
||||
import org.tinylog.Logger;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class SomeCodes {
|
||||
public final static String currentDirectory = System.getProperty("user.dir");
|
||||
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
public static final Java2DFrameConverter converter = new Java2DFrameConverter();
|
||||
public static final OpenCVFrameConverter.ToMat matconverter = new OpenCVFrameConverter.ToMat();
|
||||
public static final Gson gson = new Gson();
|
||||
public static final ConfigFile config = new ConfigFile();
|
||||
|
||||
public static Path GetLogsPath(){
|
||||
return Path.of(currentDirectory, "logs");
|
||||
}
|
||||
|
||||
public static int[] FindIndexes(List<String> source, String value){
|
||||
if (source!=null && !source.isEmpty()){
|
||||
if (ValidString(value)){
|
||||
List<Integer> result = new ArrayList<>();
|
||||
for (int i=0; i<source.size(); i++){
|
||||
if (source.get(i).equals(value)){
|
||||
result.add(i);
|
||||
}
|
||||
}
|
||||
if (!result.isEmpty()) return result.stream().mapToInt(i->i).toArray();
|
||||
}
|
||||
}
|
||||
return new int[0];
|
||||
}
|
||||
|
||||
public static int FindFirstIndex(List<String> source, String value, int... avoidedindex){
|
||||
if (source!=null && !source.isEmpty()){
|
||||
if (ValidString(value)){
|
||||
for (int i=0; i<source.size(); i++){
|
||||
if (source.get(i).equals(value)){
|
||||
// ketemu, tapi cek dulu apakah masuk avoidedindex
|
||||
|
||||
if (avoidedindex!=null && avoidedindex.length>0){
|
||||
boolean found = false;
|
||||
for (int j : avoidedindex){
|
||||
if (j!=-1){
|
||||
if (i==j){
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (found) continue;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static Image ConvertToImage(Mat mat, int width, int height){
|
||||
if (mat!=null){
|
||||
Mat resized = new Mat();
|
||||
opencv_imgproc.resize(mat, resized, new org.bytedeco.opencv.opencv_core.Size(width, height));
|
||||
BufferedImage img = converter.convert(matconverter.convert(resized));
|
||||
return SwingFXUtils.toFXImage(img, null);
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Find thumbfile in thumbs directory
|
||||
* @param sourcejpg source jpg file
|
||||
* @return thumbfile if found, or null if not found
|
||||
*/
|
||||
public static String FindThumbfile(String sourcejpg){
|
||||
File sourcefile = new File(sourcejpg);
|
||||
Path thumbpath = Path.of(sourcefile.getParent(), "thumbs");
|
||||
try{
|
||||
if (!Files.exists(thumbpath)) Files.createDirectories(thumbpath);
|
||||
} catch (Exception e){
|
||||
Logger.error("Error creating thumbs directory: "+thumbpath+", Msg : "+e.getMessage());
|
||||
}
|
||||
Path thumbfile = thumbpath.resolve(sourcefile.getName());
|
||||
if (Files.exists(thumbfile)){
|
||||
return thumbfile.toString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make thumbfile from source jpg file
|
||||
* @param sourcejpg source jpg file
|
||||
* @param thumbsize thumbfile Size
|
||||
* @return thumbfile if success, or null if failed
|
||||
*/
|
||||
public static String MakeThumbfile(String sourcejpg, Size thumbsize){
|
||||
try{
|
||||
File ff = new File(sourcejpg);
|
||||
if (ff.exists()){
|
||||
Path thumbpath = Path.of(ff.getParent(), "thumbs");
|
||||
if (!Files.exists(thumbpath)) Files.createDirectories(thumbpath);
|
||||
String thumbfile = thumbpath.resolve(ff.getName()).toString();
|
||||
File thumb = new File(thumbfile);
|
||||
if (thumb.exists()) return thumbfile;
|
||||
|
||||
Mat source = opencv_imgcodecs.imread(sourcejpg);
|
||||
if (source!=null && !source.empty()){
|
||||
Mat resized = new Mat();
|
||||
opencv_imgproc.resize(source, resized, thumbsize);
|
||||
opencv_imgcodecs.imwrite(thumbfile, resized);
|
||||
Logger.info("Thumbfile created: "+thumbfile);
|
||||
return thumbfile;
|
||||
} else Logger.info("MakeThumbfile failed, Source File not valid image : "+sourcejpg);
|
||||
} else Logger.info("MakeThumbfile failed, Source File not found: "+sourcejpg);
|
||||
} catch (Exception e){
|
||||
Logger.error("Error making thumbfile: "+sourcejpg+", Msg : "+e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String RemoveSpaces(String x){
|
||||
return x.replaceAll("\\s+","");
|
||||
}
|
||||
|
||||
public static String LocalDateTimeToString(LocalDateTime x){
|
||||
return x.format(dtf);
|
||||
}
|
||||
|
||||
public static String ExtractResource(String filename){
|
||||
try{
|
||||
File destination = new File(currentDirectory, filename);
|
||||
if (destination.exists()){
|
||||
return destination.getAbsolutePath();
|
||||
}
|
||||
InputStream is = SomeCodes.class.getResourceAsStream(filename);
|
||||
if (is!=null){
|
||||
Files.copy(is, destination.toPath());
|
||||
Logger.info("Resource File extracted: "+filename);
|
||||
return destination.getAbsolutePath();
|
||||
}
|
||||
} catch (Exception e){
|
||||
Logger.error("Error extracting resource: "+filename+", Message : "+e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean ValidDirectory(String path){
|
||||
if (ValidString(path)){
|
||||
File ff = new File(path);
|
||||
return ff.isDirectory();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean ValidPortNumber(int port){
|
||||
return port>0 && port<65536;
|
||||
}
|
||||
|
||||
public static int toInt(String x){
|
||||
try {
|
||||
return Integer.parseInt(x);
|
||||
} catch (Exception e){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean ValidIPV4(String ipaddress){
|
||||
if (ValidString(ipaddress)){
|
||||
try{
|
||||
InetAddress inet = InetAddress.getByName(ipaddress);
|
||||
if (inet instanceof Inet4Address){
|
||||
if (inet.getHostAddress().equals(ipaddress)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean ValidIPV6(String ipaddress){
|
||||
if (ValidString(ipaddress)){
|
||||
try{
|
||||
InetAddress inet = InetAddress.getByName(ipaddress);
|
||||
if (inet instanceof Inet6Address){
|
||||
if (inet.getHostAddress().equals(ipaddress)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String GetFileName(String filepath){
|
||||
if (ValidString(filepath)){
|
||||
File ff = new File(filepath);
|
||||
if (ff.isFile()){
|
||||
return ff.getName();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static boolean ValidFile(String filename){
|
||||
if (ValidString(filename)){
|
||||
File ff = new File(filename);
|
||||
return ff.isFile();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean ValidString(String x){
|
||||
if (x!=null){
|
||||
return !x.isEmpty();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void OpenPictureInDefaultViewer(String filename){
|
||||
try{
|
||||
File ff = new File(filename);
|
||||
if (ff.exists()){
|
||||
String os = System.getProperty("os.name").toLowerCase();
|
||||
if (os.contains("win")){
|
||||
//Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+ff.getAbsolutePath());
|
||||
Runtime.getRuntime().exec(new String[]{"rundll32", "url.dll,FileProtocolHandler", ff.getAbsolutePath()});
|
||||
} else if (os.contains("mac")){
|
||||
//Runtime.getRuntime().exec("open "+ff.getAbsolutePath());
|
||||
Runtime.getRuntime().exec(new String[]{"open", ff.getAbsolutePath()});
|
||||
} else if (os.contains("nix") || os.contains("nux")){
|
||||
//Runtime.getRuntime().exec("xdg-open "+ff.getAbsolutePath());
|
||||
Runtime.getRuntime().exec(new String[]{"xdg-open", ff.getAbsolutePath()});
|
||||
}
|
||||
}
|
||||
} catch (Exception e){
|
||||
Logger.error("Error opening file: "+filename+", Msg : "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static MultiFormatReader qrreader;
|
||||
public static void LoadQRReader(){
|
||||
if (qrreader==null) {
|
||||
qrreader = new MultiFormatReader();
|
||||
Logger.info("QRReader loaded");
|
||||
}
|
||||
|
||||
}
|
||||
public static CascadeClassifier faceDetector;
|
||||
public static void LoadFaceDetector(){
|
||||
String filename = SomeCodes.ExtractResource("/haarcascade_frontalface_alt.xml");
|
||||
if (filename!=null) {
|
||||
if (faceDetector==null) {
|
||||
faceDetector = new CascadeClassifier(filename);
|
||||
Logger.info("FaceDetector loaded");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String[] MakeArray(String... args){
|
||||
if (args!=null && args.length>0){
|
||||
List<String> ll = new ArrayList<String>();
|
||||
for(String x : args){
|
||||
if (ValidString(x)) ll.add(x);
|
||||
}
|
||||
return ll.toArray(new String[0]);
|
||||
}
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user