commit 27/2025

Tambah Crop Setting
This commit is contained in:
2025-05-27 11:26:29 +07:00
parent 9e69714ae1
commit d0fe8123e9
9 changed files with 280 additions and 224 deletions

View File

@@ -35,7 +35,6 @@ import java.awt.image.BufferedImage;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;
@@ -117,11 +116,11 @@ public class Cameradetail {
private @Getter final UMat BestMat = new UMat();
private @Getter final UMat LiveMat = new UMat();
private @Getter final UMat ReducedMat = new UMat();
private @Getter final UMat GrayMat = new UMat();
private @Getter final Mat GrayMat = new Mat();
private @Getter Rect BestMatROI;
private @Getter Rect ReducedMatROI;
private @Getter Rect LiveMatROI;
// private @Getter Rect BestMatROI;
// private @Getter Rect ReducedMatROI;
// private @Getter Rect LiveMatROI;
private boolean IsPortrait = false;
@@ -157,7 +156,6 @@ public class Cameradetail {
private boolean use_qr = false;
private boolean use_face = false;
private int _hardwareID = -1;
private void setSliderValue(Slider sld, CameraProperty prop, double value){
@@ -267,10 +265,8 @@ public class Cameradetail {
public void setCameraTitle(String title){
if (ValidString(title)){
if (!Objects.equals(this.title, title)){
this.title = title;
LabelSetText(cameratitle, title,null);
}
this.title = title;
LabelSetText(cameratitle, title,null);
}
}
@@ -422,7 +418,6 @@ public class Cameradetail {
}
mGrabber = grabber;
_hardwareID = hardwareID;
}
//Exposure and Focus Tricks :
@@ -687,7 +682,7 @@ public class Cameradetail {
* @return filename path of the saved photo, or null if failed
*/
public PhotoResult TakePhoto(String directory, String prefix) {
PhotoResult result = new PhotoResult(cameratitle.getText());
PhotoResult result = new PhotoResult(title);
if (!ValidDirectory(directory)) directory = currentDirectory;
if (mGrabber!=null){
@@ -699,37 +694,48 @@ public class Cameradetail {
if (!BestMat.empty()){
UMat cloned;
UMat cloned = new UMat();
synchronized (BestMat){
cloned = BestMat.clone();
BestMat.copyTo(cloned);
}
// save BestMat at quality 9 PNG
String filename = GetFullQualityPhotoPath(directory, prefix);
if (opencv_imgcodecs.imwrite(filename, cloned, parampng)){
result.setFullres(filename);
} else System.out.println("TakePhoto failed, Unable to Save FullQUality Photo for camera "+cameratitle.getText());
String xx = CropBestMat(directory, prefix, BestMatROI);
if (ValidFile(xx)) {
result.setFullcrop(xx);
result.setBestROI(new Rect(BestMatROI.x(), BestMatROI.y(), BestMatROI.width(), BestMatROI.height()));
}
String fullresfile = GetFullQualityPhotoPath(directory, prefix);
if (opencv_imgcodecs.imwrite(fullresfile, cloned, parampng)){
result.setFullres(fullresfile);
} else System.out.println("TakePhoto failed, Unable to Save FullQUality Photo for camera "+title);
// save ReducedMat at 100% JPEG
String reducedfilename = GetReducedPhotoPath(directory, prefix);
String reducedfile = GetReducedPhotoPath(directory, prefix);
opencv_imgproc.resize(cloned, ReducedMat, ReducedSize);
if (!opencv_imgcodecs.imwrite(reducedfilename, ReducedMat, paramjpeg)){
System.out.println("TakePhoto failed, Unable to Save Reduced Photo for camera "+cameratitle.getText());
} else result.setCompressedfile(reducedfilename);
String xy = CropReducedMat(directory, prefix, ReducedMatROI);
if (ValidFile(xy)){
result.setCompressedcrop(xy);
result.setReducedROI(new Rect(ReducedMatROI.x(), ReducedMatROI.y(), ReducedMatROI.width(), ReducedMatROI.height()));
if (opencv_imgcodecs.imwrite(reducedfile, ReducedMat, paramjpeg)){
result.setCompressedfile(reducedfile);
} else {
System.out.println("TakePhoto failed, Unable to Save Reduced Photo for camera "+title);
}
String fullcropfile = GetFullQualityCropPhotoPath(directory, prefix);
UMat croppedfull = CropMat(cloned);
if (croppedfull!=null && !croppedfull.empty()){
if (opencv_imgcodecs.imwrite(fullcropfile, croppedfull, parampng)){
result.setFullcrop(fullcropfile);
} else {
System.out.println("TakePhoto failed, Unable to Save FullCrop Photo for camera "+title);
}
} else System.out.println("TakePhoto failed, Unable to Save FullCrop Photo for camera "+title);
String reducedcropfile = GetReducedCropPhotoPath(directory, prefix);
UMat reducedcrop = CropMat(ReducedMat);
if (reducedcrop!=null && !reducedcrop.empty()){
if (opencv_imgcodecs.imwrite(reducedcropfile, reducedcrop, paramjpeg)){
result.setCompressedcrop(reducedcropfile);
} else {
System.out.println("TakePhoto failed, Unable to Save ReducedCrop Photo for camera "+title);
}
} else System.out.println("TakePhoto failed, Unable to Save ReducedCrop Photo for camera "+title);
cloned.release();
} else raise_log("TakePhoto failed, Live View is Empty");
@@ -749,9 +755,9 @@ public class Cameradetail {
public String CropBestMat(String directory, String prefix, Rect ROI){
UMat cloned;
UMat cloned = new UMat();
synchronized (BestMat){
cloned = BestMat.clone();
BestMat.copyTo(cloned);
}
if (!cloned.empty()) {
if (ValidROI(ROI)){
@@ -796,34 +802,34 @@ public class Cameradetail {
private String makeFileName(String prefix, String extension){
LocalDateTime ldt = LocalDateTime.now();
String timetag = ldt.getYear() + "-" + ldt.getMonthValue() + "-" + ldt.getDayOfMonth() + "_" + ldt.getHour() + "-" + ldt.getMinute() + "-" + ldt.getSecond();
return prefix+" "+timetag+" "+cameratitle.getText() + extension;
return prefix+" "+timetag+" "+title + extension;
}
@SuppressWarnings("SameParameterValue")
private String makeReducedFileName(String prefix, String extension){
LocalDateTime ldt = LocalDateTime.now();
String timetag = ldt.getYear() + "-" + ldt.getMonthValue() + "-" + ldt.getDayOfMonth() + "_" + ldt.getHour() + "-" + ldt.getMinute() + "-" + ldt.getSecond();
return prefix+" "+timetag+" "+cameratitle.getText() + "_reduced" + extension;
return prefix+" "+timetag+" "+title + "_reduced" + extension;
}
public void Release(){
if (mGrabber!=null){
try{
StopLiveView();
mGrabber.release();
mGrabber = null;
} catch (Exception e){
System.out.println("Release failed, Unable to Release Camera, Error: " + e.getMessage());
}
}
}
// public void Release(){
// if (mGrabber!=null){
// try{
// StopLiveView();
// mGrabber.release();
// mGrabber = null;
// } catch (Exception e){
// System.out.println("Release failed, Unable to Release Camera, Error: " + e.getMessage());
// }
// }
// }
public void StopLiveView(){
Capturing.set(false);
if (mGrabber!=null){
try{
mGrabber.close();
System.out.println("Camera "+cameratitle.getText()+" stopped");
System.out.println("Camera "+title+" stopped");
setCameraStatus("Camera Stopped");
} catch (Exception e){
raise_log("StopLiveView failed, Unable to Stop Camera, Error: " + e.getMessage());
@@ -858,11 +864,8 @@ public class Cameradetail {
while(Capturing.get()){
try {
qr_semaphore.acquire();
UMat gray;
synchronized (lockObject){
gray = GrayMat;
}
String qr = DetectQRFromMat(gray);
String qr = DetectQRFromMat(GrayMat.clone());
if (ValidBarCode(qr)){
if (event!=null) event.onDetectedQRCode(qr);
}
@@ -887,10 +890,7 @@ public class Cameradetail {
while(Capturing.get()){
try {
face_semaphore.acquire();
UMat gray;
synchronized (lockObject){
gray = GrayMat;
}
DetectorResult theface = null;
boolean have_frontal_face = false;
@@ -898,7 +898,7 @@ public class Cameradetail {
int _face_width = 0;
int _face_height = 0;
List<DetectorResult> frontalfaces = HaveFrontalFace(gray);
List<DetectorResult> frontalfaces = HaveFrontalFace(GrayMat.clone());
if (!frontalfaces.isEmpty()){
for(DetectorResult rect : frontalfaces){
if (rect.haveFace() ){
@@ -916,7 +916,7 @@ public class Cameradetail {
} else {
// gak punya frontal face
// coba cek punya profile left face 45 gak
List<DetectorResult> Left45Faces = HaveLeft45Face(gray);
List<DetectorResult> Left45Faces = HaveLeft45Face(GrayMat.clone());
if (!Left45Faces.isEmpty()){
for(DetectorResult rect : Left45Faces){
if (rect.haveFace()){
@@ -938,9 +938,10 @@ public class Cameradetail {
if (event!=null) event.onFrontalFaceDetector(true, _face_width, _face_height);
LabelVisible(face_indicator,true);
if (theface.getFace()!=null){
LiveMatROI = new Rect(theface.getFace().x(), theface.getFace().y(), theface.getFace().width(), theface.getFace().height());
}
// Revisi 26/05/2025, LiveMatROI dihitung dari topcrop, bottomcrop, leftcrop dan rightcrop
// if (theface.getFace()!=null){
// LiveMatROI = new Rect(theface.getFace().x(), theface.getFace().y(), theface.getFace().width(), theface.getFace().height());
// }
if (theface.getEyesCount()>=2){
// ada mata (buka mata)
@@ -955,7 +956,7 @@ public class Cameradetail {
if (eye_state.get()!=1){
// transisi dari tutup mata ke buka mata
if (eye_state.get()==-1) {
System.out.println("First Eye Detected from camera "+cameratitle.getText());
System.out.println("First Eye Detected from camera "+title);
eye_state.set(1);
} else {
eye_state.set(1);
@@ -969,13 +970,13 @@ public class Cameradetail {
long diff = now - last_blink.get();
// kalau beda waktu antara blink 1 dan blink 2 kurang dari 3 detik
if (diff<=3000){
System.out.println("Double Blink Detected from camera "+cameratitle.getText());
System.out.println("Double Blink Detected from camera "+title);
if (event!=null) event.onDoubleBlink((int)diff);
}
waiting_for_second_blink.set(false);
} else {
waiting_for_second_blink.set(true);
System.out.println("First Blink Detected from camera "+cameratitle.getText());
System.out.println("First Blink Detected from camera "+title);
}
last_blink.set(now);
}
@@ -1029,13 +1030,19 @@ public class Cameradetail {
}
Mat imgmat = new Mat();
LiveMat.copyTo(imgmat);
// copy back to CPU
setCameraStream(MatToImage(imgmat));
imgmat.release();
if (LiveMat != null && !LiveMat.empty()){
Mat imgmat = new Mat();
// copy back to CPU
LiveMat.copyTo(imgmat);
setCameraStream(MatToImage(imgmat));
imgmat.release();
}
} catch (Exception e) {
//System.out.println(Thread.currentThread().getName()+" interrupted");
}
@@ -1055,7 +1062,7 @@ public class Cameradetail {
Frame frame = mGrabber.grab(); // grab frame
delta = System.currentTimeMillis() - now;
now = System.currentTimeMillis();
} catch (FrameGrabber.Exception ignored) {
} catch (Exception ignored) {
}
@@ -1081,11 +1088,9 @@ public class Cameradetail {
if (Capturing.get()) {
try{
if (mGrabber!=null)
frame = mGrabber.grab();
else throw new FrameGrabber.Exception("Grabber is null");
} catch (FrameGrabber.Exception e){
if (mGrabber==null) continue;
frame = mGrabber.grab();
} catch (Exception e){
if (Capturing.get()){
// kalau ada exception padahal masih capturing. Kalau sudah tidak capturing, tidak peduli
if (ValidString(e.getMessage())){
@@ -1139,15 +1144,29 @@ public class Cameradetail {
// rotate 90 degree counter clockwise karena kamera potrait
opencv_core.rotate(originalmat, BestMat, opencv_core.ROTATE_90_COUNTERCLOCKWISE);
originalmat.release();
if (!BestMat.empty()) {
// LiveMat and GrayMat are synchronized
synchronized (lockObject){
opencv_imgproc.resize(BestMat, LiveMat, LiveSize); // resize to LiveSize
opencv_imgproc.cvtColor(LiveMat,GrayMat, COLOR_BGR2GRAY); // convert to grayscale
// resize BestMat to LiveSize
UMat _liveMat = new UMat();
opencv_imgproc.resize(BestMat, _liveMat, LiveSize);
// crop LiveMat
UMat croppedLiveMat = CropMat(_liveMat);
if (croppedLiveMat!=null && !croppedLiveMat.empty()){
croppedLiveMat.copyTo(LiveMat);
croppedLiveMat.release();
} else {
_liveMat.copyTo(LiveMat);
_liveMat.release();
}
// convert to grayscale
Mat xx = new Mat();
LiveMat.copyTo(xx);
opencv_imgproc.cvtColor(xx,GrayMat, COLOR_BGR2GRAY);
}
if (use_qr){
@@ -1172,6 +1191,29 @@ public class Cameradetail {
}
});
private UMat CropMat(UMat mat){
if (mat != null && !mat.empty()){
int width = mat.cols();
int height = mat.rows();
int x = (int) (width * leftcrop / 100);
int y = (int) (height * topcrop / 100);
int w = (int) (width * (100 - leftcrop - rightcrop) / 100);
int h = (int) (height * (100 - topcrop - bottomcrop) / 100);
// Ensure the crop dimensions are valid
if (x >= 0 && y >= 0 && w > 0 && h > 0 && x + w <= width && y + h <= height) {
Rect roi = new Rect(x, y, w, h);
UMat crop = new UMat();
mat.apply(roi).copyTo(crop);
return crop;
}
}
return null;
}
public void ChangeCropValue(){
if ("01".equals(title)){
topcrop = config.getCam1TopCrop();
@@ -1281,90 +1323,89 @@ public class Cameradetail {
}
/**
* Remap LiveMatROI to BestMatROI and ReducedMatROI Resolution
* @param scaleX scale factor for width
* @param scaleY scale factor for height
*/
public void RemapROI(double scaleX, double scaleY, boolean printdebug){
BestMatROI = null;
ReducedMatROI = null;
if (ValidROI(LiveMatROI)){
if (ROIInsideUMat(LiveMatROI, LiveMat)){
if (printdebug) System.out.println("LiveMatROI camera "+cameratitle.getText()+" = "+RectToString(LiveMatROI));
double scaleXBest = 1.0*BestSize.width()/LiveSize.width();
double scaleYBest = 1.0*BestSize.height()/LiveSize.height();
int XBest = (int) (LiveMatROI.x()*scaleXBest);
int YBest = (int) (LiveMatROI.y()*scaleYBest);
int WBest = (int) (LiveMatROI.width()*scaleXBest);
int HBest = (int) (LiveMatROI.height()*scaleYBest);
int deltaWBest = (int) (BestSize.width() * scaleX);
int deltaHBest = (int) (BestSize.height() * scaleY);
XBest = XBest - deltaWBest/2;
if (XBest<0) XBest = 0;
YBest = YBest - deltaHBest/2;
if (YBest<0) YBest = 0;
WBest = WBest + deltaWBest;
if (WBest>BestSize.width()) WBest = BestSize.width();
HBest = HBest + deltaHBest;
if (HBest>BestSize.height()) HBest = BestSize.height();
BestMatROI = new Rect(XBest, YBest, WBest, HBest);
if (printdebug){
System.out.println("scaleXBest = "+scaleXBest+" scaleYBest = "+scaleYBest);
System.out.println("BestMatROI camera "+cameratitle.getText()+" = "+RectToString(BestMatROI));
}
double scaleXReduced = 1.0*ReducedSize.width()/LiveSize.width();
double scaleYReduced = 1.0*ReducedSize.height()/LiveSize.height();
int XReduced = (int) (LiveMatROI.x()*scaleXReduced);
int YReduced = (int) (LiveMatROI.y()*scaleYReduced);
int WReduced = (int) (LiveMatROI.width()*scaleXReduced);
int HReduced = (int) (LiveMatROI.height()*scaleYReduced);
int deltaWReduced = (int) (ReducedSize.width() * scaleX);
int deltaHReduced = (int) (ReducedSize.height() * scaleY);
XReduced = XReduced - deltaWReduced/2;
if (XReduced<0) XReduced = 0;
YReduced = YReduced - deltaHReduced/2;
if (YReduced<0) YReduced = 0;
WReduced = WReduced + deltaWReduced;
if (WReduced>ReducedSize.width()) WReduced = ReducedSize.width();
HReduced = HReduced + deltaHReduced;
if (HReduced>ReducedSize.height()) HReduced = ReducedSize.height();
ReducedMatROI = new Rect(XReduced, YReduced, WReduced, HReduced);
if (printdebug){
System.out.println("scaleXReduced = "+scaleXReduced+" scaleYReduced = "+scaleYReduced);
System.out.println("ReducedMatROI camera "+cameratitle.getText()+" = "+RectToString(ReducedMatROI));
}
}
}
}
// /**
// * Remap LiveMatROI to BestMatROI and ReducedMatROI Resolution
// * @param scaleX scale factor for width
// * @param scaleY scale factor for height
// */
// public void RemapROI(double scaleX, double scaleY, boolean printdebug){
// BestMatROI = null;
// ReducedMatROI = null;
// if (ValidROI(LiveMatROI)){
// if (ROIInsideUMat(LiveMatROI, LiveMat)){
// if (printdebug) System.out.println("LiveMatROI camera "+cameratitle.getText()+" = "+RectToString(LiveMatROI));
//
// double scaleXBest = 1.0*BestSize.width()/LiveSize.width();
// double scaleYBest = 1.0*BestSize.height()/LiveSize.height();
// int XBest = (int) (LiveMatROI.x()*scaleXBest);
// int YBest = (int) (LiveMatROI.y()*scaleYBest);
// int WBest = (int) (LiveMatROI.width()*scaleXBest);
// int HBest = (int) (LiveMatROI.height()*scaleYBest);
// int deltaWBest = (int) (BestSize.width() * scaleX);
// int deltaHBest = (int) (BestSize.height() * scaleY);
// XBest = XBest - deltaWBest/2;
// if (XBest<0) XBest = 0;
// YBest = YBest - deltaHBest/2;
// if (YBest<0) YBest = 0;
// WBest = WBest + deltaWBest;
// if (WBest>BestSize.width()) WBest = BestSize.width();
// HBest = HBest + deltaHBest;
// if (HBest>BestSize.height()) HBest = BestSize.height();
// BestMatROI = new Rect(XBest, YBest, WBest, HBest);
//
// if (printdebug){
// System.out.println("scaleXBest = "+scaleXBest+" scaleYBest = "+scaleYBest);
// System.out.println("BestMatROI camera "+cameratitle.getText()+" = "+RectToString(BestMatROI));
// }
//
// double scaleXReduced = 1.0*ReducedSize.width()/LiveSize.width();
// double scaleYReduced = 1.0*ReducedSize.height()/LiveSize.height();
// int XReduced = (int) (LiveMatROI.x()*scaleXReduced);
// int YReduced = (int) (LiveMatROI.y()*scaleYReduced);
// int WReduced = (int) (LiveMatROI.width()*scaleXReduced);
// int HReduced = (int) (LiveMatROI.height()*scaleYReduced);
// int deltaWReduced = (int) (ReducedSize.width() * scaleX);
// int deltaHReduced = (int) (ReducedSize.height() * scaleY);
// XReduced = XReduced - deltaWReduced/2;
// if (XReduced<0) XReduced = 0;
// YReduced = YReduced - deltaHReduced/2;
// if (YReduced<0) YReduced = 0;
// WReduced = WReduced + deltaWReduced;
// if (WReduced>ReducedSize.width()) WReduced = ReducedSize.width();
// HReduced = HReduced + deltaHReduced;
// if (HReduced>ReducedSize.height()) HReduced = ReducedSize.height();
// ReducedMatROI = new Rect(XReduced, YReduced, WReduced, HReduced);
// if (printdebug){
// System.out.println("scaleXReduced = "+scaleXReduced+" scaleYReduced = "+scaleYReduced);
// System.out.println("ReducedMatROI camera "+cameratitle.getText()+" = "+RectToString(ReducedMatROI));
// }
// }
// }
//
// }
/**
* Detect QR Code from Mat
* @param graymat Mat in Gray Scale
* @param mat Mat in Gray Scale
* @return QR Code Text, or null if not detected
*/
private String DetectQRFromMat(UMat graymat){
private String DetectQRFromMat(Mat mat){
if (qrreader!=null){
Mat mat = new Mat();
graymat.copyTo(mat); // back to CPU, because zxing only accept BufferedImage
BufferedImage bufferedImage = matToBufferedImage(mat);
String title = cameratitle.getText();
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
try{
Result result = qrreader.decode(binaryBitmap);
if (result!=null){
return result.getText();
if (mat!=null && !mat.empty()){
BufferedImage bufferedImage = matToBufferedImage(mat);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
try{
Result result = qrreader.decode(binaryBitmap);
if (result!=null){
return result.getText();
}
} catch (Exception ignored) {
}
} catch (Exception ignored) {
mat.release();
}
mat.release();
}
return null;
}