first commit
This commit is contained in:
245
src/main/java/Other/SomeCodes.java
Normal file
245
src/main/java/Other/SomeCodes.java
Normal file
@@ -0,0 +1,245 @@
|
||||
package Other;
|
||||
|
||||
import org.bytedeco.javacv.Frame;
|
||||
import org.bytedeco.javacv.Java2DFrameConverter;
|
||||
import org.bytedeco.javacv.OpenCVFrameConverter;
|
||||
import org.bytedeco.opencv.global.opencv_core;
|
||||
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_core.UMat;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.tinylog.Logger;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.nio.file.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Properties;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class SomeCodes {
|
||||
public final static String currentDirectory = System.getProperty("user.dir");
|
||||
public final static Path audioPath = Path.of(currentDirectory, "audiofiles");
|
||||
private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
public static final OpenCVFrameConverter.ToMat matConverter = new OpenCVFrameConverter.ToMat();
|
||||
public static final Java2DFrameConverter frameConverter = new Java2DFrameConverter();
|
||||
public static final Path logsPath = Path.of(currentDirectory, "logs");
|
||||
public static final boolean haveOpenCL = opencv_core.haveOpenCL();
|
||||
public static boolean useOpenCL;
|
||||
|
||||
|
||||
public static String[] GetAudioFiles(){
|
||||
try{
|
||||
return Files.list(audioPath).map(f -> f.getFileName().toString()).toArray(String[]::new);
|
||||
} catch (Exception e){
|
||||
Logger.error("Error getting audio files: "+e.getMessage());
|
||||
}
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
public static String LocalDateTimeToString(LocalDateTime x){
|
||||
return x.format(dtf);
|
||||
}
|
||||
|
||||
public static String ExtractResource(String filename, String targetdirectory){
|
||||
try {
|
||||
File destination = new File(targetdirectory, filename);
|
||||
if (destination.exists()) {
|
||||
Logger.info("Resource File already exists: " + filename);
|
||||
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();
|
||||
} else {
|
||||
Logger.error("Resource File not found: "+filename);
|
||||
}
|
||||
|
||||
} 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 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 byte GetPanTiltSpeed(String data, byte defaultspeed){
|
||||
if (ValidInteger(data)){
|
||||
int speed = Integer.parseInt(data);
|
||||
if (speed<0) speed = 0;
|
||||
if (speed>0x3F) speed = 0x3F;
|
||||
return (byte)speed;
|
||||
}
|
||||
return defaultspeed;
|
||||
}
|
||||
|
||||
public static boolean ValidInteger(String x){
|
||||
try{
|
||||
Integer.parseInt(x);
|
||||
return true;
|
||||
} catch (Exception e){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean ValidPortNumber(int port){
|
||||
return port>0 && port<65536;
|
||||
}
|
||||
|
||||
public static boolean ValidPortNumber(String port){
|
||||
try{
|
||||
int portx = Integer.parseInt(port);
|
||||
return ValidPortNumber(portx);
|
||||
} catch (Exception e){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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 BufferedImage FrameToBufferedImage(Frame frame){
|
||||
return frameConverter.getBufferedImage(frame);
|
||||
}
|
||||
|
||||
public static BufferedImage MatToBufferedImage(Mat mat){
|
||||
return frameConverter.getBufferedImage(matConverter.convert(mat));
|
||||
}
|
||||
|
||||
public static String BufferedImageToBase64(BufferedImage image){
|
||||
if (image!=null){
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
try{
|
||||
javax.imageio.ImageIO.write(image, "jpg", baos);
|
||||
baos.flush();
|
||||
byte[] imageInByte = baos.toByteArray();
|
||||
baos.close();
|
||||
return java.util.Base64.getEncoder().encodeToString(imageInByte);
|
||||
} catch (Exception e){
|
||||
Logger.error("Error converting BufferedImage to Base64: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
public static @NotNull Properties LoadProperties(String filename){
|
||||
try{
|
||||
InputStream is = new FileInputStream(filename);
|
||||
Properties prop = new Properties();
|
||||
prop.load(is);
|
||||
return prop;
|
||||
} catch (Exception e){
|
||||
Logger.error("Error loading properties file: "+e.getMessage());
|
||||
}
|
||||
return new Properties();
|
||||
}
|
||||
|
||||
public static boolean SaveProperties(Properties prop, String filename){
|
||||
try{
|
||||
OutputStream os = new FileOutputStream(filename);
|
||||
prop.store(os, null);
|
||||
return true;
|
||||
} catch (Exception e){
|
||||
Logger.error("Error saving properties file: "+e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String GetConfigAudioFile(int index){
|
||||
Properties config = LoadProperties("config.properties");
|
||||
String key = String.format("AudioFile%02d", index);
|
||||
return config.getProperty(key, null);
|
||||
}
|
||||
|
||||
public static Mat ResizeMat(Mat source, int width, int height){
|
||||
Size sz = new Size(width, height);
|
||||
Mat dest = new Mat();
|
||||
if (useOpenCL){
|
||||
UMat src = new UMat();
|
||||
source.copyTo(src);
|
||||
UMat dst = new UMat();
|
||||
opencv_imgproc.resize(src, dst, sz);
|
||||
dst.copyTo(dest);
|
||||
} else {
|
||||
opencv_imgproc.resize(source, dest, sz);
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
public static Frame ResizeFrame(Frame source, int width, int height){
|
||||
Mat mat = matConverter.convertToMat(source);
|
||||
Mat resized = ResizeMat(mat, width, height);
|
||||
return matConverter.convert(resized);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user