/client/oim-fx/src/test/java/com/oim/test/video/VideoChatFrameSaveTest.java

https://github.com/oimchat/oim-fx · Java · 162 lines · 126 code · 27 blank · 9 comment · 5 complexity · d97bdb68df1dd18f08c304d1052b1cbd MD5 · raw file

  1. package com.oim.test.video;
  2. import java.awt.CardLayout;
  3. import java.awt.Color;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JLabel;
  10. import javax.swing.JPanel;
  11. import javax.swing.JTabbedPane;
  12. import org.bytedeco.javacpp.avcodec;
  13. import org.bytedeco.javacv.FFmpegFrameRecorder;
  14. import org.bytedeco.javacv.Frame;
  15. import org.bytedeco.javacv.OpenCVFrameConverter;
  16. import com.oim.swing.ui.video.CameraVideoPanel;
  17. public class VideoChatFrameSaveTest extends JFrame {
  18. private static final long serialVersionUID = 1L;
  19. JPanel basePanel = new JPanel();
  20. JPanel buttonPanel = new JPanel(new FlowLayout());
  21. JPanel tabPanel = new JPanel();
  22. JPanel ownPanel = new JPanel();
  23. JTabbedPane tabbedPane = new JTabbedPane();
  24. CameraVideoPanel cameraVideoPanel = new CameraVideoPanel();
  25. public VideoChatFrameSaveTest() {
  26. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27. this.setTitle("视频聊天");
  28. this.setSize(480, 680);
  29. this.setResizable(false);
  30. this.setLocationRelativeTo(null);
  31. this.setLayout(null);
  32. this.add(basePanel);
  33. this.add(buttonPanel);
  34. initUI();
  35. init();
  36. }
  37. private void initUI() {
  38. tabPanel.setLayout(new CardLayout());
  39. tabPanel.add(tabbedPane);
  40. basePanel.setBounds(0, 0, 450, 550);
  41. buttonPanel.setBounds(0, 550, 480, 70);
  42. basePanel.setLayout(null);
  43. basePanel.add(tabPanel);
  44. basePanel.add(cameraVideoPanel);
  45. basePanel.setBackground(new Color(125, 155, 52));
  46. tabPanel.setBounds(0, 0, 450, 275);
  47. cameraVideoPanel.setBounds(0, 275, 450, 275);
  48. JButton selectButton = new JButton("切换摄像头");
  49. buttonPanel.add(selectButton);
  50. selectButton.addActionListener(new ActionListener() {
  51. public void actionPerformed(ActionEvent e) {
  52. cameraVideoPanel.chooseDevice();
  53. }
  54. });
  55. JButton startButton = new JButton("开始");
  56. buttonPanel.add(startButton);
  57. startButton.addActionListener(new ActionListener() {
  58. public void actionPerformed(ActionEvent e) {
  59. cameraVideoPanel.startVideo();
  60. }
  61. });
  62. JButton stopButton = new JButton("停止");
  63. buttonPanel.add(stopButton);
  64. stopButton.addActionListener(new ActionListener() {
  65. public void actionPerformed(ActionEvent e) {
  66. cameraVideoPanel.stopVideo();
  67. }
  68. });
  69. new ImageThread().start();
  70. }
  71. private void init() {
  72. final JLabel label = new JLabel();
  73. JPanel panel = new JPanel();
  74. panel.setLayout(new CardLayout());
  75. panel.add(label);
  76. // panel.setBackground(Color.red);
  77. tabbedPane.addTab("000", panel);
  78. }
  79. public static void main(String[] args) {
  80. VideoChatFrameSaveTest camera = new VideoChatFrameSaveTest();
  81. camera.setVisible(true);
  82. }
  83. class ImageThread extends Thread {
  84. String saveMp4name = "Temp/f1.flv"; // 保存的视频名称
  85. FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(saveMp4name, 640, 480);
  86. OpenCVFrameConverter.ToIplImage conveter = new OpenCVFrameConverter.ToIplImage();
  87. long time = 0;
  88. boolean has = false;
  89. public void run() {
  90. //recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);// 28
  91. recorder.setVideoCodec(avcodec.AV_CODEC_ID_FLV1);// 28
  92. // recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4); // 13
  93. recorder.setFormat("flv");
  94. // recorder.setFormat("mov,mp4,m4a,3gp,3g2,mj2,h264,ogg,MPEG4");
  95. recorder.setFrameRate(20);
  96. recorder.setPixelFormat(0);// yuv420p
  97. while (true) {
  98. try {
  99. createImage();
  100. sleep(50);
  101. } catch (InterruptedException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. }
  106. public void createImage() {
  107. if (cameraVideoPanel.isStart()) {
  108. try {
  109. if (!has) {
  110. recorder.start();
  111. has = true;
  112. }
  113. Frame frame = cameraVideoPanel.getFrame();
  114. //IplImage image = conveter.convert(frame);
  115. recorder.record(frame);
  116. // 释放内存? cvLoadImage(fname); // 非常吃内存!!
  117. //opencv_core.cvReleaseImage(image);
  118. } catch (Exception e) {
  119. // TODO: handle exception
  120. }
  121. } else {
  122. if (has) {
  123. try {
  124. recorder.stop();
  125. recorder.release();
  126. has = false;
  127. } catch (org.bytedeco.javacv.FrameRecorder.Exception e) {
  128. // TODO Auto-generated catch block
  129. e.printStackTrace();
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }