//package org.aohe.webcam;
//
//import lombok.extern.slf4j.Slf4j;
//import org.aohe.constant.Control;
//import org.aohe.constant.SocketEnum;
//import org.aohe.web.SocketUtils;
//import org.java_websocket.framing.BinaryFrame;
//
//import javax.imageio.ImageIO;
//import java.awt.image.BufferedImage;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.nio.ByteBuffer;
//import java.util.concurrent.Executors;
//import java.util.concurrent.ScheduledExecutorService;
//import java.util.concurrent.TimeUnit;
//
//@Slf4j
//public class WebcamShowTask2 extends Thread {
//
//    private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
//
//    @Override
//    public void run() {
//        scheduler.scheduleAtFixedRate(() -> {
//            try {
//                while (Control.SHOW_CAMERA) {
//                    WebCamUtils.open();
//                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
//                    BufferedImage bi = WebCamUtils.getImage();
//                    try {
//                        ImageIO.write(bi, "JPG", baos);
//                    } catch (IOException e) {
//                        //log.error(e.getMessage(), e);
//                    }finally {
//                        if (bi != null){
//                            bi.flush();
//                            bi = null;
//                        }
//                    }
////                    String base64 = null;
////                    try {
////                        base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
////                    } catch (UnsupportedEncodingException e) {
////                        log.error(e.getMessage(), e);
////                    }finally {
////                        baos.close();
////                    }
//
//                    SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(WebCamUtils.getImageByteBuffer());
//                }
//            } catch (Exception e) {
//                log.error(e.getMessage(), e);
//            }
//        }, 0, 100, TimeUnit.MILLISECONDS);
//
//        try {
//            Thread.currentThread().join();
//        } catch (InterruptedException e) {
//            log.error("Main thread interrupted", e);
//        }
//
//    }
//
//    public static void main(String[] args) {
//        SocketUtils.start(SocketEnum.CAM_SOCKET);
//        WebCamUtils.setWebcam(1);
//        WebCamUtils.open();
//        //File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png");
//        while (true){
//            ByteArrayOutputStream baos = new ByteArrayOutputStream();
//            BufferedImage bi = WebCamUtils.getImage();
//            try {
//                ImageIO.write(bi, "JPG", baos);
//            } catch (IOException e) {
//                //log.error(e.getMessage(), e);
//            }finally {
//                if (bi != null){
//                    bi.flush();
//                    bi = null;
//                }
//            }
////                    try {
////                        base64 = new String(Base64.getEncoder().encode(baos.toByteArray()), "UTF8");
////                    } catch (UnsupportedEncodingException e) {
////                        log.error(e.getMessage(), e);
////                    }finally {
////                        baos.close();
////                    }
//            BinaryFrame frame = new BinaryFrame();
//            frame.setPayload(ByteBuffer.wrap(baos.toByteArray()));
//            SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(frame);
//        }
//    }
//}
