//package org.aohe.webcam;
//
//import com.github.sarxos.webcam.Webcam;
//import com.github.sarxos.webcam.WebcamResolution;
//import io.humble.video.*;
//import io.humble.video.awt.MediaPictureConverter;
//import io.humble.video.awt.MediaPictureConverterFactory;
//import org.aohe.constant.SocketEnum;
//import org.aohe.web.SocketUtils;
//
//import java.awt.*;
//import java.awt.image.BufferedImage;
//import java.io.IOException;
//
//public class WebCam {
//
//    public static void main(String[] args) throws InterruptedException, IOException {
//
//        SocketUtils.start(SocketEnum.CAM_SOCKET);
//
//        Webcam webcam = Webcam.getDefault();
//        Dimension[] viewSizes = webcam.getViewSizes();
//        Dimension bigSize = WebcamResolution.VGA.getSize();
//        for (Dimension d : viewSizes){
//            //获取最大分辨率
//            if(bigSize.getWidth()<d.getWidth()){
//                bigSize = d;
//            }
//        }
//        webcam.setViewSize(bigSize);
//        webcam.open();
//
//        Rational framerate = Rational.make(1, 10); // 10 fps
//        int width = webcam.getViewSize().width;
//        int height = webcam.getViewSize().height;
//
//        Muxer muxer = Muxer.make("output.mp4", null, "mp4");
//        Codec codec = Codec.findEncodingCodec(Codec.ID.CODEC_ID_H264);
//        Encoder encoder = Encoder.make(codec);
//        encoder.setWidth(width);
//        encoder.setHeight(height);
//        encoder.setTimeBase(framerate);
//        encoder.setPixelFormat(PixelFormat.Type.PIX_FMT_YUV420P);
//        encoder.setFlag(Encoder.Flag.FLAG_GLOBAL_HEADER, true);
//        encoder.open(null, null);
//
//        MuxerStream muxerStream = muxer.addNewStream(encoder);
//        muxer.open(null, null);
//
//        MediaPicture picture = MediaPicture.make(
//                encoder.getWidth(),
//                encoder.getHeight(),
//                encoder.getPixelFormat());
//        picture.setTimeBase(framerate);
//
//        MediaPacket packet = MediaPacket.make();
//
//        System.out.println("BufferedImage Type: " + webcam.getImage().getType());
//        BufferedImage screen = convertToType(webcam.getImage(), BufferedImage.TYPE_3BYTE_BGR);
//        MediaPictureConverter converter = MediaPictureConverterFactory.createConverter(screen, picture);
//
//        while (true) {
//            System.out.println("frame 循环");
//            BufferedImage frame = convertToType(webcam.getImage(), BufferedImage.TYPE_3BYTE_BGR);
//            converter.toPicture(picture, frame, 0);
//
//            do {
//                System.out.println("packet 循环");
//                encoder.encode(packet, picture);
//                if (packet.isComplete()) {
//                    System.out.println("packet size: " + packet.getSize());
//                    SocketUtils.get(SocketEnum.CAM_SOCKET.getName())
//                            .broadcast(packet.getData().getByteBuffer(0, packet.getSize()));
//                    muxer.write(packet, false);
//                }
//            } while (packet.isComplete());
//
//            Thread.sleep(100);
//        }
//    }
//
//    public static BufferedImage convertToType(BufferedImage sourceImage,
//                                              int targetType) {
//        BufferedImage image;
//        // if the source image is already the target type, return the source image
//        if (sourceImage.getType() == targetType)
//            image = sourceImage;
//            // otherwise create a new image of the target type and draw the new
//            // image
//        else {
//            image = new BufferedImage(sourceImage.getWidth(),
//                    sourceImage.getHeight(), targetType);
//            image.getGraphics().drawImage(sourceImage, 0, 0, null);
//        }
//
//        return image;
//    }
//}
//
