Ask Your Question

Revision history [back]

How to copy recognized image (only recognize image not full image)

 private DaemonThread myThread = null;
int count = 0;
VideoCapture webSource = null;
Mat frame = new Mat();
MatOfByte mem = new MatOfByte();
CascadeClassifier faceDetector = new CascadeClassifier(FaceCrp.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));
MatOfRect faceDetections = new MatOfRect();
public Point point1,point2; 
Image img1;
Rect rect;

///

class DaemonThread implements Runnable {

    protected volatile boolean runnable = false;

    @Override
    public void run() {
        synchronized (this) {
            while (runnable) {
                if (webSource.grab()) {
                    try {
                        webSource.retrieve(frame);
                        Graphics g = jPanel1.getGraphics();
                        faceDetector.detectMultiScale(frame, faceDetections);
                        //Highgui.imwrite("d:/capture5.jpg", frame); No face detection till
                        //Rect rect = new Rect(faceDetections.toArray(frame));
                        for (Rect rect : faceDetections.toArray()) {
                           System.out.println("ttt");

                           Core.rectangle(frame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
                                    new Scalar(0, 255,0));
                           Highgui.imwrite("d:/capture6.jpg", frame);

                        }

                        Highgui.imencode(".bmp", frame, mem);
                        Image im = ImageIO.read(new ByteArrayInputStream(mem.toArray()));
                        BufferedImage buff = (BufferedImage) im;
                        if (g.drawImage(buff, 0, 0, getWidth(), getHeight()-150 , 0, 0, buff.getWidth(), buff.getHeight(), null))
                        {
                            //Highgui.imwrite("d:/capture2.jpg", frame);

                            System.out.println("ttt");
                            if (runnable == false) {
                                System.out.println("Paused ..... ");
                                this.wait();
                            }
                        }

                    } catch (Exception ex) {
                        System.out.println("Error");
                    }
                }
            }
        }
    }

}

Thanks in advance